非法状态异常onclick

时间:2014-04-25 23:58:49

标签: java android

我正在尝试通过单击活动中的按钮来检索数据。我在activity_second中声明了一个onclick.Here是我的代码。我得到的是IllegalstateException:方法无法在activity中执行。我的代码出了什么问题。为什么logcat显示InvocationTargetException。究竟这个错误是什么以及为什么NullPointerException存在??

public class SecondActivity extends Activity {

private EditText loadinfo;
private Button butn1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
loadinfo = (EditText)findViewById(R.id.editText1);
butn1 = (Button)findViewById(R.id.button1);
}

public void apurv1(View view){
File folder = getCacheDir();
File myfile = new File(folder, "mydata1.txt");
String data = readdata(myfile);
if(data!=null){
loadinfo.setText(data);
}
    else {

        loadinfo.setText("no entry was done");
        }
    }



private String readdata(File myfile){

    FileInputStream fis= null;
    try {
        fis = new FileInputStream(myfile);
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    int read = -1;
    StringBuffer stringbuffer = new StringBuffer();
    try {
        while((read = fis.read())!=-1){
            stringbuffer.append((char)read);
        }
        return stringbuffer.toString();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    finally {
        if (fis!=null){
            try {
                fis.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
    return null;
}   



@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.second, menu);
    return true;
}

}

StackTrace是

04-26 00:59:00.545: E/AndroidRuntime(2023): FATAL EXCEPTION: main
04-26 00:59:00.545: E/AndroidRuntime(2023): java.lang.IllegalStateException: Could not execute      method of the activity
04-26 00:59:00.545: E/AndroidRuntime(2023):     at    android.view.View$1.onClick(View.java:3633)
04-26 00:59:00.545: E/AndroidRuntime(2023):     at android.view.View.performClick(View.java:4240)
04-26 00:59:00.545: E/AndroidRuntime(2023):     at android.view.View$PerformClick.run(View.java:17721)
04-26 00:59:00.545: E/AndroidRuntime(2023):     at android.os.Handler.handleCallback(Handler.java:730)
04-26 00:59:00.545: E/AndroidRuntime(2023):     at android.os.Handler.dispatchMessage(Handler.java:92)
04-26 00:59:00.545: E/AndroidRuntime(2023):     at android.os.Looper.loop(Looper.java:137)
04-26 00:59:00.545: E/AndroidRuntime(2023):     at android.app.ActivityThread.main(ActivityThread.java:5103)
04-26 00:59:00.545: E/AndroidRuntime(2023):     at java.lang.reflect.Method.invokeNative(Native Method)
04-26 00:59:00.545: E/AndroidRuntime(2023):     at java.lang.reflect.Method.invoke(Method.java:525)
04-26 00:59:00.545: E/AndroidRuntime(2023):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
04-26 00:59:00.545: E/AndroidRuntime(2023):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
04-26 00:59:00.545: E/AndroidRuntime(2023):     at dalvik.system.NativeStart.main(Native Method)
04-26 00:59:00.545: E/AndroidRuntime(2023): Caused by: java.lang.reflect.InvocationTargetException
04-26 00:59:00.545: E/AndroidRuntime(2023):     at java.lang.reflect.Method.invokeNative(Native Method)
04-26 00:59:00.545: E/AndroidRuntime(2023):     at     java.lang.reflect.Method.invoke(Method.java:525)
04-26 00:59:00.545: E/AndroidRuntime(2023):     at android.view.View$1.onClick(View.java:3628)
04-26 00:59:00.545: E/AndroidRuntime(2023):     ... 11 more
04-26 00:59:00.545: E/AndroidRuntime(2023): Caused by: java.lang.NullPointerException
04-26 00:59:00.545: E/AndroidRuntime(2023):     at     com.example.storageexternal.SecondActivity.readdata(SecondActivity.java:62)
04-26 00:59:00.545: E/AndroidRuntime(2023):     at com.example.storageexternal.SecondActivity.apurv1(SecondActivity.java:36)
04-26 00:59:00.545: E/AndroidRuntime(2023):     ... 14 more

1 个答案:

答案 0 :(得分:0)

假设activity_second是你的xml文件并且你的按钮有一个属性android:onClick="readdate",你的readdate方法应该返回void并且参数为View,例如

public void readdate (View v) {
    //add code
}

或通过OnClickListener

查看此帖子了解更多详情How exactly does the android:onClick XML attribute differ from setOnClickListener?