我是 android 编程的新手,我尝试搜索如何修复此错误;但是,我仍然无法弄清楚我做错了什么。我只是按照 youtube 教程进行操作,并决定创建自己的程序来练习。我还没有完成该程序,但基本上,我试图将按钮从我的 .main xml(又名菜单类)链接到 triangle_guide.xml 页面,但由于某种原因,它不起作用。请帮忙。多谢。
日志猫
09-14 04:44:16.444: E/AndroidRuntime(277): FATAL EXCEPTION: main
09-14 04:44:16.444: E/AndroidRuntime(277): java.lang.IllegalStateException: Could not find a method btnGuide(View) in the activity class com.example.trianglegame.MainActivity for onClick handler on view class android.widget.Button with id 'btnGuide'
09-14 04:44:16.444: E/AndroidRuntime(277): at android.view.View$1.onClick(View.java:2059)
09-14 04:44:16.444: E/AndroidRuntime(277): at android.view.View.performClick(View.java:2408)
09-14 04:44:16.444: E/AndroidRuntime(277): at android.view.View$PerformClick.run(View.java:8816)
09-14 04:44:16.444: E/AndroidRuntime(277): at android.os.Handler.handleCallback(Handler.java:587)
09-14 04:44:16.444: E/AndroidRuntime(277): at android.os.Handler.dispatchMessage(Handler.java:92)
09-14 04:44:16.444: E/AndroidRuntime(277): at android.os.Looper.loop(Looper.java:123)
09-14 04:44:16.444: E/AndroidRuntime(277): at android.app.ActivityThread.main(ActivityThread.java:4627)
09-14 04:44:16.444: E/AndroidRuntime(277): at java.lang.reflect.Method.invokeNative(Native Method)
09-14 04:44:16.444: E/AndroidRuntime(277): at java.lang.reflect.Method.invoke(Method.java:521)
09-14 04:44:16.444: E/AndroidRuntime(277): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
09-14 04:44:16.444: E/AndroidRuntime(277): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
09-14 04:44:16.444: E/AndroidRuntime(277): at dalvik.system.NativeStart.main(Native Method)
09-14 04:44:16.444: E/AndroidRuntime(277): Caused by: java.lang.NoSuchMethodException: btnGuide
09-14 04:44:16.444: E/AndroidRuntime(277): at java.lang.ClassCache.findMethodByName(ClassCache.java:308)
09-14 04:44:16.444: E/AndroidRuntime(277): at java.lang.Class.getMethod(Class.java:985)
09-14 04:44:16.444: E/AndroidRuntime(277): at android.view.View$1.onClick(View.java:2052)
09-14 04:44:16.444: E/AndroidRuntime(277): ... 11 more
MainActivity Java 代码
package com.example.trianglegame;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
菜单按钮类
package com.example.trianglegame;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MenuBtn extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button btnGuide = (Button) findViewById(R.id.btnGuide);
btnGuide.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(v.getContext(), TriangleGuide.class);
startActivityForResult(intent, 0);
}
});
}
}
指导类
package com.example.trianglegame;
import android.app.Activity;
import android.os.Bundle;
public class TriangleGuide extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.triangle_guide);
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
}
}
Android list
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.trianglegame"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="21" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".TriangleGuide"></activity>
</application>
</manifest>
.main xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/background"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.trianglegame.MainActivity" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:gravity="center"
android:text="@string/Welcome"
android:textSize="25sp" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView1"
android:layout_centerHorizontal="true"
android:layout_marginTop="20sp"
android:text="@string/select_option" />
<Button
android:id="@+id/btnGuide"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView2"
android:layout_below="@+id/textView2"
android:layout_marginLeft="34dp"
android:layout_marginTop="113dp"
android:text="@string/guide_btn"
android:onClick="btnGuide" />
<Button
android:id="@+id/btnGame"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView2"
android:layout_below="@+id/btnGuide"
android:layout_marginLeft="34dp"
android:text="@string/game_btn" />
</RelativeLayout>
三角指南.xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="@+id/t_tri"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="@string/e_tri"
android:textSize="15sp"
android:textStyle="bold" />
<View
android:layout_width="1dp"
android:layout_height="40dp"
android:layout_weight="1" />
<ImageView
android:id="@+id/e_triangle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:contentDescription="@string/e_tri"
android:src="@drawable/e_triangle" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="@string/e_tri" />
<View
android:layout_width="1dp"
android:layout_height="40dp"
android:layout_weight="1" />
<ImageView
android:id="@+id/i_triangle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:contentDescription="@string/i_tri"
android:src="@drawable/i_triangle" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="@string/i_tri" />
<View
android:layout_width="1dp"
android:layout_height="40dp"
android:layout_weight="1" />
<ImageView
android:id="@+id/s_triangle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:contentDescription="@string/s_tri"
android:src="@drawable/s_triangle" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="@string/s_tri" />
<TextView>
</TextView>
</LinearLayout>
</ScrollView>
最佳答案
如果你想在你的.xml中注册监听器android:onClick="btnGuide"像这样
那么你的 Activity 必须包含一个方法 btnGuide:
public void btnGuide(View v){
switch(v.getId()){
case R.id.btnGuide:
Intent intent = new Intent(v.getContext(), TriangleGuide.class);
startActivityForResult(intent, 0);
break;
}
}
现在不需要:
btnGuide.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(v.getContext(), TriangleGuide.class);
startActivityForResult(intent, 0);
}
}
);
关于java - 应用程序意外停止错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25830288/
我需要在客户计算机上运行Ruby应用程序。通常需要几天才能完成(复制大备份文件)。问题是如果启用sleep,它会中断应用程序。否则,计算机将持续运行数周,直到我下次访问为止。有什么方法可以防止执行期间休眠并让Windows在执行后休眠吗?欢迎任何疯狂的想法;-) 最佳答案 Here建议使用SetThreadExecutionStateWinAPI函数,使应用程序能够通知系统它正在使用中,从而防止系统在应用程序运行时进入休眠状态或关闭显示。像这样的东西:require'Win32API'ES_AWAYMODE_REQUIRED=0x0
对于具有离线功能的智能手机应用程序,我正在为Xml文件创建单向文本同步。我希望我的服务器将增量/差异(例如GNU差异补丁)发送到目标设备。这是计划:Time=0Server:hasversion_1ofXmlfile(~800kiB)Client:hasversion_1ofXmlfile(~800kiB)Time=1Server:hasversion_1andversion_2ofXmlfile(each~800kiB)computesdeltaoftheseversions(=patch)(~10kiB)sendspatchtoClient(~10kiBtransferred)Cl
大约一年前,我决定确保每个包含非唯一文本的Flash通知都将从模块中的方法中获取文本。我这样做的最初原因是为了避免一遍又一遍地输入相同的字符串。如果我想更改措辞,我可以在一个地方轻松完成,而且一遍又一遍地重复同一件事而出现拼写错误的可能性也会降低。我最终得到的是这样的:moduleMessagesdefformat_error_messages(errors)errors.map{|attribute,message|"Error:#{attribute.to_s.titleize}#{message}."}enddeferror_message_could_not_find(obje
Rackup通过Rack的默认处理程序成功运行任何Rack应用程序。例如:classRackAppdefcall(environment)['200',{'Content-Type'=>'text/html'},["Helloworld"]]endendrunRackApp.new但是当最后一行更改为使用Rack的内置CGI处理程序时,rackup给出“NoMethodErrorat/undefinedmethod`call'fornil:NilClass”:Rack::Handler::CGI.runRackApp.newRack的其他内置处理程序也提出了同样的反对意见。例如Rack
我想用ruby编写一个小的命令行实用程序并将其作为gem分发。我知道安装后,Guard、Sass和Thor等某些gem可以从命令行自行运行。为了让gem像二进制文件一样可用,我需要在我的gemspec中指定什么。 最佳答案 Gem::Specification.newdo|s|...s.executable='name_of_executable'...endhttp://docs.rubygems.org/read/chapter/20 关于ruby-在Ruby中编写命令行实用程序
为了将Cucumber用于命令行脚本,我按照提供的说明安装了arubagem。它在我的Gemfile中,我可以验证是否安装了正确的版本并且我已经包含了require'aruba/cucumber'在'features/env.rb'中为了确保它能正常工作,我写了以下场景:@announceScenario:Testingcucumber/arubaGivenablankslateThentheoutputfrom"ls-la"shouldcontain"drw"假设事情应该失败。它确实失败了,但失败的原因是错误的:@announceScenario:Testingcucumber/ar
我构建了两个需要相互通信和发送文件的Rails应用程序。例如,一个Rails应用程序会发送请求以查看其他应用程序数据库中的表。然后另一个应用程序将呈现该表的json并将其发回。我还希望一个应用程序将存储在其公共(public)目录中的文本文件发送到另一个应用程序的公共(public)目录。我从来没有做过这样的事情,所以我什至不知道从哪里开始。任何帮助,将不胜感激。谢谢! 最佳答案 无论Rails是什么,几乎所有Web应用程序都有您的要求,大多数现代Web应用程序都需要相互通信。但是有一个小小的理解需要你坚持下去,网站不应直接访问彼此
我尝试运行2.x应用程序。我使用rvm并为此应用程序设置其他版本的ruby:$rvmuseree-1.8.7-head我尝试运行服务器,然后出现很多错误:$script/serverNOTE:Gem.source_indexisdeprecated,useSpecification.Itwillberemovedonorafter2011-11-01.Gem.source_indexcalledfrom/Users/serg/rails_projects_terminal/work_proj/spohelp/config/../vendor/rails/railties/lib/r
刚入门rails,开始慢慢理解。有人可以解释或给我一些关于在application_controller中编码的好处或时间和原因的想法吗?有哪些用例。您如何为Rails应用程序使用应用程序Controller?我不想在那里放太多代码,因为据我了解,每个请求都会调用此Controller。这是真的? 最佳答案 ApplicationController实际上是您应用程序中的每个其他Controller都将从中继承的类(尽管这不是强制性的)。我同意不要用太多代码弄乱它并保持干净整洁的态度,尽管在某些情况下ApplicationContr
我真的很习惯使用Ruby编写以下代码:my_hash={}my_hash['test']=1Java中对应的数据结构是什么? 最佳答案 HashMapmap=newHashMap();map.put("test",1);我假设? 关于java-等价于Java中的RubyHash,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/22737685/