Timmo
Timmo

Reputation: 2334

NullPointerException: SetOnClickListener(this) - Android

When running my application in Android Studio I get a NullPointerException at line 30 which is this:

llTheory.setOnClickListener(this);

which has always worked for me.. I am implementing View.OnClickListener and have the override method here:

@Override
public void onClick(View v) {
    ....
}

My LinearLayout is declared before the setOnClickListener here:

    LinearLayout llTheory = (LinearLayout) findViewById(R.id.llTheory);

so what is going wrong? Here is my LogCat output:

10-20 21:09:24.045  14625-14625/com.timmo.applauncher E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.timmo.applauncher, PID: 14625
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.timmo.applauncher/com.timmo.timmoapplauncher.Main}: java.lang.NullPointerException
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2224)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2283)
            at android.app.ActivityThread.access$800(ActivityThread.java:144)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1205)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5153)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:796)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:612)
            at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:132)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.NullPointerException
            at com.timmo.timmoapplauncher.Main.Init(Main.java:30)
            at com.timmo.timmoapplauncher.Main.onCreate(Main.java:78)
            at android.app.Activity.performCreate(Activity.java:5312)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2181)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2283)
            at android.app.ActivityThread.access$800(ActivityThread.java:144)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1205)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5153)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:796)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:612)
            at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:132)
            at dalvik.system.NativeStart.main(Native Method)

Upvotes: 1

Views: 962

Answers (3)

SeahawksRdaBest
SeahawksRdaBest

Reputation: 886

Fragments work a little bit differently.

You have to use the concept of rootview when dealing with them. Otherwise you will find yourself always coming across the dreaded "NULL POINTER" exception.

Here is how to go about it.

Define the rootview when you inflate each view for each fragment, then call the onClick functionality.

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
    View rootview = inflater.inflate(R.layout.xml_you_want_to_inflate, container,false);        
    // Now call rootview and the onclick fuction, presumably for a list or button
    rootview.findViewById(R.id.your_button/list_id_here).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
        // Do stuff.
        }
    });

Another alternative is using getView which always returns the rootview. Try this instead

LinearLayout llTheory = (LinearLayout)  getView().findViewById(R.id.llTheory);

This link explains a similar problem.

The former way however is the correct way as you isolate your fragments from each other and understand the view hierarchy correctly. I would read this. Trust me the time investment reading that stuff is worthwhile then the hours you'll spend otherwise with half-wit hack job ideas to make stuff work.

Upvotes: 1

Bartosz Bilicki
Bartosz Bilicki

Reputation: 13265

Do you have

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

in your activity? Your activity (or fragment hosted by activity) must have layout with R.id.llTheory.

Upvotes: 0

PLNech
PLNech

Reputation: 3177

findViewById returns a null value when the requested view cannot be found.

  • Check in your corresponding xml that a view is indeed declared with the id @id/llTheory
  • A standard Java best practice would be checking a casted item before using it.

Upvotes: 0

Related Questions