Question

Being new to android development I'm expecting few hiccups there and now, but this one got me scratching my head.

I started out by creating few buttons in fragment_main.xml file (I'm working in Android Studio and using Genymotion as my Emulator)

The reason why I mentioned this is because I use activity_main.xml as a view in my MainActivity java file (I'm still a little bit confused with these two files, but from what I've heard activity_main.xml uses layout in fragment_main.xml file?)

Anyways, Application runs as expected, but gives "Unfortunately [Application Name] has stopped" error once I add onClick Listener to getAboutButton

My MainActivity Looks like this at the moment (including imports as well just in case):

package com.example.braintraininggame.app;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBarActivity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

public class MainActivity extends ActionBarActivity {

    /*
        Declare variables.
    */
    Button getAboutButton;

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

        if (savedInstanceState == null) {
            getSupportFragmentManager().beginTransaction()
                    .add(R.id.container, new PlaceholderFragment())
                    .commit();
        }

        /*
            Assign views to variables.
         */
        getAboutButton = (Button) findViewById(R.id.buttonAbout);

        /*
            Create Listeners.
         */
        getAboutButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {

            }
        });


    }

    /*
        Methods
     */



    @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;
    }

    /**
     * A placeholder fragment containing a simple view.
     */
    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;
        }
    }

}

I assume this is stacktrace:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.braintraininggame.app/com.example.braintraininggame.app.MainActivity}: java.lang.NullPointerException
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
            at android.app.ActivityThread.access$600(ActivityThread.java:141)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:5041)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
            at dalvik.system.NativeStart.main(Native Method)

Android studio says that it is caused by line 38, in this case line that follows right after "Create Listener" comment.

Était-ce utile?

La solution

Your about button doesn't exist yet since your Fragment has not inflated its view. Calling commit() on a fragment transaction does not mean your fragment is ready to go. It just means you are scheduling the commit transaction the next time the UI thread is available. Your calls to findViewById will return null. You should put code that accesses or modifies a fragments UI within the fragment itself. By putting it in the activity you are eliminating the point of fragments.

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

    Button getAboutButton = (Button) rootView.findViewById(R.id.buttonAbout);

    getAboutButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {

        }
    });

    return rootView;
}

Autres conseils

Add @override.

Change this code:

getAboutButton.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {

    }
});

To this:

        getAboutButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top