java - 单击按钮时应用程序崩溃

标签 java android crash fileoutputstream

因此,我的应用程序从 TextView 读取数据,然后将其保存到文件中。无论如何,当我按下提交按钮时,它崩溃了。我不知道为什么,我尝试删除粗略的代码,但这也不起作用。

无论如何,如果你能看一下我的代码并看看我做错了什么,那就太好了;另外,如果有人知道如何复制 LogCat,那就太酷了。

我的代码:

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.login);
    send = (Button) findViewById(R.id.bLogIn);
    user = (EditText) findViewById(R.id.eTuser);
    pass = (EditText) findViewById(R.id.eTpassword);
    staySignedIn = (CheckBox) findViewById(R.id.Cbstay);
    send.setOnClickListener(this);
    try {
        Fos = openFileOutput(FILENAME, Context.MODE_PRIVATE);
        Fos.close();
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    if (staySignedIn.isChecked()) {

        String u = user.getText().toString();
        String p = pass.getText().toString();
        File f = new File(FILENAME);
        try {
            Fos = new FileOutputStream(f);
            //Write some Data
            Fos.close();
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } 


    } 

}

public void onClick(View v) {
    // TODO Auto-generated method stub
    switch (v.getId()) {
    case R.id.bLogIn:
        if (pass.length() == 0)
            Toast.makeText(this,
                    "Try to type in your username and password again!",
                    Toast.LENGTH_LONG).show();
        else if (user.length() == 0)
            Toast.makeText(this,
                    "Try to type in your username and password again!",
                    Toast.LENGTH_LONG).show();
        else {

            String u = user.getText().toString();
            String p = pass.getText().toString();
            Bundle send = new Bundle();
            send.putString("key", u);
            send.putString("key", p);
            Intent a = new Intent(LogIn.this, logincheck.class);
            startActivity(a);
            Toast.makeText(this, "Were signing you in!", Toast.LENGTH_LONG)
                    .show();
            break;
        }
    }

}

}

Logcat:

 01-19 07:02:42.341: W/System.err(3074): java.io.FileNotFoundException: /userandpass (Read-only file system)
    01-19 07:02:42.341: W/System.err(3074):         at   org.apache.harmony.luni.platform.OSFileSystem.openImpl(Native Method)
    01-19 07:02:42.341: W/System.err(3074):         at   org.apache.harmony.luni.platform.OSFileSystem.open(OSFileSystem.java:152)
    01-19 07:02:42.341: W/System.err(3074):         at java.io.FileOutputStream.<init>(FileOutputStream.java:97)
    01-19 07:02:42.341: W/System.err(3074):         at java.io.FileOutputStream.<init>(FileOutputStream.java:69)
    01-19 07:02:42.341: W/System.err(3074):         at com.gta5news.bananaphone.LogIn.onCreate(LogIn.java:59)
    01-19 07:02:42.341: W/System.err(3074):         at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
