DialogActivity:java.lang.illegalStateException

时间:2014-06-27 09:20:41

标签: android stack-trace illegalstateexception runtimeexception

创建DialogActivity时我在运行时收到错误。它显示了java.lang.illegalStateExcepion。

堆栈跟踪:

 E/AndroidRuntime(1685): FATAL EXCEPTION: main
 E/AndroidRuntime(1685): Process: com.steve.dialogactivity, PID: 1685 E/AndroidRuntime(1685): java.lang.IllegalStateException: Could not find a method onClick(View) in the activity class com.steve.dialogactivity.MainActivity for onClick handler on view class android.widget.Button with id 'btn_dislog'
 E/AndroidRuntime(1685):    at android.view.View$1.onClick(View.java:3810)
 E/AndroidRuntime(1685):    at android.view.View.performClick(View.java:4438)
 E/AndroidRuntime(1685):    at android.view.View$PerformClick.run(View.java:18422)
 E/AndroidRuntime(1685):    at android.os.Handler.handleCallback(Handler.java:733) E/AndroidRuntime(1685):  at android.os.Handler.dispatchMessage(Handler.java:95)
 E/AndroidRuntime(1685):    at android.os.Looper.loop(Looper.java:136)
 E/AndroidRuntime(1685):    at android.app.ActivityThread.main(ActivityThread.java:5017)
 E/AndroidRuntime(1685):    at java.lang.reflect.Method.invokeNative(Native Method)
 E/AndroidRuntime(1685):    at java.lang.reflect.Method.invoke(Method.java:515)
 E/AndroidRuntime(1685):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
 E/AndroidRuntime(1685):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
 E/AndroidRuntime(1685):    at dalvik.system.NativeStart.main(Native Method)
 E/AndroidRuntime(1685): Caused by: java.lang.NoSuchMethodException: onClick [class android.view.View]
 E/AndroidRuntime(1685):    at java.lang.Class.getConstructorOrMethod(Class.java:472)
 E/AndroidRuntime(1685):    at java.lang.Class.getMethod(Class.java:857)
 E/AndroidRuntime(1685):    at android.view.View$1.onClick(View.java:3803)
 E/AndroidRuntime(1685):    ... 11 more

MainActivity.java:

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.util.Log;
import android.view.Window;
import android.widget.Toast;

public class MainActivity extends Activity{

    CharSequence[] items={"Google","Apple","Windows"};
    boolean[] itemsChecked=new boolean[items.length];

    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

    }

    public void onClick(){
        showDialog(0);
    }

    @Override
    protected Dialog onCreateDialog(int id){
        switch(id){
        case  0:
            return new AlertDialog.Builder(this).setIcon(R.drawable.ic_launcher)
                    .setTitle("This is a Dialog").setPositiveButton("OK", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int whichButton) {
                                    Toast.makeText(getBaseContext(), "OK Clicked", Toast.LENGTH_SHORT).show();
                                }

                            }).setNegativeButton("Cancel", new DialogInterface.OnClickListener() {

                                @Override
                                public void onClick(DialogInterface dialog, int whichButton) {
                                    Toast.makeText(getBaseContext(), "Cancel Clicked", Toast.LENGTH_SHORT).show();
                                }
                            }).setMultiChoiceItems(items, itemsChecked, new DialogInterface.OnMultiChoiceClickListener() {


                                @Override
                                public void onClick(DialogInterface dialog, int whichButton, boolean isChecked) {
                                    Toast.makeText(getBaseContext(), items[whichButton]+(isChecked?"checked!":"unchecked!"), Toast.LENGTH_SHORT).show();
                                }
                            }).create();


        }
        return null;
    }


}

activity_main.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/btn_dialog"
       android:layout_width="fill_parent"
       android:layout_height="wrap_content"
       android:text="click to display a dislog"
       android:onClick="onClick"/>

</LinearLayout>

清单:

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

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="19" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.steve.dialogactivity.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>
    </application>

</manifest>

任何人都可以帮助我。谢谢。

4 个答案:

答案 0 :(得分:2)

尝试这种方式:您应该在方法View中将onClick()作为参数传递

public void onClick(View v){
    showDialog(0);
}

答案 1 :(得分:1)

您必须在onClick

的方法中传递View参数
public void onClick(View view){
        showDialog(0);
    }

答案 2 :(得分:1)

public void onClick()替换为public void onClick(View view){

答案 3 :(得分:1)

原因如下所示无法找到方法onClick(查看)

 Process: com.steve.dialogactivity, PID: 1685 E/AndroidRuntime(1685): java.lang.IllegalStateException: Could not find a method onClick(View) in the activity class com.steve.dialogactivity.MainActivity for onClick handler on view class android.widget.Button with id 'btn_dislog'

     Caused by: java.lang.NoSuchMethodException: onClick [class android.view.View]

这意味着您没有任何名为onClick的方法,其参数为View类型。更改您的onclick方法如下

public void onClick(View view){
        showDialog(0);
    }