“应用程序意外停止请再试一次”

时间:2014-04-26 18:39:01

标签: android

我正面临这个问题。当我想在模拟器上运行一个简单的应用程序时,我收到此消息the application has stopped unexpectedly please try again emulator android

我已经尝试过了:

  
      
  1. 删除我的所有AVD并创建一个新的AVD。

  2.   
  3. 单击后退按钮并从eclipse运行。

  4.   
  5. 我检查了我的清单文件,似乎一切正常。

  6.   

任何人都可以给我任何解决方案吗? 我附上了以下来源:

MainActivity.java

package com.example.notesquirrel;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;


public class MainActivity extends Activity {
public  static final String DEBUGTAG = "SS";
public  static final String TEXTFILE= "NoteSquirrel.txt";
@Override
//this method is called whenever the application is started for the first time

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //display the xml activity_main
    setContentView(R.layout.activity_main);
    addSaveButtonListener();
    try {
        loadSaveFile();
    } catch (IOException e) {

        e.printStackTrace();
    }
}

private void loadSaveFile() throws IOException{
    FileInputStream fis = null;
    try {
        fis = openFileInput(TEXTFILE);
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    EditText editText = (EditText)findViewById(R.id.text);
    BufferedReader reader = new BufferedReader(new InputStreamReader(
            new DataInputStream(fis) ) );

    String line;
    while((line = reader.readLine())!=null){
        editText.append(line);
        editText.append("\n");
    }
    fis.close();
}
private void addSaveButtonListener(){
    Button saveBtn = (Button) findViewById(R.id.save);
    //onClickListener is an interface 
    saveBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        //this code will run whenever the button is clicked. 
        public void onClick(View v) {

            //we need to cast it to EditText

            EditText editText = (EditText)findViewById(R.id.text);
            // TODO Auto-generated method stub
            Log.d(DEBUGTAG, "Save Button clicked");
            //we get the text from it 
            String text = editText.getText().toString();
            try {
                FileOutputStream fos = openFileOutput(TEXTFILE, MODE_PRIVATE);
                fos.write(text.getBytes());
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            Log.d (DEBUGTAG, "Unable to save file ");

        }
    });
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

}

AndroidManifest.java          

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="18" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.example.notesquirrel.MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

logcat的:

