2014-04-25 79 views
0

我试图通过点击我的活动中的按钮检索数据。我已经在activity_second中声明了一个onclick.Here是我的代码。我得到了IllegalstateException:方法不能在activity中执行。我的代码有什么错误。为什么logcat显示InvocationTargetException。这个错误究竟是什么以及为什么NullPointerException存在??非法状态异常onclick

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; 
} 

} 

堆栈跟踪被

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 
+0

Stacktrace please? –

+0

logcat发布它。 –

回答

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?

+0

<按钮 机器人:ID = “@ + ID/button1的” 机器人:layout_width = “WRAP_CONTENT” 机器人:layout_height = “WRAP_CONTENT” 机器人:layout_below = “@ + ID /状态” 机器人:的onClick =“apurv1 “ android:layout_centerHorizo​​ntal =”true“ android:text =”@ string/button1“/> – user3559063

+0

为什么logcat显示InvocationTargetException。这个错误究竟是什么? – user3559063