Ohad
Ohad

Reputation: 1631

"the application has stopped unexpectedly please try again"

I am facing this problem. when I want to run a simple application on the simulator I am getting this message the application has stopped unexpectedly please try again emulator android.

I have already tried:

  1. delete all my AVD and create a new one.

  2. click the back button and run it from eclipse.

  3. I have checked my manifest file and it seems all fine.

could anyone give me any solution ? I have attached the following sources:

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

Upvotes: 0

Views: 354

Answers (1)

gerbit
gerbit

Reputation: 962

As you see in exception msg, your file is not exists. You should handle it in right way, check that file exists and after that create it or open depending on file existance. In loadSaveFile() after openFileInput fis still null

Upvotes: 3

Related Questions