Android编程错误IllegalStateException

时间:2016-09-15 20:43:57

标签: android android-intent android-activity

您好,其他程序员,我正在创建一个隐含地从Activity1调用Activity2的应用程序。我尝试从Activity1中明确地调用Activity3,这似乎工作得很好。你能帮我找到如何克服我的错误吗?顺便说一句,我必须这么做。

当通过模拟器运行应用程序时,系统会发送类似这样的错误"不幸的是,应用已停止"

这是LogCat:

09-15 08:31:03.977 31960-31960/com.seneca.lab2b E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.seneca.lab2b, PID: 31960
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:60)
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) 
09-15 08:31:08.979 31960-31960/com.seneca.lab2b I/Process: Sending signal. PID: 31960 SIG: 9

这是FirstActivity的布局文件:

<?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>

SecondActivity的布局:

<?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" >

<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="This is the Second Activity!" />

<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Please enter your name" />

<EditText
    android:id="@+id/txt_username"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" />

<Button
    android:id="@+id/btn_OK"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="OK" 
    android:onClick="onClick"/>

</LinearLayout>

这是清单文件:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.seneca.lab2b"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="21"
              android:targetSdkVersion="23"/>

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >

        <activity
            android:label="@string/app_name"
            android:name="com.seneca.lab2b.FirstActivity" >
            <intent-filter >
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:label="Second Activity"
            android:name="com.seneca.lab2b.SecondActivity" >
            <intent-filter >
                <action android:name="com.seneca.abbas" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>

        <activity
            android:label="Third Activity"
            android:name="com.seneca.lab2b.ThirdActivity" >
            <intent-filter >
                <action android:name="com.seneca.third" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>

    </application>

</manifest>

这是FirstActivity的java文件:

package com.seneca.lab2b;

import com.seneca.lab2b.R;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;

import android.util.Log;
import android.view.View;


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"));
        //Intent intent=new Intent(Intent.ACTION_VIEW);
        //intent.setData(Uri.parse("com.seneca.lab2b.abbas"));
        //startActivity(intent);
    }

    // event handler for a button (activity 3)
    public void onClick3(View view) {

        startActivity( new Intent( this, ThirdActivity.class ) );

    }
}

2 个答案:

答案 0 :(得分:1)

在onClick方法中你有:

startActivity(new Intent("com.seneca.lab2b.abbas"));

和xml:

com.seneca.abbas

你传递了错误的动作名称(lab2b不在xml中),这可能是原因,修复它。

答案 1 :(得分:0)

查看Android Start Another Activity文档

根据您的错误日志,您的意图找不到您的活动。

您可以开始下面的第二项活动。

 public void onClick(View view) {

    Intent intent = new Intent(getApplicationContext(),SecondActivity.class);
    startActivity(intent);
 }