这是我在stackoverflow上的第一个问题,所以如果我做错了什么/不合适,请帮助我。
我尝试使用隐含意图创建一个基本练习应用来调用activity1中的一个活动(activity2)。但由于某种原因,我有这个错误让我疯了:
java.lang.IllegalStateException: Could not execute method for android:onClick.
以下是activity1的代码:
public class FirstActivity extends Activity {
private final String CLASS_NAME = "FirstActiviy";
private int request_Code = 1;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.firstactivity);
Log.d( CLASS_NAME, "onCreate invoked!" );
}
public void onStart(){
super.onStart();
Log.d( CLASS_NAME, "onStart invoked!" );
}
public void onPause(){
super.onPause();
Log.d( CLASS_NAME, "onPause invoked!!" );
}
public void onResume(){
super.onResume();
Log.d( CLASS_NAME, "onResume invoked!!" );
}
public void onStop(){
super.onStop();
Log.d( CLASS_NAME, "onStop invoked!!!" );
}
public void onDestroy(){
super.onDestroy();
Log.d( CLASS_NAME, "onDestroy INVOKED!!!" );
}
public void onRestart(){
super.onRestart();
Log.d( CLASS_NAME, "onRestart invoked!!" );
}
// event handler for the button (activity 2)
public void onClick(View view) {
startActivity(new Intent("com.seneca.lab2b.abbas"));
}
// event handler for a button (activity 3)
public void onClick3(View view) {
startActivity( new Intent( this, ThirdActivity.class ) );
}
}
以下是错误日志:
09-15 01:45:42.070 10359-10359/com.seneca.lab2b E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.seneca.lab2b, PID: 10359
java.lang.IllegalStateException: Could not execute method for android:onClick
at android.view.View$DeclaredOnClickListener.onClick(View.java:4452)
at android.view.View.performClick(View.java:5198)
at android.view.View$PerformClick.run(View.java:21147)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at android.view.View$DeclaredOnClickListener.onClick(View.java:4447)
at android.view.View.performClick(View.java:5198)
at android.view.View$PerformClick.run(View.java:21147)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: android.content.ActivityNotFoundException: No Activity found to handle Intent { act=com.seneca.lab2b.abbas }
at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1798)
at android.app.Instrumentation.execStartActivity(Instrumentation.java:1512)
at android.app.Activity.startActivityForResult(Activity.java:3917)
at android.app.Activity.startActivityForResult(Activity.java:3877)
at android.app.Activity.startActivity(Activity.java:4200)
at android.app.Activity.startActivity(Activity.java:4168)
at com.seneca.lab2b.FirstActivity.onClick(FirstActivity.java:59)
at java.lang.reflect.Method.invoke(Native Method)
at android.view.View$DeclaredOnClickListener.onClick(View.java:4447)
at android.view.View.performClick(View.java:5198)
at android.view.View$PerformClick.run(View.java:21147)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
布局xml文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/display"
android:textSize="20sp"
android:onClick="onClick"/>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/display3"
android:textSize="20sp"
android:onClick="onClick3"/>
</LinearLayout>
答案 0 :(得分:1)
非常感谢,但正如我在最初的问题中所说,我想要这样的事情:&#34;事件处理程序应该使用隐式意图从FirstActivity调用SecondActivity类。使用的操作名称 意图过滤器应该是“com.seneca.lab2b .. &#34;
答案 1 :(得分:0)
在某些设备中,xml onclick方法无效,因为新版本的android不支持onclick(查看)。 因此,在xml中删除并在JAVA类中创建onclick。
答案 2 :(得分:0)
你的xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:id="@+id/button1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/display"
android:textSize="20sp"/>
<Button
android:id="@+id/button2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/display3"
android:textSize="20sp"/>
</LinearLayout>
您的Java类:
public class FirstActivity extends Activity {
private final String CLASS_NAME = "FirstActiviy";
private int request_Code = 1;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.firstactivity);
Log.d( CLASS_NAME, "onCreate invoked!" );
b1 = (Button) findViewById(R.id.button1);
b2 = (Button) findViewById(R.id.button2);
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
startActivity(newIntent(FirstActivity.this,ThirdActivity.class));
}
});
b2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
startActivity(newIntent(FirstActivity.this,ThirdActivity.class));
}
});
}
public void onStart(){
super.onStart();
Log.d( CLASS_NAME, "onStart invoked!" );
}
public void onPause(){
super.onPause();
Log.d( CLASS_NAME, "onPause invoked!!" );
}
public void onResume(){
super.onResume();
Log.d( CLASS_NAME, "onResume invoked!!" );
}
public void onStop(){
super.onStop();
Log.d( CLASS_NAME, "onStop invoked!!!" );
}
public void onDestroy(){
super.onDestroy();
Log.d( CLASS_NAME, "onDestroy INVOKED!!!" );
}
public void onRestart(){
super.onRestart();
Log.d( CLASS_NAME, "onRestart invoked!!" );
}
}