Frage

I am trying to make a little program which allows user to add items into a shopping list. The problem is that the program starts and closes immediately after a short while with the message: "Unfortunately, shopping has stopped" Shopping is the name of the app. What can be the reason? Here are all errors from LogCat in Eclipse:

AndroidRuntime(800): Shutting down VM

W/dalvikvm(800): threadid=1: thread exiting with uncaught exception (group=0x41465700)

E/AndroidRuntime(800): FATAL EXCEPTION: main

E/AndroidRuntime(800): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.shopping/com.example.shopping.MainActivity}: java.lang.NullPointerException

E/AndroidRuntime(800):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)

E/AndroidRuntime(800):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)

E/AndroidRuntime(800):  at android.app.ActivityThread.access$600(ActivityThread.java:141)

E/AndroidRuntime(800):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)

E/AndroidRuntime(800):  at android.os.Handler.dispatchMessage(Handler.java:99)

E/AndroidRuntime(800):  at android.os.Looper.loop(Looper.java:137)

E/AndroidRuntime(800):  at android.app.ActivityThread.main(ActivityThread.java:5103)

E/AndroidRuntime(800):  at java.lang.reflect.Method.invokeNative(Native Method)

E/AndroidRuntime(800):  at java.lang.reflect.Method.invoke(Method.java:525)

E/AndroidRuntime(800):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)

E/AndroidRuntime(800):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)

E/AndroidRuntime(800):  at dalvik.system.NativeStart.main(Native Method)

E/AndroidRuntime(800): Caused by: java.lang.NullPointerException

E/AndroidRuntime(800):  at com.example.shopping.ShoppingFragment.onCreateView(ShoppingFragment.java:31)

E/AndroidRuntime(800):  at android.support.v4.app.Fragment.performCreateView(Fragment.java:1500)

E/AndroidRuntime(800):  at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:927)

E/AndroidRuntime(800):  at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)

E/AndroidRuntime(800):  at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)

E/AndroidRuntime(800):  at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1467)

E/AndroidRuntime(800):  at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:570)

E/AndroidRuntime(800):  at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1171)

E/AndroidRuntime(800):  at android.app.Activity.performStart(Activity.java:5143)

E/AndroidRuntime(800):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184)

E/AndroidRuntime(800):  ... 11 more

I/Process(800): Sending signal. PID: 800 SIG: 9

And here is my code but I can't see any mistakes here:

MainActivity file:

package com.example.shopping;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;

public class MainActivity extends FragmentActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        FragmentManager fragManager = getSupportFragmentManager();

        Fragment theFragment = fragManager.findFragmentById(R.id.FragmentContainer);

        if (theFragment == null) {
            theFragment = new ShoppingFragment();

            fragManager.beginTransaction().add(R.id.FragmentContainer, theFragment).commit();
        }   
    }
}

ShoppingFragment file:

package com.example.shopping;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;

public class ShoppingFragment extends Fragment {

    private Shopping shopping;
    private EditText itemNameEditText;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        shopping = new Shopping();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View theView = inflater.inflate(R.layout.fragment_list, container, false);
        itemNameEditText = (EditText) theView.findViewById(R.id.itemNameEditText);
        itemNameEditText.addTextChangedListener(new TextWatcher() {

            @Override
            public void afterTextChanged(Editable s) {
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                    int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before,
                    int count) {

                shopping.setName(s.toString()); 
            }
        });

        return theView;
    }
}

I have really no idea why it doesn't want to work so thank you for any suggestions.

War es hilfreich?

Lösung

Let me explain again.

When you use setContentView(R.layout.activity_main ), the layout R.layout.activity_main will be inflated and associated to your activity. If you want to retrieve a View from that layout, you just call findViewById(R.id.MyId);.

But if your View is not inside the layout you passed to setContentView(), you will have a NullPointerException.

Besides, if you inflate a layout using an inflater then call inflatedView.findViewById(R.id.MyId)

Hope you will understand now.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top