04-26 18:41:22.968: W/System.err(363): java.io.FileNotFoundException:             /data/data/com.example.notesquirrel/files/NoteSquirrel.txt (No such file or directory)
04-26 18:41:22.968: W/System.err(363):  at      org.apache.harmony.luni.platform.OSFileSystem.open(Native Method)
04-26 18:41:22.978: W/System.err(363):  at     dalvik.system.BlockGuard$WrappedFileSystem.open(BlockGuard.java:232)
04-26 18:41:22.978: W/System.err(363):  at java.io.FileInputStream.<init>(FileInputStream.java:80)
04-26 18:41:22.978: W/System.err(363):  at android.app.ContextImpl.openFileInput(ContextImpl.java:404)
04-26 18:41:22.978: W/System.err(363):  at   android.content.ContextWrapper.openFileInput(ContextWrapper.java:152)
04-26 18:41:22.978: W/System.err(363):  at com.example.notesquirrel.MainActivity.loadSaveFile(MainActivity.java:43)
04-26 18:41:22.978: W/System.err(363):  at com.example.notesquirrel.MainActivity.onCreate(MainActivity.java:33)
04-26 18:41:22.978: W/System.err(363):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
04-26 18:41:22.978: W/System.err(363):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
04-26 18:41:22.978: W/System.err(363):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
04-26 18:41:22.988: W/System.err(363):  at android.app.ActivityThread.access$1500(ActivityThread.java:117)
04-26 18:41:22.988: W/System.err(363):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
04-26 18:41:22.988: W/System.err(363):  at android.os.Handler.dispatchMessage(Handler.java:99)
04-26 18:41:22.988: W/System.err(363):  at android.os.Looper.loop(Looper.java:123)
04-26 18:41:22.988: W/System.err(363):  at android.app.ActivityThread.main(ActivityThread.java:3683)
04-26 18:41:22.988: W/System.err(363):  at java.lang.reflect.Method.invokeNative(Native Method)
04-26 18:41:22.988: W/System.err(363):  at java.lang.reflect.Method.invoke(Method.java:507)
04-26 18:41:22.988: W/System.err(363):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
04-26 18:41:22.998: W/System.err(363):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
04-26 18:41:22.998: W/System.err(363):  at dalvik.system.NativeStart.main(Native Method)
04-26 18:41:22.998: D/AndroidRuntime(363): Shutting down VM
04-26 18:41:22.998: W/dalvikvm(363): threadid=1: thread exiting with uncaught exception (group=0x40015560)
04-26 18:41:23.008: E/AndroidRuntime(363): FATAL EXCEPTION: main
04-26 18:41:23.008: E/AndroidRuntime(363): java.lang.RuntimeException: Unable to start  activity ComponentInfo{com.example.notesquirrel/com.example.notesquirrel.MainActivity}:  java.lang.NullPointerException
04-26 18:41:23.008: E/AndroidRuntime(363):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
04-26 18:41:23.008: E/AndroidRuntime(363):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
04-26 18:41:23.008: E/AndroidRuntime(363):  at android.app.ActivityThread.access$1500(ActivityThread.java:117)
04-26 18:41:23.008: E/AndroidRuntime(363):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
04-26 18:41:23.008: E/AndroidRuntime(363):  at android.os.Handler.dispatchMessage(Handler.java:99)
04-26 18:41:23.008: E/AndroidRuntime(363):  at android.os.Looper.loop(Looper.java:123)
04-26 18:41:23.008: E/AndroidRuntime(363):  at android.app.ActivityThread.main(ActivityThread.java:3683)
04-26 18:41:23.008: E/AndroidRuntime(363):  at java.lang.reflect.Method.invokeNative(Native Method)
04-26 18:41:23.008: E/AndroidRuntime(363):  at  java.lang.reflect.Method.invoke(Method.java:507)
04-26 18:41:23.008: E/AndroidRuntime(363):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
04-26 18:41:23.008: E/AndroidRuntime(363):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
04-26 18:41:23.008: E/AndroidRuntime(363):  at dalvik.system.NativeStart.main(Native Method)
04-26 18:41:23.008: E/AndroidRuntime(363): Caused by: java.lang.NullPointerException
04-26 18:41:23.008: E/AndroidRuntime(363):  at java.io.FilterInputStream.available(FilterInputStream.java:54)
04-26 18:41:23.008: E/AndroidRuntime(363):  at java.io.InputStreamReader.read(InputStreamReader.java:245)
04-26 18:41:23.008: E/AndroidRuntime(363):  at java.io.BufferedReader.fillBuf(BufferedReader.java:128)
04-26 18:41:23.008: E/AndroidRuntime(363):  at java.io.BufferedReader.readLine(BufferedReader.java:357)
04-26 18:41:23.008: E/AndroidRuntime(363):  at com.example.notesquirrel.MainActivity.loadSaveFile(MainActivity.java:54)
04-26 18:41:23.008: E/AndroidRuntime(363):  at com.example.notesquirrel.MainActivity.onCreate(MainActivity.java:33)
04-26 18:41:23.008: E/AndroidRuntime(363):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
04-26 18:41:23.008: E/AndroidRuntime(363):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
04-26 18:41:23.008: E/AndroidRuntime(363):  ... 11 more

1 个答案:

答案 0 :(得分:3)

正如您在异常消息中看到的那样,您的文件不存在。您应该以正确的方式处理它,检查该文件是否存在,之后创建它或打开,具体取决于文件存在。在loadSaveFile() openFileInput fis

后的null