NullPointerException: SetOnClickListener(this) - Android - java

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)

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.

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.

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.

Related

Trying to get Android_ID bu the App crashes when i start it

That's the Code to get the AndroidID:
String userID = Settings.Secure.getString(getApplicationContext()
.getContentResolver(), Settings.Secure.ANDROID_ID);
When i start the Application i get the following logcat review:
04-28 01:30:02.012 9328-9328/com.hubermoritz.beercounter.beercounter02 E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.hubermoritz.beercounter.beercounter02, PID: 9328
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.hubermoritz.beercounter.beercounter02/com.hubermoritz.beercounter.beercounter02.MainActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2305)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2470)
at android.app.ActivityThread.access$900(ActivityThread.java:174)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1307)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:146)
at android.app.ActivityThread.main(ActivityThread.java:5593)
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:1283)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at android.content.ContextWrapper.getApplicationContext(ContextWrapper.java:114)
at com.hubermoritz.beercounter.beercounter02.MainActivity.<init>(MainActivity.java:48)
at java.lang.Class.newInstanceImpl(Native Method)
at java.lang.Class.newInstance(Class.java:1208)
at android.app.Instrumentation.newActivity(Instrumentation.java:1067)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2296)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2470) 
at android.app.ActivityThread.access$900(ActivityThread.java:174) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1307) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:146) 
at android.app.ActivityThread.main(ActivityThread.java:5593) 
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:1283) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099) 
at dalvik.system.NativeStart.main(Native Method) 
04
Tried with:
public String userID = Settings.Secure.getString(getContentResolver(), Settings.Secure.ANDROID_ID);
also, didn't work either. By the way I'm well aware off that it's not a perfect way to recognize users, because the number can change due to factory reset or changing phone and few other ways.
Caused by: java.lang.NullPointerException
at android.content.ContextWrapper.getApplicationContext(ContextWrapper.java:114)
at com.hubermoritz.beercounter.beercounter02.MainActivity.<init>(MainActivity.java:48)
You are attempting to call a method that you are inheriting from Activity from a field initializer. Do not do this. In general, do not call inherited methods on Activity until after super.onCreate() has been called in your onCreate() method. You are welcome to have a field, but do not initialize it until after super.onCreate().
You can't use getContentResolver until after the constructor is completed. Set this variable in your onCreate instead and you should be ok

Android testfairy java.lang.VerifyError?

After I upload my apk to testfairy and install it I get a java.lang.VerifyError crash
The class extends android.support.v4.app.Fragment
This is the function that is crashing. It crashes on the function execution, not the click but when I remove the onClick. Inner stuff all works.
fitroom_lly is LinearLayout
mixpanel defined :
MixpanelAPI mixpanel = MixpanelAPI.getInstance(getActivity(), getResources().getString(R.string.Mix_Panel_Token));
Function:
private void AddOnFittingRoomClickListener() {
fitroom_lly.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
mixpanel.track("Fitting room");
}
});
}
Error:
java.lang.VerifyError: co/slider/fashion/Slider/SliderFragment$6
at co.slider.fashion.Slider.SliderFragment.AddOnFittingRoomClickListener(SliderFragment.java:334)
at co.slider.fashion.Slider.SliderFragment.onCreateView(SliderFragment.java:135)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:1962)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1067)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1248)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:738)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1613)
at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:517)
at android.os.Handler.handleCallback(Handler.java:808)
at android.os.Handler.dispatchMessage(Handler.java:103)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:5292)
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:824)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:640)
at dalvik.system.NativeStart.main(Native Method)
I work for TestFairy. I need a bit information, is your app multidex'd or a single dex? That's the only VerifyError we ever encountered, and we have an answer for that. Though I would need you to email support at testfairy.com, and one of the technical developer will explain to you the workaround. We're making this solution available to everybody, but in the meanwhile, only support is explaining this.

java.lang.IllegalStateException: Fragment does not have a view

