Android app crashing when i hit a button [duplicate] - java

This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 6 years ago.
Hey guys so when I hit this particular button it is supposed to take me to the another layout. However everytime i hit it, the entire app crashes. Here is the onCreate method for that page im trying to get to:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_bench_data_entry);
//opens the database
openDB();
//Initializes and declares the sensors, and accelerometer
mInitialized = false;
mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
mAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
mSensorManager.registerListener(this, mAccelerometer, SensorManager.SENSOR_DELAY_NORMAL);
// tv.setText("Aasfdaf");
}
Here is the onClick for the button I am pressing:
public void onClick_Bench(View v){
//Intent to create a new activity
Intent intent = new Intent(MainMenuActivity.this, BenchDataEntry.class);
//Goes to the Data Entry Screen
startActivity(intent);
}
and here is the error log I am getting when I press the button:
08-08 17:30:30.785 31113-31113/com.example.jacob.repcapture E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.jacob.repcapture, PID: 31113
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.jacob.repcapture/com.example.jacob.repcapture.BenchDataEntry}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.Window.findViewById(int)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2327)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5422)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.Window.findViewById(int)' on a null object reference
at android.app.Activity.findViewById(Activity.java:2096)
at com.example.jacob.repcapture.BenchDataEntry.<init>(BenchDataEntry.java:49)
at java.lang.Class.newInstance(Native Method)
at android.app.Instrumentation.newActivity(Instrumentation.java:1067)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2317)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
            at android.app.ActivityThread.-wrap11(ActivityThread.java)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:148)
            at android.app.ActivityThread.main(ActivityThread.java:5422)
            at java.lang.reflect.Method.invoke(Native Method)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

I think error not in these code blocks logcat about android.view.View android.view.Window.findViewById(int)' on a null object reference
First - do clean project under build-menu, and restart the emulator. After that the exception went away in my situation.
Second - check FindviewById items You need to initialize them in the onCreate too. 

Related

Why is my intent not working when it doesn't show any syntax error? [duplicate]

This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 1 year ago.
The function called to switch activity is
public void GoToLogIn(View v){
Intent goToLogIn = new Intent(MainActivity.this,LogInActivity.class);
startActivity(goToLogIn);
finish();
}
This segment of code is showing error on logcat:
2021-08-21 12:43:33.396 9644-9644/com.example.expenditurehandlingapplication E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.expenditurehandlingapplication, PID: 9644
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.expenditurehandlingapplication/com.example.expenditurehandlingapplication.LogInActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.pm.ApplicationInfo android.content.Context.getApplicationInfo()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2679)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.pm.ApplicationInfo android.content.Context.getApplicationInfo()' on a null object reference
at android.content.ContextWrapper.getApplicationInfo(ContextWrapper.java:152)
at android.view.ContextThemeWrapper.getTheme(ContextThemeWrapper.java:157)
at android.content.Context.obtainStyledAttributes(Context.java:655)
at androidx.appcompat.app.AppCompatDelegateImpl.createSubDecor(AppCompatDelegateImpl.java:842)
at androidx.appcompat.app.AppCompatDelegateImpl.ensureSubDecor(AppCompatDelegateImpl.java:809)
at androidx.appcompat.app.AppCompatDelegateImpl.findViewById(AppCompatDelegateImpl.java:633)
at androidx.appcompat.app.AppCompatActivity.findViewById(AppCompatActivity.java:259)
at com.example.expenditurehandlingapplication.LogInActivity.<init>(LogInActivity.java:10)
at java.lang.Class.newInstance(Native Method)
at android.app.Instrumentation.newActivity(Instrumentation.java:1174)
I have given the proper class name, but the app still stops working at this point. Can anyone please tell me what did i do wrong in this?
Are you sure that you added all activities to Manifest file?

Binder invocation to an incorrect interface

