This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 5 years ago.
I am trying to implement a simple navigation drawer in my Android app but I am not able to solve a certain bug.
Attempting to run an app throws a NullPointerException:
Process: com.emertonalex.whiskynotes04, PID: 22225
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.emertonalex.whiskynotes04/com.emertonalex.whiskynotes04.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.app.ActionBar.setDisplayHomeAsUpEnabled(boolean)' 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.support.v7.app.ActionBar.setDisplayHomeAsUpEnabled(boolean)' on a null object reference
at com.emertonalex.whiskynotes04.MainActivity.setupToolbar(MainActivity.java:320)
at com.emertonalex.whiskynotes04.MainActivity.onCreate(MainActivity.java:97)
at android.app.Activity.performCreate(Activity.java:6999)
at android.app.Activity.performCreate(Activity.java:6990)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1214)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2731)
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)
As far as I see it the getSupportActionBar() function call that messes stuff up. I tried covering the call with the "assert" in setupToolbar() and it seems like it passes that stage and fails at the
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
The pieces of code themselves that call the getSupportActionBar() are these:
In MainActivity
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.add_notes);
...
setupToolbar();
getSupportActionBar().setDisplayHomeAsUpEnabled(false);
getSupportActionBar().setHomeButtonEnabled(true);
...
}
And
void setupToolbar(){
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
assert getSupportActionBar() != null;
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
I am new to Android/Java dev and would be very appreciative of any help.
Also, it would be great if you tell me why this error appears, to begin with, so I can avoid it in the future.
Thank you
Changing from Support Action Bar to ActionBar should fix the issue:
getActionBar().setDisplayHomeAsUpEnabled(true);
Related
This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 2 years ago.
I'm trying to set a textview to a editText text. When I do that I have this error:
Process: com.example.game, PID: 9829
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
at com.example.game.MainActivity.onClick(MainActivity.java:79)
at android.view.View.performClick(View.java:5637)
at android.view.View$PerformClick.run(View.java:22429)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
It's strange because when I do system.out.println with the text I get
I/System.out: Player1
Can someone help me ?
My code:
System.out.println(p1.getText().toString());
System.out.println(p1.getEditableText().toString());
j1.setText(p1.getEditableText().toString());
j2.setText(p2.getEditableText().toString());
p1 and p2 are editText and j1 and j2 are TextView. I don't know if it's the problem but they are in different resource layout.
All your views are needed to be initialized after setting setContentView(R.layout.activity_main)
However in your case you have them in different resource layout.
let's say you want to have views of layout_xyz in activity_main.
in activity_main
<include
android:id="#+id/xyzView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
layout="#layout/layout_xyz" />
In MainActivity.java
initialize views from layout_xyz
View xyzView = findViewById(R.id.xyzView);
TextView j1FromXyz = xyzView.findViewById(R.id.j1);
TextView j2FromXyz = xyzView.findViewById(R.id.j2);
For more: Re-using layouts with <include/>
I have added a menu in a simple app of hello world! But my app keeps crashing.
I am not adding my xml code. My xml file name is menu inside the menu folder in res.
Here is my java file.
MyActivity.java
public boolean onCreateOptionsMenu(Menu menu1){
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu,menu1); //line 19
return true;
}
Logcat
06-03 18:03:57.682 13542-13542/com.example.kaushalraj.a424 E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.kaushalraj.a424, PID: 13542
android.view.InflateException: Couldn't resolve menu item onClick handler action in class com.example.kaushalraj.a424.MainActivity
at android.support.v7.view.SupportMenuInflater$InflatedOnMenuItemClickListener.<init>(SupportMenuInflater.java:253)
at android.support.v7.view.SupportMenuInflater$MenuState.setItem(SupportMenuInflater.java:481)
at android.support.v7.view.SupportMenuInflater$MenuState.addItem(SupportMenuInflater.java:529)
at android.support.v7.view.SupportMenuInflater.parseMenu(SupportMenuInflater.java:205)
at android.support.v7.view.SupportMenuInflater.inflate(SupportMenuInflater.java:127)
at com.example.kaushalraj.a424.MainActivity.onCreateOptionsMenu(MainActivity.java:19)
at android.app.Activity.onCreatePanelMenu(Activity.java:3388)
at android.support.v4.app.FragmentActivity.onCreatePanelMenu(FragmentActivity.java:364)
at android.support.v7.view.WindowCallbackWrapper.onCreatePanelMenu(WindowCallbackWrapper.java:93)
at android.support.v7.app.AppCompatDelegateImplBase$AppCompatWindowCallbackBase.onCreatePanelMenu(AppCompatDelegateImplBase.java:332)
at android.support.v7.app.AppCompatDelegateImplV9.preparePanel(AppCompatDelegateImplV9.java:1377)
at android.support.v7.app.AppCompatDelegateImplV9.doInvalidatePanelMenu(AppCompatDelegateImplV9.java:1657)
at android.support.v7.app.AppCompatDelegateImplV9$1.run(AppCompatDelegateImplV9.java:134)
at android.os.Handler.handleCallback(Handler.java:790)
at android.os.Handler.dispatchMessage(Handler.java:99)
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.NoSuchMethodException: action [interface android.view.MenuItem]
at java.lang.Class.getMethod(Class.java:2068)
at java.lang.Class.getMethod(Class.java:1690)
at android.support.v7.view.SupportMenuInflater$InflatedOnMenuItemClickListener.<init>(SupportMenuInflater.java:249)
at android.support.v7.view.SupportMenuInflater$MenuState.setItem(SupportMenuInflater.java:481)
at android.support.v7.view.SupportMenuInflater$MenuState.addItem(SupportMenuInflater.java:529)
at android.support.v7.view.SupportMenuInflater.parseMenu(SupportMenuInflater.java:205)
at android.support.v7.view.SupportMenuInflater.inflate(SupportMenuInflater.java:127)
at com.example.kaushalraj.a424.MainActivity.onCreateOptionsMenu(MainActivity.java:19)
at android.app.Activity.onCreatePanelMenu(Activity.java:3388)
at android.support.v4.app.FragmentActivity.onCreatePanelMenu(FragmentActivity.java:364)
at android.support.v7.view.WindowCallbackWrapper.onCreatePanelMenu(WindowCallbackWrapper.java:93)
at android.support.v7.app.AppCompatDelegateImplBase$AppCompatWindowCallbackBase.onCreatePanelMenu(AppCompatDelegateImplBase.java:332)
at android.support.v7.app.AppCompatDelegateImplV9.preparePanel(AppCompatDelegateImplV9.java:1377)
at android.support.v7.app.AppCompatDelegateImplV9.doInvalidatePanelMenu(AppCompatDelegateImplV9.java:1657)
at android.support.v7.app.AppCompatDelegateImplV9$1.run(AppCompatDelegateImplV9.java:134)
at android.os.Handler.handleCallback(Handler.java:790)
at android.os.Handler.dispatchMessage(Handler.java:99)
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)
I don't know if this your case (and i don't have the reputation to comment) but: What app theme are you using? If you are using a theme that does not have a actionbar at the top then you will get this error when you try to attach your menu to it, as the actionbar does not exist.
You have two solutions:
Check your theme in res/values/styles.xml if you are using something like Theme.AppCompat.Light.NoActionBar you must change it to something like Theme.AppCompat.Light.DarkActionBar. This will add the actionbar to your activity.
Using the support toolbar library, add the toolbar to your activity xml layout then from your activity class get the view in OnCreate() and use setSupportActionBar(myToolbar). This will set your toolbar view as the actionbar where your menu will be attached to.
First of all sorry for my English, I am a new android developer (hobbies) and I have a problem.
I am sorry but I do not know how to do a better description of my issue. Inded, since 3 days I try to fix this issue but I have no more idea.
I believe to think android studio is crazy, or its probably to me.
So in my project, I have tried to implement an auto update system but to do that I already achieve the part to get the version from my text on server.
But since I have implement the code to get the current version of my app "app name", I have the same error
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{erim.ups/erim.ups.PreLoadScreen}: 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:2361)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2510)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1363)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5461)
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.content.pm.ApplicationInfo android.content.Context.getApplicationInfo()' on a null object reference
at android.content.ContextWrapper.getApplicationInfo(ContextWrapper.java:156)
at android.view.ContextThemeWrapper.getTheme(ContextThemeWrapper.java:110)
at android.app.AlertDialog.resolveDialogTheme(AlertDialog.java:222)
at android.app.AlertDialog$Builder.(AlertDialog.java:452)
at erim.ups.PreLoadScreen.(PreLoadScreen.java:44)
at java.lang.Class.newInstance(Native Method)
at android.app.Instrumentation.newActivity(Instrumentation.java:1068)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2351)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2510)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1363)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5461)
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)
But the line 44 is to declare sharedPreferences, I dont understand what is my problem.
final SharedPreferences data = this.getSharedPreferences("Mes_Parametres", MODE_PRIVATE);
Could you please help me ?
Link of my source: preloadScreen.java
In general, do not call methods that you inherit from Activity until after super.onCreate() has been called in the onCreate() method.
Replace:
final SharedPreferences sharedPrefs = getSharedPreferences("Mes_Parametres", MODE_PRIVATE);
final SharedPreferences data = this.getSharedPreferences("Mes_Parametres", MODE_PRIVATE);
final String monParametreUrl = data.getString("ParametreUrl", null);
with:
SharedPreferences sharedPrefs;
SharedPreferences data;
String monParametreUrl;
Then, change your onCreate() to:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
sharedPrefs = getSharedPreferences("Mes_Parametres", MODE_PRIVATE);
data = this.getSharedPreferences("Mes_Parametres", MODE_PRIVATE);
monParametreUrl = data.getString("ParametreUrl", null);
setContentView(R.layout.activity_pre_load_screen);
// rest of your onCreate() code goes here
}
Also, in the future, when you ask a question here on Stack Overflow, please put the relevant source code in the question, rather than as a link. After all, as soon as you delete that file in DropBox, your question can no longer help other developers nearly as well.
I work with SVG format, added a library, all the methods recognized, did everything as in the example and then run NullPointerException ??
I can not understand why ...
Here is my onCreate method in Activity
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_camera);
RelativeLayout rlHumanBody = (RelativeLayout) findViewById(R.id.control);
\\But in this line error
SVG svg = SVGParser.getSVGFromResource(getResources(), R.raw.frontfigure);
Drawable drawable = svg.createPictureDrawable();
rlHumanBody.setBackground(drawable);
}
is such a mistake
FATAL EXCEPTION: main
Process: com.example.android.camera2basic, PID: 4396
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.
android.camera2basic/com.example.android.camera2basic.CameraActivity}:
com.applantation.android.svg.SVGParseException: java.lang.NullPointerException:
Attempt to invoke virtual method 'float java.lang.Float.floatValue()' on a null
object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
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 :903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Caused by: com.applantation.android.svg.SVGParseException: java.lang.
NullPointerException: Attempt to invoke virtual method 'float java.lang.Float.
floatValue()' on a null object reference
at com.applantation.android.svg.SVGParser.parse(SVGParser.java:211)
at com.applantation.android.svg.SVGParser.getSVGFromResource(SVGParser.java:1 10)
at com.example.android.camera2basic.CameraActivity.onCreate(CameraActivity.java:222)
the last line here indicates the line 222 is a line in my onCreate method in which code and crashes.
What am I doing wrong?
Just put the "savedInstanceState" in an "if" block and see what it does.
You might actually be passing a "null" state somehow (not very 'pro' in android, so sorry for the dumb suggestion)
in the class(is it an Activity?) view there is no such view. It usually happens because of one of three reasons:
1) you forgot to setContentView
2) you try to get view before you called setContentView
3) you use a wrong layout in setContentView
please check once for third point
My app crashes on this setText here. I think probably because it cannot find the id so I looked into to R.java and found the id there. I'm confused why it still crashes.
TextView city = (TextView)findViewById(R.id.city_result);
city.setText(msg[0]);
R.java
public static final int city_result=0x7f080041;
and this code is in the public static final class id method.
Any help will be appreciated! Thanks!!
================
More Code:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_weather_report);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment())
.commit();
}
//get message from intent
Intent intent = getIntent();
String[] message = intent.getStringArrayExtra(MainActivity.EXTRA_MESSAGE);
setResult(message);
}
public void setResult(String[] msg)
{
//test
TextView city = (TextView)findViewById(R.id.city_result);
city.setText(msg[0]);
}
This file I just started, elsewhere is just system generated new file code, I didn't edit yet.
Here's logcat:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.chen.weather/com.example.chen.weather.WeatherReport}: 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:2298)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
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:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
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.example.chen.weather.WeatherReport.setResult(WeatherReport.java:78)
at com.example.chen.weather.WeatherReport.onCreate(WeatherReport.java:32)
at android.app.Activity.performCreate(Activity.java:5933)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
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:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
NullPointerException: Attempt to invoke virtual method 'void
android.widget.TextView.setText
Means city TextView object is null due to :
1. setContentView is not called before using findViewById:
2. Not using layout in which TextView is defined with city_result id. make sure you are passing right layout in setContentView
check that your activity is correct!!
setContentView(R.layout.activity_weather_report);
Which layout did you put the TextView? I doubt that in this case, you put the TextView in PlaceHolder fragment layout (not the activity_weather_report.xml layout). If so, putting the TextView in activity_weather_report.xml will solve your problem.
Problem is just the id
Make sure that you are using correct id for TextView in Layout xml file .
I had same issue and it resolved by solving spelling mistake .
it is showing this exception because your textview is not getting data at time of onCeate method.
declare your textview in onCreate method,
put yourtextview.setText(xyz) wherever you are getting that "xyz" value.
Hope this will work.