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/>
Related
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);
This question already has answers here:
Your content must have a ListView whose id attribute is 'android.R.id.list'
(7 answers)
Closed 6 years ago.
I've got a problem when trying to run my app in Android Studio. I get a
FATAL EXCEPTION: main
more precisely there seems to be a problem at:
dancam.com.chords.ChordsListActivity.onCreate(ChordsListActivity.java:13)
which is:
setContentView(R.layout.activity_chords_list);
here is the ChordsListActivity class:
import android.app.ListActivity;
import android.os.Bundle;
public class ChordsListActivity extends ListActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_chords_list);
String[] accordi = new String[] {"Do maggiore", "Re maggiore", "Si maggiore"};
String[] note = new String[] {"note: Do, Mi, Sol", "note: La, Re, Fa#", "note: Fa#, Si, Re#"};
int[] immagini = new int[] {R.drawable.do_maggiore, R.drawable.re_maggiore, R.drawable.si_maggiore};
CustomListAdapter adapter = new CustomListAdapter(getApplicationContext(), accordi, note, immagini);
setListAdapter(adapter);
}
}
I was kinda able to solve it by extending Activity instead of ListActivity but then I cannot use the setListAdapter() method.
I'd really appreciate if someone could help me fix the code!
please let me know if you need me to post the custom Adapter or any other file.
Edit: added the whole stack trace
FATAL EXCEPTION: main
Process: dancam.com.chords, PID: 3080
java.lang.RuntimeException: Unable to start activity ComponentInfo{dancam.com.chords/dancam.com.chords.ChordsListActivity}: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
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.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
at android.app.ListActivity.onContentChanged(ListActivity.java:243)
at com.android.internal.policy.PhoneWindow.setContentView(PhoneWindow.java:398)
at android.app.Activity.setContentView(Activity.java:2166)
at dancam.com.chords.ChordsListActivity.onCreate(ChordsListActivity.java:13)
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)
This is problem with ListView.
Go to R.layout.activity_chords_list and add <ListView> and set id android:id="#android:id/list"
Like Exception says: 'Your content must have a ListView whose id attribute is 'android.R.id.list'
It should works if rest of the code is ok.
When you extend from ListActivity, it already has ListView, but Activity doesn't. You have to create list inbActivity's layout- better use RecyclerView instead of List view. In your activity create RecyclerView reference, initialize it in onCreate. Create adapter for your data in activity (onCreate method). Finnaly, set adapter to listview reference. In order to modify data in a list, set new data in adapter and call .notifyDatasetChange () on it. Notice, for RecyclerView you will also need LayoutManager (see docs)
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
When I open this activity my app crashes and says that it is due to this line of code:
private CheckBox chkWeekly = (CheckBox)findViewById(R.id.alarm_details_repeat_weekly);
The layout for alarm_details_repeat_weekly is right here:
<CheckBox
android:id="#+id/alarm_details_repeat_weekly"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/alarm_details_label_wednesday"
android:layout_alignBottom="#+id/alarm_details_label_wednesday"
android:layout_alignParentRight="true"
android:checked="true" />
Since some people asked, here is the logcat:
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:2083)
at minutemovement.mintemovement.com.minutemovement.AlarmDetailsActivity.<init>(AlarmDetailsActivity.java:28)
at java.lang.reflect.Constructor.newInstance(Native Method)
at java.lang.Class.newInstance(Class.java:1572)
at android.app.Instrumentation.newActivity(Instrumentation.java:1083)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2312)
Here is the full code: http://pastebin.com/EygRhAj4
You have a typo. You declared your id as alarm_details_repeat_wednesday in your xml but then tried to get an object with id alarm_details_repeat_weekly. Change your code to this:
private CheckBox chkWeekly = (CheckBox)findViewById(R.id.alarm_details_repeat_wednesday);
Try remove the private,
CheckBox chkWeekly = (CheckBox)findViewById(R.id.alarm_details_repeat_weekly); or declare the private CheckBox outside oncreate and initialize the object this way: chkWeekly = (CheckBox)findViewById(R.id.alarm_details_repeat_weekly);