I tried to build android app for android box. In my scenario my app need to stream live TV channels by using HDMI input.Why am I getting this kind of exception?
Any help to slow this error would be highly appreciated!
Thank you!
Fatal Exception: java.lang.RuntimeException: Unable to start activity ComponentInfo{eu.danman.zidostreamer.zidostreamer/eu.danman.zidostreamer.zidostreamer.MainActivity}: java.lang.SecurityException: Binder invocation to an incorrect interface
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2417)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2477)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1345)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5458)
at java.lang.reflect.Method.invoke(Method.java)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by java.lang.SecurityException: Binder invocation to an incorrect interface
at android.os.Parcel.readException(Parcel.java:1620)
at android.os.Parcel.readException(Parcel.java:1573)
at com.mstar.android.tv.ITvService$Stub$Proxy.getTvCommon(ITvService.java:365)
at com.mstar.android.tv.TvManager.getTvCommon(TvManager.java:205)
at com.mstar.android.tv.TvCommonManager.getService(TvCommonManager.java:136)
at com.mstar.android.tv.TvCommonManager.getCurrentInputSource(TvCommonManager.java:166)
at eu.danman.zidostreamer.zidostreamer.MainActivity.changeInputSource(MainActivity.java:49)
at eu.danman.zidostreamer.zidostreamer.MainActivity.enableHDMI(MainActivity.java:69)
at eu.danman.zidostreamer.zidostreamer.MainActivity.onCreate(MainActivity.java:89)
at android.app.Activity.performCreate(Activity.java:6251)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2370)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2477)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1345)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5458)
at java.lang.reflect.Method.invoke(Method.java)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

android page transition laybrary integration null pointer exception

Hello everyone i've got this error a nullPointerException error after i tried to integrate the android-page-transition and i couldn't find it's cause
this is the code where the error is shown PS: it's the same exact caude in the library i just integrated the code into mywork
/**
* Réglage Menu immersive title
*/
private void dealStatusBar() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
int statusBarHeight = getStatusBarHeight();
ViewGroup.LayoutParams lp = positionView.getLayoutParams();
lp.height = statusBarHeight;
positionView.setLayoutParams(lp);
}
}
this is the error i get when i run
11-19 12:11:30.781 16089-16089/mobile.esprit.tn.mobileteam E/AndroidRuntime: FATAL EXCEPTION: main
Process: mobile.esprit.tn.mobileteam, PID: 16089
java.lang.RuntimeException: Unable to start activity ComponentInfo{mobile.esprit.tn.mobileteam/mobile.esprit.tn.mobileteam.Activities.Project.PrjectsCardViews}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.ViewGroup$LayoutParams android.view.View.getLayoutParams()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2693)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2758)
at android.app.ActivityThread.access$900(ActivityThread.java:177)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1448)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:5942)
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:1400)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1195)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.ViewGroup$LayoutParams android.view.View.getLayoutParams()' on a null object reference
at mobile.esprit.tn.mobileteam.Activities.Project.PrjectsCardViews.dealStatusBar(PrjectsCardViews.java:129)
at mobile.esprit.tn.mobileteam.Activities.Project.PrjectsCardViews.onCreate(PrjectsCardViews.java:54)
at android.app.Activity.performCreate(Activity.java:6289)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2758) 
at android.app.ActivityThread.access$900(ActivityThread.java:177) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1448) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:145) 
at android.app.ActivityThread.main(ActivityThread.java:5942) 
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:1400) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1195) 
the PrjectsCardViews.java:129 is the code i posted above
ok i found the problem it was a dump fault caused by the copy and paste , i dident refere the oncreate to the necessary activity layout
You might have copied the projet but you may forgotten to change
<com.stone.transition.DragLayout
android:id="#+id/drag_layout" when you merged the code in your project check this out it will work

java.lang.OutOfMemoryError Displayed After Tapping Button Twice

