Error while starting an Android application (& ldquo; unfortunately, projec…

I’m getting an error (in my android emulator) saying that “unfortunately, my_project_name has stopped..” when i try to run the app. I would appreciate some help with this.

Here is the Logcat output:

03-30 10:43:32.079: D/AndroidRuntime(1228): Shutting down VM

03-30 10:43:32.079: W/dalvikvm(1228): threadid=1: thread exiting with uncaught exception (group=0xb4a84ba8)

03-30 10:43:32.099: E/AndroidRuntime(1228): FATAL EXCEPTION: main

03-30 10:43:32.099: E/AndroidRuntime(1228): Process: com.example.testproject28_3_2014, PID: 1228

03-30 10:43:32.099: E/AndroidRuntime(1228): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.testproject28_3_2014/com.example.testproject28_3_2014.MainActivity}: java.lang.NullPointerException

03-30 10:43:32.099: E/AndroidRuntime(1228): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)

03-30 10:43:32.099: E/AndroidRuntime(1228): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)

03-30 10:43:32.099: E/AndroidRuntime(1228): at android.app.ActivityThread.access$800(ActivityThread.java:135)

03-30 10:43:32.099: E/AndroidRuntime(1228): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)

03-30 10:43:32.099: E/AndroidRuntime(1228): at android.os.Handler.dispatchMessage(Handler.java:102)

03-30 10:43:32.099: E/AndroidRuntime(1228): at android.os.Looper.loop(Looper.java:136)

03-30 10:43:32.099: E/AndroidRuntime(1228): at android.app.ActivityThread.main(ActivityThread.java:5017)

03-30 10:43:32.099: E/AndroidRuntime(1228): at java.lang.reflect.Method.invokeNative(Native Method)

03-30 10:43:32.099: E/AndroidRuntime(1228): at java.lang.reflect.Method.invoke(Method.java:515)

03-30 10:43:32.099: E/AndroidRuntime(1228): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)

03-30 10:43:32.099: E/AndroidRuntime(1228): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)

03-30 10:43:32.099: E/AndroidRuntime(1228): at dalvik.system.NativeStart.main(Native Method)

03-30 10:43:32.099: E/AndroidRuntime(1228): Caused by: java.lang.NullPointerException

03-30 10:43:32.099: E/AndroidRuntime(1228): at com.example.testproject28_3_2014.MainActivity.addSaveButtonListener(MainActivity.java:30)

03-30 10:43:32.099: E/AndroidRuntime(1228): at com.example.testproject28_3_2014.MainActivity.onCreate(MainActivity.java:21)

03-30 10:43:32.099: E/AndroidRuntime(1228): at android.app.Activity.performCreate(Activity.java:5231)

03-30 10:43:32.099: E/AndroidRuntime(1228): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)

03-30 10:43:32.099: E/AndroidRuntime(1228): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)

03-30 10:43:32.099: E/AndroidRuntime(1228): ... 11 more

Here is some xml:







Here is the first half of my mainactivity.java file(I didn’t change anything in the second half):

package com.example.testproject28_3_2014;

import android.os.Bundle;

import android.support.v4.app.Fragment;

import android.support.v7.app.ActionBarActivity;

import android.util.Log;

import android.view.LayoutInflater;

import android.view.Menu;

import android.view.MenuItem;

import android.view.View;

import android.view.ViewGroup;

import android.widget.Button;

public class MainActivity extends ActionBarActivity {

public static final String DEBUGTAG = "DLD";

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

addSaveButtonListener();

if (savedInstanceState == null) {

getSupportFragmentManager().beginTransaction()

.add(R.id.container, new PlaceholderFragment()).commit();

}

}

private void addSaveButtonListener(){

Button saveBtn = (Button)findViewById(R.id.save);

saveBtn.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

// TODO Auto-generated method stub

Log.d(DEBUGTAG, "Save button clicked");

}

});

}

I did try other similar queries on Stackoverflow, but that didn’t help. I’ve had this problem before also, with another app, thus I would like to solve this error.

Here is some extra code, to help find the solution:

public static class PlaceholderFragment extends Fragment {

    public PlaceholderFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_main, container,
                false);
        return rootView;
    }
}

}

Your save button with id save
is not in the activity layout but in a fragment layout. It isn’t in the activity view hierarchy when you call addSaveButtonListener()
and findViewById()
returns null
.

Move the save button setup to your fragment onCreateView()
, e.g.

View rootView = inflater.inflate(R.layout.fragment_main, container,
            false);

Button saveBtn = (Button)rootView.findViewById(R.id.save);
saveBtn.setOnClickListener(...);

return rootView;
Hello, buddy!稿源:Hello, buddy! (源链) | 关于 | 阅读提示

本站遵循[CC BY-NC-SA 4.0]。如您有版权、意见投诉等问题,请通过eMail联系我们处理。
酷辣虫 » 移动开发 » Error while starting an Android application (& ldquo; unfortunately, projec…

喜欢 (0)or分享给?

专业 x 专注 x 聚合 x 分享 CC BY-NC-SA 4.0

使用声明 | 英豪名录