I have an activity which displays a DialogFragment, inside that dialog fragment I have a linearlayout which contains one button, and a fragmentLAyout where I want to display the info.
I´d have to display one list of elements, and if any of them are pressed I´ll have to display the details for them. I thought the best way to do that is with fragments, when trying to call
FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
transaction.add(R.id.framePlaceHolder, listAccountFragment).commit();
after creating my listAccountFragment, I´m getting this error
10-01 10:53:34.004 12836-12836/com.bbva.compassBuzz.qa.debug E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.bbva.compassBuzz.qa.debug, PID: 12836
java.lang.IllegalStateException: Fragment does not have a view
at android.support.v4.app.Fragment$1.findViewById(Fragment.java:1746)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:943)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1136)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:739)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1499)
at android.support.v4.app.Fragment.performResume(Fragment.java:1836)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:993)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1136)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:739)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1499)
at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:456)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5253)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1384)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1179)

NullPointerException in logcat with no clues

I am violating best practices here trying to figure this out, so to start off with, I realize that and I appreciate any insight. I'm making something simple that relies on a lot of Dialog calls. Nothing fancy, not calling a lot of Fragments. My game will require no permissions.
With the backstory out of the way, I'm getting this NPE:
06-13 01:29:03.581 24690-24690/com.myapp.myapp E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.myapp.myapp, PID: 24690
java.lang.NullPointerException
at android.widget.FrameLayout.onMeasure(FrameLayout.java:309)
at android.view.View.measure(View.java:17388)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5353)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1410)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
at android.view.View.measure(View.java:17388)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5353)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2533)
at android.view.View.measure(View.java:17388)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2217)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1354)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1553)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1238)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6473)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:803)
at android.view.Choreographer.doCallbacks(Choreographer.java:603)
at android.view.Choreographer.doFrame(Choreographer.java:573)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:789)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:157)
at android.app.ActivityThread.main(ActivityThread.java:5356)
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:1265)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
at dalvik.system.NativeStart.main(Native Method)
In my very flat app, I do this:
public static MainActivity ACTIVITY;
#Override
protected void onCreate(Bundle savedInstanceState) {
ACTIVITY = this;
super.onCreate(savedInstanceState);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
ACTIVITY.setContentView(R.layout.activity_main);
Then I do a bunch of stuff that works fine. The problem is, I call a class that does a battle event:
FightEvent thisFight = new FightEvent(this);
ALLOWSAVE = false;
thisFight.doFightEvent();
In that class, when the fight starts, I do:
ACTIVITY.setContentView(R.layout.activity_fight);
When I exit the fight, I do:
ACTIVITY.setContentView(R.layout.activity_main);
PLOT TWIST: This all works fine in older versions. I'm trying to make my game 100% compatible back to API 8.
Works great in API 8. API 19-22, not so much.
Help?

Static dialog with TextView

what`s wrong with this because I cant compile to apk. Error is Cannot use this in a static context but if I change this to context app freeze when dialog must appear.
public static void mydialog(final Context context) {
AlertDialog.Builder builder = new AlertDialog.Builder(context);
TextView myMsg = new TextView(this);
myMsg.setText("Central");
}
Logcat error:
android.view.WindowLeaked: Activity com.test.testapp.activities.MenuActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{430fc9a8 V.E..... R.....I. 0,0-1048,466} that was originally added here
at android.view.ViewRootImpl.<init>(ViewRootImpl.java:457)
at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:267)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
at android.app.Dialog.show(Dialog.java:288)
at com.dialog.dialog.showDialog(RateThisApp.java:229)
at com.dialog.dialog.showDialogIfNeeded(RateThisApp.java:108)
at com.test.testapp.activities.MenuActivity.onCreate(MenuActivity.java:47)
at android.app.Activity.performCreate(Activity.java:5426)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2269)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2363)
at android.app.ActivityThread.access$900(ActivityThread.java:161)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1265)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:157)
at android.app.ActivityThread.main(ActivityThread.java:5356)
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:1265)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
at dalvik.system.NativeStart.main(Native Method)
A static method is not associated with any particular instance of a class. It is a method associated with the class itself. Thus this has no meaning in a static method- there is no instance of the class to reference.
If this method is in a class that is a Context (e.g. an Application subclass or an Activity), then you can remove the static modifier and use this like you are already trying to do.

Categories