I have two ImageButtons on one activity that work completely fine when pressed, one makes a call while the other opens up the sms text messaging app. I also have a 'skip' Button at the bottom of the activity where users can go back to their profile page, however, one I tap on the button once in simulator nothing happens and when I press it twice I receive this error java.lang.OutOfMemoryError. I set all my buttons up the same way so I'm not sure why I'm receiving this error.
I did have the button pressed within its own method, but then switched and put it in the onCreate. But here's the code for my button:
Button _skipButton = (Button)findViewById(R.id.skipButton);
_skipButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(MessageOrCallActivity.this, MyActivity.class));
}
});
Additionally, here's the error printed within the logs:
FATAL EXCEPTION: main
java.lang.OutOfMemoryError: Failed to allocate a 499046412 byte allocation with 1060954 free bytes and 62MB until OOM
at dalvik.system.VMRuntime.newNonMovableArray(Native Method)
at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:609)
at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:444)
at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:1080)
at android.content.res.Resources.loadDrawableForCookie(Resources.java:2635)
at android.content.res.Resources.loadDrawable(Resources.java:2540)
at android.content.res.TypedArray.getDrawable(TypedArray.java:870)
at android.widget.ImageView.<init>(ImageView.java:152)
at android.widget.ImageView.<init>(ImageView.java:140)
at android.support.v7.widget.AppCompatImageView.<init>(AppCompatImageView.java:58)
at android.support.v7.widget.AppCompatImageView.<init>(AppCompatImageView.java:54)
at android.support.v7.app.AppCompatViewInflater.createView(AppCompatViewInflater.java:95)
at android.support.v7.app.AppCompatDelegateImplV7.createView(AppCompatDelegateImplV7.java:938)
at android.support.v7.app.AppCompatDelegateImplV7.onCreateView(AppCompatDelegateImplV7.java:992)
at android.support.v4.view.LayoutInflaterCompatHC$FactoryWrapperHC.onCreateView(LayoutInflaterCompatHC.java:44)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:746)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:835)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:838)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798)
at android.view.LayoutInflater.inflate(LayoutInflater.java:515)
at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:256)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:109)
at com.example.myApp.myApp.MyActivity.onCreate(MyActivity.java:14)
at android.app.Activity.performCreate(Activity.java:6237)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Anyone know whats going on? I saw several post where this occurred while working with an array, but I have no arrays currently for this activity or the one I'm trying to transition to. Thanks in advance.

Why is getApplicationContext throwing nullPointerException, in singleton getInstance?

I have created a class (GameHelper) which extends SQLiteOpenHelper. I want it to be a singleton, so I can create it and have it store the contents of the database during runtime, with all other classes referencing it.
I followed the advice here, (Approach 1).
So I when I need to reference the database, I don't create a new instance of GameHelper, I call getInstance. Here is the relevant code:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game_info);
populate(getIntent().getStringExtra("game"), getIntent().getStringExtra("game"));
}
public void populate(String gameToGo, String game) {
DataTable game = gameHelper.getgame(game, gameToGo); //GameInfo.java:12
TextView tv = (TextView) findViewById(R.id.tableId);
String text = game.TABLE_ID;
tv.setText(text);
}
Which refers to the following method in GameHelper:
private static GameHelper sInstance;
public static synchronized GameHelper getInstance(Context context) {
// Use the application context, which will ensure that you
// don't accidentally leak an Activity's context.
// See this article for more information: http://bit.ly/6LRzfx
if (sInstance == null) {
sInstance = new GameHelper(context.getApplicationContext()); //GameHelper.java:60
}
return sInstance;
}
The problem occurs on the line sInstance = new GameHelper(context.getApplicationContext());, which throws a NullPointerException, as shown here:
11-08 10:14:54.974 16214-16214/com.example.android.whichgame E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.android.whichgame, PID: 16214
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.android.whichgame/com.example.android.whichgame.GameInfo}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2177)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2301)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1246)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5212)
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:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:602)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at android.content.ContextWrapper.getApplicationContext(ContextWrapper.java:109)
at com.example.android.whichgame.GameHelper.getInstance(GameHelper.java:60)
at com.example.android.whichgame.GameInfo.<init>(GameInfo.java:12)
at java.lang.Class.newInstanceImpl(Native Method)
at java.lang.Class.newInstance(Class.java:1208)
at android.app.Instrumentation.newActivity(Instrumentation.java:1062)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2155)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2301)
            at android.app.ActivityThread.access$800(ActivityThread.java:144)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1246)
            at android.os.Handler.dispatchMessage(Handler.java:106)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5212)
            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:786)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:602)
            at dalvik.system.NativeStart.main(Native Method)
I have spent all day scouring the internet for answers, and while I have found numerous responses to similar problems, I haven't found anything that has helped. The best and most popular answer seems to be that I need to set android:name for <application> in AndroidManifest, but herein lies another problem. I'm working in Android Studio, and when I try to add my application name, the name turns red. I suppose it's possible that I'm doing it wrong! My package (as defined in the manifest) is "com.example.android.whichgame", though the app itself (and the folder it is in) is called Whichgame.
So my question is this: What am I missing that is causing getApplicationContext() to kick out the NPE when called through getInstance? And if it is because I haven't set android:name, what is causing Studio and the compiler to reject the name?
You're using your GameInfo activity as a Context too early at initialization phase. Postpone the initialization that requires a valid Context to onCreate() or later in the activity lifecycle.

Categories

Resources