I am trying to retrieve data by clicking on a button in my activity. I have declared an onclick in activity_second.Here is my code.I am getting am IllegalstateException: method cannot be executed in activity.what is wrong with my code.Why the logcat is showing InvocationTargetException.what exactly this error is and why NullPointerException is there??
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 is
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