单击其他选项卡上的其他按钮时,允许按钮可单击

时间:2013-07-25 04:09:13

标签: java android

在我的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);

此代码的结果导致app force关闭,logcat没有显示任何内容。 任何帮助将不胜感激。感谢

ATTEMPT: 根据维克拉姆的说法,点击“清除”按钮后,应用程序强制关闭 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,app力关闭:

**

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

**

2 个答案:

答案 0 :(得分:1)

在课程开头声明你的按钮(在任何特定方法之外):

Button btn;

onCreate(Bundle)中初始化您的按钮:

btn = (Button) findViewById(R.id.Save);

// Diable the button
btn.setEnabled(false);

将您的Clear(View)更改为:

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

                    btn.setEnabled(true);
                    TextView myTextView = (TextView) findViewById(R.id.TextView1);
                    myTextView.setText("");
        }

}

答案 1 :(得分:0)

由于NullPointerException,你的应用程序正在强行关闭。

07-25 13:15:08.220: E/AndroidRuntime(4739): at com.example.androidtablayout.HistoryActivity.Clear(HistoryActivity.java:172)

查看你的logcat ......

解决方案是更改以下两行......

Button btn= (Button)findViewById(R.id.Save); btn.setEnabled(true);
TextView myTextView = (TextView) findViewById(R.id.TextView1);

为...

Button btn= (Button) view.findViewById(R.id.Save); btn.setEnabled(true);
TextView myTextView = (TextView) view.findViewById(R.id.TextView1);

根据@ vikram的建议,将声明移到Clear方法之外。将它们声明为全局变量,以便可以在类中的任何位置使用它们。