在我的 android 应用程序中,我有两个选项卡。第一个选项卡包含一个名为“保存”的按钮,我已将其设置为btn.setEnable(false)
.
在第二个选项卡上,我有一个“清除”按钮。
所以我打算做的是当我点击“清除”按钮时,我希望“保存”按钮可以点击。
下面的代码是在单击“清除”按钮时显示。
public void Clear(View view) {
StringBuilder text = new StringBuilder();
try {
InputStream instream = openFileInput("myfile.txt");
if (instream != null) {
InputStreamReader inputreader = new InputStreamReader(instream);
BufferedReader buffreader = new BufferedReader(inputreader);
File dir = getFilesDir();
File file = new File(dir, "myfile.txt");
boolean delete = file.delete();
}
}
catch (IOException e) {
e.printStackTrace();
}
Button btn=(Button)findViewById(R.id.Save); btn.setEnabled(true);
TextView myTextView = (TextView) findViewById(R.id.TextView1);
myTextView.setText("");
}
}
忽略我的 TODO 代码...我很困惑我应该把我的
Button btn=(Button)findViewById(R.id.Save);
btn.setEnabled(true);
此代码的结果导致应用程序强制关闭,logcat 什么也不显示。任何帮助将不胜感激。谢谢
尝试: 根据维克拉姆的说法,在我点击“清除”按钮后,应用程序强制关闭 LogCat:
07-25 13:15:02.430: W/System.err(4739): java.io.FileNotFoundException: /data/data/com.example.androidtablayout/files/myfile.txt: open failed: ENOENT (No such file or directory)
07-25 13:15:02.445: W/System.err(4739): at libcore.io.IoBridge.open(IoBridge.java:406)
07-25 13:15:02.445: W/System.err(4739): at java.io.FileInputStream.<init>(FileInputStream.java:78)
07-25 13:15:02.445: W/System.err(4739): at android.app.ContextImpl.openFileInput(ContextImpl.java:673)
07-25 13:15:02.445: W/System.err(4739): at android.content.ContextWrapper.openFileInput(ContextWrapper.java:159)
07-25 13:15:02.445: W/System.err(4739): at com.example.androidtablayout.HistoryActivity.ViewText(HistoryActivity.java:114)
07-25 13:15:02.445: W/System.err(4739): at java.lang.reflect.Method.invokeNative(Native Method)
07-25 13:15:02.445: W/System.err(4739): at java.lang.reflect.Method.invoke(Method.java:511)
07-25 13:15:02.445: W/System.err(4739): at android.view.View$1.onClick(View.java:3064)
07-25 13:15:02.445: W/System.err(4739): at android.view.View.performClick(View.java:3591)
07-25 13:15:02.445: W/System.err(4739): at android.view.View$PerformClick.run(View.java:14263)
07-25 13:15:02.445: W/System.err(4739): at android.os.Handler.handleCallback(Handler.java:605)
07-25 13:15:02.445: W/System.err(4739): at android.os.Handler.dispatchMessage(Handler.java:92)
07-25 13:15:02.445: W/System.err(4739): at android.os.Looper.loop(Looper.java:137)
07-25 13:15:02.445: W/System.err(4739): at android.app.ActivityThread.main(ActivityThread.java:4507)
07-25 13:15:02.450: W/System.err(4739): at java.lang.reflect.Method.invokeNative(Native Method)
07-25 13:15:02.450: W/System.err(4739): at java.lang.reflect.Method.invoke(Method.java:511)
07-25 13:15:02.455: W/System.err(4739): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
07-25 13:15:02.455: W/System.err(4739): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
07-25 13:15:02.455: W/System.err(4739): at dalvik.system.NativeStart.main(Native Method)
07-25 13:15:02.455: W/System.err(4739): Caused by: libcore.io.ErrnoException: open failed: ENOENT (No such file or directory)
07-25 13:15:02.460: D/dalvikvm(4739): GC_CONCURRENT freed 112K, 3% free 9418K/9671K, paused 2ms+2ms
07-25 13:15:02.460: W/System.err(4739): at libcore.io.Posix.open(Native Method)
07-25 13:15:02.460: W/System.err(4739): at libcore.io.BlockGuardOs.open(BlockGuardOs.java:110)
07-25 13:15:02.460: W/System.err(4739): at libcore.io.IoBridge.open(IoBridge.java:390)
07-25 13:15:02.460: W/System.err(4739): ... 18 more
07-25 13:15:03.730: D/Network(4739): Network
07-25 13:15:03.940: D/Network(4739): Network
07-25 13:15:04.140: D/Network(4739): Network
07-25 13:15:04.335: D/Network(4739): Network
07-25 13:15:08.215: D/AndroidRuntime(4739): Shutting down VM
07-25 13:15:08.215: W/dalvikvm(4739): threadid=1: thread exiting with uncaught exception (group=0x40c3e1f8)
07-25 13:15:08.220: E/AndroidRuntime(4739): FATAL EXCEPTION: main
07-25 13:15:08.220: E/AndroidRuntime(4739): java.lang.IllegalStateException: Could not execute method of the activity
07-25 13:15:08.220: E/AndroidRuntime(4739): at android.view.View$1.onClick(View.java:3069)
07-25 13:15:08.220: E/AndroidRuntime(4739): at android.view.View.performClick(View.java:3591)
07-25 13:15:08.220: E/AndroidRuntime(4739): at android.view.View$PerformClick.run(View.java:14263)
07-25 13:15:08.220: E/AndroidRuntime(4739): at android.os.Handler.handleCallback(Handler.java:605)
07-25 13:15:08.220: E/AndroidRuntime(4739): at android.os.Handler.dispatchMessage(Handler.java:92)
07-25 13:15:08.220: E/AndroidRuntime(4739): at android.os.Looper.loop(Looper.java:137)
07-25 13:15:08.220: E/AndroidRuntime(4739): at android.app.ActivityThread.main(ActivityThread.java:4507)
07-25 13:15:08.220: E/AndroidRuntime(4739): at java.lang.reflect.Method.invokeNative(Native Method)
07-25 13:15:08.220: E/AndroidRuntime(4739): at java.lang.reflect.Method.invoke(Method.java:511)
07-25 13:15:08.220: E/AndroidRuntime(4739): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
07-25 13:15:08.220: E/AndroidRuntime(4739): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
07-25 13:15:08.220: E/AndroidRuntime(4739): at dalvik.system.NativeStart.main(Native Method)
07-25 13:15:08.220: E/AndroidRuntime(4739): Caused by: java.lang.reflect.InvocationTargetException
07-25 13:15:08.220: E/AndroidRuntime(4739): at java.lang.reflect.Method.invokeNative(Native Method)
07-25 13:15:08.220: E/AndroidRuntime(4739): at java.lang.reflect.Method.invoke(Method.java:511)
07-25 13:15:08.220: E/AndroidRuntime(4739): at android.view.View$1.onClick(View.java:3064)
07-25 13:15:08.220: E/AndroidRuntime(4739): ... 11 more
07-25 13:15:08.220: E/AndroidRuntime(4739): Caused by: java.lang.NullPointerException
07-25 13:15:08.220: E/AndroidRuntime(4739): at com.example.androidtablayout.HistoryActivity.Clear(HistoryActivity.java:172)
07-25 13:15:08.220: E/AndroidRuntime(4739): ... 14 more
根据 Tarsem,应用程序强制关闭:
**
07-25 13:25:56.960: D/Network(6667): Network
07-25 13:25:57.000: D/dalvikvm(6667): GC_CONCURRENT freed 112K, 3% free 9417K/9671K, paused 2ms+3ms
07-25 13:25:57.155: D/Network(6667): Network
07-25 13:25:57.315: D/Network(6667): Network
07-25 13:25:57.440: D/Network(6667): Network
07-25 13:25:59.010: D/AndroidRuntime(6667): Shutting down VM
07-25 13:25:59.010: W/dalvikvm(6667): threadid=1: thread exiting with uncaught exception (group=0x40c3e1f8)
07-25 13:25:59.020: E/AndroidRuntime(6667): FATAL EXCEPTION: main
07-25 13:25:59.020: E/AndroidRuntime(6667): java.lang.IllegalStateException: Could not execute method of the activity
07-25 13:25:59.020: E/AndroidRuntime(6667): at android.view.View$1.onClick(View.java:3069)
07-25 13:25:59.020: E/AndroidRuntime(6667): at android.view.View.performClick(View.java:3591)
07-25 13:25:59.020: E/AndroidRuntime(6667): at android.view.View$PerformClick.run(View.java:14263)
07-25 13:25:59.020: E/AndroidRuntime(6667): at android.os.Handler.handleCallback(Handler.java:605)
07-25 13:25:59.020: E/AndroidRuntime(6667): at android.os.Handler.dispatchMessage(Handler.java:92)
07-25 13:25:59.020: E/AndroidRuntime(6667): at android.os.Looper.loop(Looper.java:137)
07-25 13:25:59.020: E/AndroidRuntime(6667): at android.app.ActivityThread.main(ActivityThread.java:4507)
07-25 13:25:59.020: E/AndroidRuntime(6667): at java.lang.reflect.Method.invokeNative(Native Method)
07-25 13:25:59.020: E/AndroidRuntime(6667): at java.lang.reflect.Method.invoke(Method.java:511)
07-25 13:25:59.020: E/AndroidRuntime(6667): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
07-25 13:25:59.020: E/AndroidRuntime(6667): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
07-25 13:25:59.020: E/AndroidRuntime(6667): at dalvik.system.NativeStart.main(Native Method)
07-25 13:25:59.020: E/AndroidRuntime(6667): Caused by: java.lang.reflect.InvocationTargetException
07-25 13:25:59.020: E/AndroidRuntime(6667): at java.lang.reflect.Method.invokeNative(Native Method)
07-25 13:25:59.020: E/AndroidRuntime(6667): at java.lang.reflect.Method.invoke(Method.java:511)
07-25 13:25:59.020: E/AndroidRuntime(6667): at android.view.View$1.onClick(View.java:3064)
07-25 13:25:59.020: E/AndroidRuntime(6667): ... 11 more
07-25 13:25:59.020: E/AndroidRuntime(6667): Caused by: java.lang.NullPointerException
07-25 13:25:59.020: E/AndroidRuntime(6667): at com.example.androidtablayout.HistoryActivity.Clear(HistoryActivity.java:171)
07-25 13:25:59.020: E/AndroidRuntime(6667): ... 14 more
**