NullPointerException in logcat with no clues - java

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?

Related

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.

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.

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)
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.

Xamarin Android Theme.AppCompat exception

I'm writing an app in Xamarin for Android, and all is going well, but all of a sudden, i'm getting this "java.lang.illegalStateExeption error when i run the program.
I have done nothing to the code that might have caused this, and i've even gone back to copies of the program that i've backed up, where i know there was nothing wrong, and it's still throwing the exception.
The exception is thrown on the first line of the OnCreate method, here:
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState); // error thrown here
and this is the error:
You need to use a Theme.AppCompat theme (or descendant) with this activity.
At the top of the Main activity, i DO declare an AppCompat theme, like this:
[Activity(Label = "FrogPoint", Theme = "#style/Theme.AppCompat.Light", LaunchMode = LaunchMode.SingleTask, ScreenOrientation = ScreenOrientation.Portrait)] //master copy
public class MainActivity : ActionBarActivity, IBeaconConsumer
{
this is driving me nuts, particularly as it's just started happening for no apparent reason. Every time the app runs, the error is thrown.
I have tried both with, and without, the following in the Android Manifest:
<activity android:name="MainActivity" android:theme="#style/Theme.AppCompat" android:label="#string/app_name"></activity>
It makes no difference.
Can anybody please help?
Here is the stack trace:
java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
at android.support.v7.app.ActionBarActivityDelegate.onCreate(ActionBarActivityDelegate.java:102)
at android.support.v7.app.ActionBarActivityDelegateICS.onCreate(ActionBarActivityDelegateICS.java:58)
at android.support.v7.app.ActionBarActivity.onCreate(ActionBarActivity.java:98)
at frogpoint.droid.MainActivity.n_onCreate(Native Method)
at frogpoint.droid.MainActivity.onCreate(MainActivity.java:41)
at android.app.Activity.performCreate(Activity.java:5231)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5001)
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:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
The problem was a corrupt Resource.Designer file. I deleted it from the project, then rebuilt the project and added the file back (it doesn't add it back to the project automatically). Then the problem was gone!

Why I get an exception when I try to start a new Intent?

I'm very new to Android and I get an exception like this:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.simplepay.hellomobile/com.simplepay.hellomobile.CardPayment}: android.os.NetworkOnMainThreadException
the stacktrace is here:
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:5039)
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)
Caused by: android.os.NetworkOnMainThreadException
at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117)
at libcore.io.BlockGuardOs.recvfrom(BlockGuardOs.java:163)
at libcore.io.IoBridge.recvfrom(IoBridge.java:513)
at java.net.PlainSocketImpl.read(PlainSocketImpl.java:488)
at java.net.PlainSocketImpl.access$000(PlainSocketImpl.java:46)
at java.net.PlainSocketImpl$PlainSocketInputStream.read(PlainSocketImpl.java:240)
at org.apache.http.impl.io.AbstractSessionInputBuffer.fillBuffer(AbstractSessionInputBuffer.java:103)
at org.apache.http.impl.io.SocketInputBuffer.isStale(SocketInputBuffer.java:109)
at org.apache.http.impl.AbstractHttpClientConnection.isStale(AbstractHttpClientConnection.java:205)
at org.apache.http.impl.conn.AbstractClientConnAdapter.isStale(AbstractClientConnAdapter.java:185)
at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:336)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:653)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:627)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:616)
at juspay.services.WalletService.makeServiceCall(WalletService.java:283)
at juspay.services.WalletService.getCardTokens(WalletService.java:237)
at com.simplepay.hellomobile.CardPayment.addContentTo(CardPayment.java:107)
at com.simplepay.hellomobile.CardPayment.setUpLayoutAndActionListeners(CardPayment.java:81)
at com.simplepay.hellomobile.CardPayment.onCreate(CardPayment.java:56)
at android.app.Activity.performCreate(Activity.java:5104)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
Note that all the network calls are been done using AsyncTask and placed inside the doInBackground method. The code is :
protected JSONObject doInBackground(Void... voids)
{
JSONObject initOrderResponse = walletService.initOrder(this.amount);
return initOrderResponse;
}
where initOrder is a normal java method and it has a line like:
responseBody = httpClient.execute(post,responseHandler);
But still I get this error.
Not sure where I'm making the mistake.
Thanks in advance.
Caused by: android.os.NetworkOnMainThreadException
You are trying to make a Network call on UI thread.
Hence, use an AsyncTask to accomplish such taks.
In Android 3.0 + versions, its made mandatory that any Network calls or actions that can potentially block the UI must be done on a background thread.
EDIT:
Try this in the onCreate()
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
I have seen people executing AsyncTask like this. new MyAsyncTask().doInBackground(); instead of new MyAsyncTask().execute();. Please make sure that you are not doing this mistake unintentionally.

Categories