This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 3 years ago.
I am trying to declare a RecyclerView in an activity in my project. When I switch to this activity, the project crashes.
I am getting a NullPointerException when I try to instantiate my RecyclerView, see error below:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.marykate.marykatefordefyp, PID: 13490
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.marykate.marykatefordefyp/com.example.marykate.marykatefordefyp.ViewFavourites}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2264)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2313)
at android.app.ActivityThread.access$1100(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1238)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5336)
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:865)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:681)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.example.marykate.marykatefordefyp.ViewFavourites.onCreate(ViewFavourites.java:54)
at android.app.Activity.performCreate(Activity.java:5302)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1090)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2228
I have looked at similar questions on this site but none have the answer I am looking for. I am not referencing the wrong xml file and I am calling the correct RecyclerView.
See my code below:
RecyclerView eventsListview;
List<Favourite> favourites = new ArrayList<>();
FaveAdapter adapter = new FaveAdapter(this, R.layout.eventlist_layout, favourites);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_favourites);
eventsListview.findViewById(R.id.eventsListview1); //the line causing an error
Does anyone know what might be causing this?
eventsListview = (RecyclerView)findViewById(R.id.eventsListview1);
Related
LOGCAT error prevents me from opening the MAP activity and stops
working.
2019-07-03 17:12:01.642 29273-29273/com.example.ess E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.ess, PID: 29273
java.lang.IllegalStateException: Could not find method map(View) in a parent or ancestor Context for android:onClick attribute defined on view class android.support.v7.widget.AppCompatImageView with id 'imageView5'
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.resolveMethod(AppCompatViewInflater.java:424)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:381)
at android.view.View.performClick(View.java:6993)
at android.view.View$PerformClick.run(View.java:26512)
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:7025)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:441)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1408)
You applied the android:onClick=map attribute in your layout xml.
As you can see in the log it's on the ImageView android.support.v7.widget.AppCompatImageView with the android:id=#+id/imageView5
So either:
remove this line: android:onClick="map" and handle clicks with an OnClickListener
create this method in the Activity where you applied the layout:
public void map(View v) {}
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.
I'm new to Android development. I recently encoutered an issue when I hit a button on my application. I'm not sure what caused this because it worked flawlessly before I did some editing with the app, adding another activity, removing the software menu buttons on the splash screen, some other stuff. Not much of it had to do with the first activity which is where this problem exists. I would've posted all of the code but this website refused to let me do so
When I hit my button (readybutton) I get the following error:
java.lang.RuntimeException: Unable to start activity ComponentInfo
{com.unviewedchat/com.unviewedchat.SecondActivity}:
java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
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:5017)
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:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.unviewedchat.SecondActivity.onCreate(SecondActivity.java:23)
at android.app.Activity.performCreate(Activity.java:5231)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
Button code (readybutton)
button1 = (Button)findViewById(R.id.readybutton);
button1.setOnClickListener(this);
}
Button button1;
private void readyButtonclick()
{
startActivity(new Intent("unviewedchat.SecondActivity"));
}
public void onClick(View v) {
switch (v.getId()) {
case R.id.readybutton:
readyButtonclick();
break;
}
}
SecondActivity row 22-23
button2 = (Button)findViewById(R.id.help);
button2.setOnClickListener(this); }
this code is for the software menu buttons to open the help screen, does this actually effect the button code for FirstActivity?
There is no code on SecondActivity that i'm trying to access, i'm trying to go to that activity when someone hits the button.
I attempted to post all of the error report, only to get told it wasn't formatted correctly when it was, either way I added almost all of it.
Just change:
startActivity(new Intent("unviewedchat.SecondActivity"));
with this:
startActivity(new Intent(this, SecondActivity.class));
Caused by: java.lang.NullPointerException
at com.unviewedchat.SecondActivity.onCreate(SecondActivity.java:23)
So in here:
SecondActivity row 22-23
button2 = (Button)findViewById(R.id.help);
button2.setOnClickListener(this); }
... button2 is null. Either your layout you've set with setContentView() does not contain a view with id help, or you haven't called setContentView() at all. In either case findViewById() returns null.
where in the SecondActivity do you declare button2?
you should do it in the onCreate method after setContentView()
and check your layout you are in SecondActivity if the button with that specific id "help" actually exists
Try the following
use the code following code
button2 = (Button)findViewById(R.menu.help);
instead of
button2 = (Button)findViewById(R.id.help);
I really don't know how to start this, but I have searched through the whole of Google and stackoverflow.
I have this radio app which plays radio(duh) and display information about the song playing currently. But I have a problem here:
I have an Activity(MainActivity) and 3 Fragments(playerFragment, the rest are not important) and the fragments are tabs.
I parse the JSON data on MainActivity and display it on playerFragment. I am able to display it straight, no problems. But when I press the back the button and open the app again, it crashes immediately. Of course, I have a service playing the audio only and I ensure that the app loads the data when the user returns to the app(in this case the Activity and fragment is destroyed while service is still running). I checked the log it stated NullPointerException on the point when it starts to setText onCreate. I use the exact same code as it was to load the data at first (before destroying the Activity and Fragment).
I don't know how am I gonna post the code here because I do not know where went wrong and the code was long, please tell me which part of the code you would want to see to check what's wrong.
Code where it start to have error:
TextView titleTextView = (TextView) findViewById(R.id.titleTextView);
TextView artistTextView = (TextView) findViewById(R.id.artistTextView);
TextView lyricsTextView = (TextView) findViewById(R.id.lyricsTextView);
ImageView albumImageView = (ImageView) findViewById(R.id.albumImageView);
JSONObject jsonObject = new JSONObject(jsonData);
String title = jsonObject.getString("title");
String artist = jsonObject.getString("artist");
String lyrics = jsonObject.getString("lyrics");
String albumURL = jsonObject.getString("coverUrl");
STARTING HERE: ---> titleTextView.setText(title);
artistTextView.setText("\n" + artist);
lyricsTextView.setText("\n" + lyrics);
This code was able to display the data when I first open the app and parse it, the app the force close when I return/start to the app(while service is running).
The logcat:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.brandon.sgpradio/com.brandon.sgpradio.MainActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2395)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2453)
at android.app.ActivityThread.access$900(ActivityThread.java:173)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5579)
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:1268)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.brandon.sgpradio.MainActivity.parseJson(MainActivity.java:225)
at com.brandon.sgpradio.MainActivity.isMyServiceRunning(MainActivity.java:282)
at com.brandon.sgpradio.MainActivity.onCreate(MainActivity.java:97)
at android.app.Activity.performCreate(Activity.java:5451)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1093)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2359)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2453)
at android.app.ActivityThread.access$900(ActivityThread.java:173)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5579)
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:1268)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084)
at dalvik.system.NativeStart.main(Native Method)
Thanks!
Update:
1. Add a tag to your fragment when you add it to the activity via ViewPager.
fragment = new playerFragment();
fragment.setTag("player_fragment");
Get the fragment via getFragmentManger().findFragmentByTag() method.
PlayerFragment pFragment =
getFragmentManager().findFragmentByTag("player_fragment");
if(pFragment != null)
{
pFragment.setTextViewText();
}
I get this weird error in my MainActivity, can't reproduce it myself but this is what I get in Crash report:
java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=65538, result=0, data=null} to activity {lv.mobi.android/lv.mobi.android.MainActivity}: java.lang.NullPointerException
at android.app.ActivityThread.deliverResults(ActivityThread.java:3455)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:3498)
at android.app.ActivityThread.access$1300(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1294)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:212)
at android.app.ActivityThread.main(ActivityThread.java:5135)
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:877)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:693)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at lv.mobi.android.b.aj.a(Unknown Source)
at android.support.v4.app.FragmentActivity.onActivityResult(Unknown Source)
at lv.mobi.android.MainActivity.onActivityResult(Unknown Source)
at android.app.Activity.dispatchActivityResult(Activity.java:5423)
at android.app.ActivityThread.deliverResults(ActivityThread.java:3451)
... 11 more
I thought it could be something in onActivityResult, but can't figure out what, my result function doesn't have much in it:
#Override
protected void onActivityResult(int _requestCode, int _resultCode, Intent _data) {
super.onActivityResult(_requestCode, _resultCode, _data);
if(_resultCode==Activity.RESULT_OK){
reloadUser();
}
}
According to my guess, you need to put below code
if(resultCode != RESULT_CANCELED){
if(_resultCode==Activity.RESULT_OK){
reloadUser();
}
}
Because you are cancelling that activity/process due to that intent is getting null value.
For what i can see and based on the comments. It's a NullPointerException, witch means some where in your code you are not initializing a variable / class or something is returning null. If you like to read up on it to get a better understanding here is a link NullPointerException Documentation, also it would help to post some of your code.