01-19 07:02:42.341: W/System.err(3074):         at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
01-19 07:02:42.341: W/System.err(3074):         at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
01-19 07:02:42.341: W/System.err(3074):         at android.app.ActivityThread.access$2300(ActivityThread.java:125)
01-19 07:02:42.351: W/System.err(3074):         at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
01-19 07:02:42.351: W/System.err(3074):         at android.os.Handler.dispatchMessage(Handler.java:99)
01-19 07:02:42.351: W/System.err(3074):         at android.os.Looper.loop(Looper.java:123)
01-19 07:02:42.351: W/System.err(3074):         at android.app.ActivityThread.main(ActivityThread.java:4627)
01-19 07:02:42.351: W/System.err(3074):         at java.lang.reflect.Method.invokeNative(Native Method)
01-19 07:02:42.351: W/System.err(3074):         at java.lang.reflect.Method.invoke(Method.java:521)
01-19 07:02:42.351: W/System.err(3074):         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
01-19 07:02:42.351: W/System.err(3074):         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
01-19 07:02:42.351: W/System.err(3074):         at dalvik.system.NativeStart.main(Native Method)
01-19 07:02:47.371: W/KeyCharacterMap(3074): No keyboard for id 0
01-19 07:02:47.371: W/KeyCharacterMap(3074): Using default keymap: /system/usr/keychars/qwerty.kcm.bin
01-19 07:02:49.421: D/AndroidRuntime(3074): Shutting down VM
01-19 07:02:49.421: W/dalvikvm(3074): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
01-19 07:02:49.421: E/AndroidRuntime(3074): FATAL EXCEPTION: main
01-19 07:02:49.421: E/AndroidRuntime(3074): android.content.ActivityNotFoundException: Unable to find explicit activity class {com.gta5news.bananaphone/com.gta5news.bananaphone.logincheck}; have you declared this activity in your AndroidManifest.xml?
01-19 07:02:49.421: E/AndroidRuntime(3074):     at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1404)
01-19 07:02:49.421: E/AndroidRuntime(3074):     at android.app.Instrumentation.execStartActivity(Instrumentation.java:1378)
01-19 07:02:49.421: E/AndroidRuntime(3074):     at android.app.Activity.startActivityForResult(Activity.java:2817)
01-19 07:02:49.421: E/AndroidRuntime(3074):     at android.app.Activity.startActivity(Activity.java:2923)
01-19 07:02:49.421: E/AndroidRuntime(3074):     at com.gta5news.bananaphone.LogIn.onClick(LogIn.java:95)
01-19 07:02:49.421: E/AndroidRuntime(3074):     at android.view.View.performClick(View.java:2408)
01-19 07:02:49.421: E/AndroidRuntime(3074):     at android.view.View$PerformClick.run(View.java:8816)
01-19 07:02:49.421: E/AndroidRuntime(3074):     at android.os.Handler.handleCallback(Handler.java:587)
01-19 07:02:49.421: E/AndroidRuntime(3074):     at android.os.Handler.dispatchMessage(Handler.java:92)
01-19 07:02:49.421: E/AndroidRuntime(3074):     at android.os.Looper.loop(Looper.java:123)
01-19 07:02:49.421: E/AndroidRuntime(3074):     at android.app.ActivityThread.main(ActivityThread.java:4627)
01-19 07:02:49.421: E/AndroidRuntime(3074):     at java.lang.reflect.Method.invokeNative(Native Method)
01-19 07:02:49.421: E/AndroidRuntime(3074):     at java.lang.reflect.Method.invoke(Method.java:521)
01-19 07:02:49.421: E/AndroidRuntime(3074):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
01-19 07:02:49.421: E/AndroidRuntime(3074):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
01-19 07:02:49.421: E/AndroidRuntime(3074):     at dalvik.system.NativeStart.main(Native Method)

最佳答案

如果您查看日志,答案就在其中。检查线路

E/AndroidRuntime(3074): android.content.ActivityNotFoundException: Unable to find explicit activity class {com.gta5news.bananaphone/com.gta5news.bananaphone.logincheck}; have you declared this activity in your AndroidManifest.xml?

它表示您没有声明您尝试在 list 中单击按钮时打开的新 Activity 。将其添加到您的 list 中,它应该可以正常工作。

它正在尝试在 list 中查找您的登录检查类。

作为旁注,请尝试遵循 Java 约定。使用驼峰式大小写命名类名等。例如,您的类应命名为 LoginCheck。有关 java 约定的更多信息 here .

您的 list 应该已经有一个 Activity 标签。在其下添加您的新 Activity ,如下所示

<activity name="com.gta5news.bananaphone.logincheck" />

应该可以了。

关于java - 单击按钮时应用程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9503773/

相关文章:

php - 我可以向 Android Studio 中的现有项目添加新包吗?

C++ 动态 boolean 数组导致崩溃

android - 在 Android 4.0.3 的 Canvas 上绘制奇怪的崩溃。 A/libc : Fatal signal 11 (SIGSEGV)

java - 在Android中比较mac地址?

java - 枚举在 Java 内部是如何表示的?

android - 当键盘出现在平板电脑上时,Bootstrap 页脚无法正确显示

html - 从 XML 中的 html 格式的字符串资源设置 TextView 文本

qt - 为什么 QMap 在 findNode 函数中调用 contains() 时崩溃?

java - 流式传输 json 元素

java - 互联网上的 UDP。它是如何工作的?