I'm getting this error and I don't know why. This same code was working before; the problem started occuring after an update at some point.
Stack trace:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.domain.appname, PID: 17964
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.domain.appname/com.domain.appname.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.domain.appname.NavigationDrawerFragment.setUp(int, android.support.v4.widget.DrawerLayout)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2379)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2441)
at android.app.ActivityThread.access$800(ActivityThread.java:162)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1349)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5431)
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:914)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:707)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.domain.appname.NavigationDrawerFragment.setUp(int, android.support.v4.widget.DrawerLayout)' on a null object reference
at com.domain.appname.MainActivity.onCreate(MainActivity.java:40)
at android.app.Activity.performCreate(Activity.java:6056)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2332)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2441)
at android.app.ActivityThread.access$800(ActivityThread.java:162)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1349)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5431)
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:914)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:707)
Here's the offending code in MainActivity.java:
...
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (BuildConfig.DEBUG){ Log.d(TAG, "Created in debug mode"); }
NavigationDrawerFragment mNavigationDrawerFragment = (NavigationDrawerFragment)
getSupportFragmentManager().findFragmentById(R.id.navigation_drawer);
// Set up the drawer.
Log.d(TAG, "Setting up drawer");
mNavigationDrawerFragment.setUp( // Line 40
R.id.navigation_drawer,
(DrawerLayout) findViewById(R.id.drawer_layout)
);
...
I've found two questions that seem to have similar problems. (Here's the first and the second) The first doesn't seem relevant, and the second is unanswered.
I'm baffled. drawer_layout and navigation_drawer do not throw "symbol unresolved" errors. Like I said, this all used to work...
You are never calling Activity.setContentView(int). This is how it should be:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_containing_drawer);
// Rest.
}
Related
ERROR
**2020-03-01 17:36:58.959 6589-6589/com.studenthelper.bscit E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.studenthelper.bscit, PID: 6589
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.studenthelper.bscit/com.studenthelper.bscit.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2914)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3049)
at android.app.ActivityThread.handleRelaunchActivityInner(ActivityThread.java:4785)
at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:4694)
at android.app.servertransaction.ActivityRelaunchItem.execute(ActivityRelaunchItem.java:69)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1809)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6692)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
at com.studenthelper.bscit.MainActivity.onCreate(MainActivity.java:21)
at android.app.Activity.performCreate(Activity.java:7140)
at android.app.Activity.performCreate(Activity.java:7131)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1272)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2894)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3049)
at android.app.ActivityThread.handleRelaunchActivityInner(ActivityThread.java:4785)
at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:4694)
at android.app.servertransaction.ActivityRelaunchItem.execute(ActivityRelaunchItem.java:69)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1809)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6692)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858) **
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
TextView textView=findViewById(R.id.logotext);
int unicode=0x1F4A1;
String emoji=getEmoji(unicode);
String Text="Bsc"+emoji+"T";
textView.setText(Text);
}
public String getEmoji(int uni)
{
return new String(Character.toChars(uni));
}
As per your crash logs:
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858) Caused by:
java.lang.NullPointerException: Attempt to invoke virtual method 'void
android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference at
com.studenthelper.bscit.MainActivity.onCreate(MainActivity.java:20)
It means your textView
TextView textView=findViewById(R.id.logotext);
Is not found from the layout R.layout.activity_main due to which when you try to textView.setText(Text); it's actually null and exception occurs.
So, I'll suggest to
Verify textVIew Id is same in the layout? and
Set the getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
before the setContentView(R.layout.activity_main);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main);
TextView textView=findViewById(R.id.logotext);
int unicode=0x1F4A1;
String emoji=getEmoji(unicode);
String Text="Bsc"+emoji+"T";
textView.setText(Text);
}
To summarise the problem: You have defined two layouts, which one of them does not have a TextView. From your code obviously the crash is caused by calling .setText() on a null object.
The best way is always to add back the missing TextView back to your another layout, so all your layouts contains the same set of IDs. This can avoid bugs caused by findViewById(). In the layout which you do not need the TextView, you can simply set its android:visibility="gone" to hide it.
Alternatively back to your code, if you know the TextView is not always there, you can simply check if your textView variable is null or not, before calling .setText(). This can also avoid the NullPointerException.
This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
findViewById returns null
(4 answers)
Closed 4 years ago.
I'm trying to set the onClickListener on an ImageButton but I get the following error:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.bordi.hotel, PID: 18420
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.bordi.hotel/com.example.bordi.hotel.DetaiImage}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageButton.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2778)
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 'void android.widget.ImageButton.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at com.example.bordi.hotel.DetaiImage.onCreate(DetaiImage.java:28)
at android.app.Activity.performCreate(Activity.java:7009)
at android.app.Activity.performCreate(Activity.java:7000)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1214)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2731)
Below is the code I'm using:
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
close = (ImageButton) findViewById(R.id.closeDetail);
close.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
DetaiImage.super.onBackPressed();
}
});
}
Thanks in advance for your help!
You forgot setContentView(R.layout.activity_main);
after super.onCreate(savedInstanceState);
and if you have not a prior global declaration in your activity of close, do it like this:
ImageButton close = (ImageButton) findViewById(R.id.closeDetail);
This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 6 years ago.
When clicking a Button to switch from the main Activity to my "ColoursGame" Activity the app crashes.
I'm still a beginner so not an expert at debugging.
The main activity code
Button colorsGame = (Button) findViewById(R.id.colours);
colorsGame.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, ColoursGame.class));
}
});
Manifest new activity
<activity android:name=".ColoursGame"
android:label="ColourGame"
android:theme="#style/AppTheme.NoActionBar"></activity>
ColoursGame Activity OnCreate code
public class ColoursGame extends Activity {
int livesCount = 3;
String x;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_colours_game);
Button start = (Button) findViewById(R.id.startColors);
start.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
vSwitch.showNext();
x = String.valueOf(livesCount);
lives.setText(x);
text();
textColor();
backgroundColor();
start();
}
});
}
The Error
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.aynaar.numbersgameforkids, PID: 3278
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.aynaar.numbersgameforkids/com.aynaar.numbersgameforkids.ColoursGame}: 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:2548)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
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:2323)
at com.aynaar.numbersgameforkids.ColoursGame.<init>(ColoursGame.java:42)
at java.lang.Class.newInstance(Native Method)
at android.app.Instrumentation.newActivity(Instrumentation.java:1078)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2538)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
at com.aynaar.numbersgameforkids.ColoursGame.<init>(ColoursGame.java:42)
Don't call findViewById outside of onCreate, there's no Window to call findViewById at that point.
If you still get a NullPointer, then you must have #+id/startColors in activity_colours_game.xml
Answer explanation here
Button start = (Button) findViewById(R.id.startColors);
According to the error you are trying to reach a button that doesnt exist in your activity layout file (whatever it is). Check the id of your button and make sure it is placed on the layout page that related with your "ColoursGame" Activity.
First, I'm new to android SDK but I have a decent amount of experience in java. I'm attempting a tutorial on the SDK to get into it. I completed writing everything required but when I go to run it, the app crashes and I get this fatal error:
java.lang.NullPointerException: Attempt to invoke virtual method 'void
android.widget.Button.setOnClickListener(android.view.View$OnClickListener)'
on a null object reference
Ok, sure. Clearly, there is an issue with View.OnClickListener. Spelling, check. Capitalization, check. Parenthesis, check. Brackets, check. Semicolons, check. Next I checked the documentation for View.OnClickListener. I see its only method is onClick(View v). Ok well everything is fine there. The IDE isn't giving me any other errors. So at this point, I figure I need outside help. For referance, this is the button id:
android:id="#+id/button"
This is MainActivity.java
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Button;
import android.content.Intent;
import android.view.View;
public class MainActivity extends AppCompatActivity
{
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v){
goToSecondActivity();
}
});
}
private void goToSecondActivity() {
Intent intent = new Intent(this, SecondActivity.class);
startActivity(intent);
}
}
And here is the logcat
08-20 16:00:01.171 2565-2565/com.example.joe.helloworld E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.joe.helloworld, PID: 2565
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.joe.helloworld/com.example.joe.helloworld.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
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)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at com.example.joe.helloworld.MainActivity.onCreate(MainActivity.java:16)
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)
You've forgotten to call setContentView() with the layout id that contains the Button.
Since there is no content layout, calls to findViewById() return null.
setContentView(R.layout.activity_main);
Right after super.onCreate(...)
You forgot to set the layout for the user to view :
setContentView(R.layout.activity_main);
Also Instead of using :
button.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v){
goToSecondActivity();
}
});
}
Just use this :
button.setOnClickListener(new Button.OnClickListener()
{
#Override
public void onClick(View v){
goToSecondActivity();
}
});
}
That means inside setOnClickListener use new Button.OnClickListener instead of View.OnClickListener
Beginner Java/Android developer here. I know this is an unbelievably over-asked question, but everything I've tried from every other thread I could find DOES NOT WORK. No matter what. Either it will return null, or not work at all and throw an error on the line meant for getting a SharedPreferences object. Apologies if this is just a really tiny error.
Full stack trace and code below.
05-03 20:35:38.225 18928-18928/com.example.admin2.clicker E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.admin2.clicker, PID: 18928
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.admin2.clicker/com.example.admin2.clicker.Home}: java.lang.NullPointerException: Attempt to invoke interface method 'android.content.SharedPreferences$Editor android.content.SharedPreferences.edit()' 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: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)
Caused by: java.lang.NullPointerException: Attempt to invoke interface method 'android.content.SharedPreferences$Editor android.content.SharedPreferences.edit()' on a null object reference
at com.example.admin2.clicker.Home.<init>(Home.java:50)
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: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)
The code:
// Used to fetch SharedPreferences object
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
TextView xpText = (TextView) findViewById(R.id.xpText);
TextView lvText = (TextView) findViewById(R.id.lvText);
xpText.setText("Experience: 0/100");
lvText.setText("Level 1");
gameData = this.getSharedPreferences("gameData", MODE_PRIVATE);
}
// The line throwing the error + its context
{
//boolean firstRun = gameData.getBoolean("f", false);
boolean firstRun = true;
if (firstRun) {
SharedPreferences.Editor e = gameData.edit();
e.putInt("xp", 0);
e.putInt("mxp", 100);
e.putInt("lv", 1);
e.putInt("$", 0);
e.putInt("$c", 0);
e.apply();
}
}
If you have any questions or need more info, let me know. I'm not sure of everything I need to provide. Thanks.
You put the block of code to edit the preference in an initializer block which means it will be run before onCreate() where you actually assign gameData a value. That is what is leading to the NullpointerException.
As a rule you generally shouldn't do anything with an Activity until onCreate() is called. Especially if what you are doing requires a Context such as when dealing with preferences.
To fix this just move the code in the initializer block into the onCreate() method after you assign a value to gameData.
try this, you have to declare the file to save your data and after that you can only edit it.
SharedPreferences pref;
SharedPreferences.Editor editor;
pref = getSharedPreferences("MyPref", 0); // 0 - for private mode
editor = pref.edit();
i use commit() to update any data i want to save in SharedPreferences. As in your case is like
editor.putInt("xp", 0);
editor.putInt("mxp", 100);
editor.putInt("lv", 1);
editor.putInt("$", 0);
editor.putInt("$c", 0);
editor.commit();
i hope this will help you. Thanks.