Force close when app restarts - java

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();
}

Related

Can an activity loose its intent extras?

The app crashes in setParametersFromIntent which is called in onCreate of the activity. The method throws a custom RumtimeException if a specific intent extra is not set.
The crash is only reported rarely, I was not able to reproduce the bug.
I checked the code and there seems to be no possible route the activity could be started without the parameter being set.
Caused by java.lang.RuntimeException: intent extra not set
at com.myapp.myapp.activities.MyActivity.setParametersFromIntent(MyActivity.java)
at com.myapp.myapp.activities.MyActivity.onCreate(MyActivity.java)
at android.app.Activity.performCreate(Activity.java:5442)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2393)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2493)
at android.app.ActivityThread.access$800(ActivityThread.java:166)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1283)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5584)
at java.lang.reflect.Method.invokeNative(Method.java)
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(NativeStart.java)
I read that upon recreation of an activity, the intent extras are also restored, so I rule that out as a possible cause.
Does the log give any hints about how the activity has been started?
The android.app.Instrumentation.callActivityOnCreate also seems suspicious, as instrumentation should be disabled in the released app. Could this be someone trying to analyze and mess around with the activity, causing this exception?

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.

DialogFragment is relaunching itself when startActivity on new Intent is called

In my Android application I have a dialog fragment that contains a ListView. The onclick handler of the items in the list view calls my internalPlayer method which calls a VideoView activity to play a video. The VideoView activity is used in other places within the app so I know it is working.
private void internalPlayer(final String channelUrl)
{
final Intent localIntent = new Intent(getActivity(), InternalVideoActivity.class);
localIntent.putExtra(EXTRA_VIDEO_URI, Uri.encode(channelUrl, ALLOWED_URI_CHARS).toString());
localIntent.putExtra(GridViewFragment.CHANNEL_NAME, mCategory.toString() + " Episode ");
getActivity().startActivity(localIntent);
}
When running the app in a tablet everything works well and the video activity starts and plays the video. When I run the app on a phone device when I click an item in the ListView the dialog fragment relaunches itself and because the serialized objects originally passed to it no longer exist there is a NullPointerException throw.
Here's the log:
04-16 19:34:33.790 6000-6000/com.xystra W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x41998700)
04-16 19:34:33.790 6000-6000/com.xystra E/MAIN PHONE ACTIVITY: 1﹕ Unable to start activity ComponentInfo{com.xystra/com.xystra.activity.CatchUpSynopsisActivity}: java.lang.NullPointerException
04-16 19:34:33.814 6000-6000/com.xystra E/MAIN PHONE ACTIVITY﹕ STACK TRACE
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.xystra/com.xystra.activity.CatchUpSynopsisActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3740)
at android.app.ActivityThread.access$700(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1262)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5103)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.xystra.ui.fragment.ProgramFragment.onCreateView(ProgramFragment.java:369)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:1786)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:947)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1126)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1108)
at android.support.v4.app.FragmentManagerImpl.dispatchActivityCreated(FragmentManager.java:1917)
at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:544)
at roboguice.activity.RoboFragmentActivity.onStart(RoboFragmentActivity.java:60)
at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1171)
at android.app.Activity.performStart(Activity.java:5143)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
            at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3740)
            at android.app.ActivityThread.access$700(ActivityThread.java:141)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1262)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:5103)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:525)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
            at dalvik.system.NativeStart.main(Native Method)
Any help or ideas are greatly appreciated.
More Info
The dialog fragment loads properly. The listview is populated everything is good.
When I click an item in the listview my internalPlayer method is called. When getActivity().startActivity(localIntent) is executed the dialog fragment relaunches itself instead of launching the new intent/activity.
Because the fragment is relaunching itself, the serialized objects that were originally passed the first time are not available or null so the dialog fragment returns the exception you see above.
Everything works perfectly on a tablet. Only on phone devices do I get this event.
Well I got rid of the dialog fragment and used the existing activity fragment for my view, ran some tests and was able to deduce that I had an arithmetic exception in an underlying activity for some reason that was causing the issue. Really don't know how that method got recalled to cause the exception. Thanks for all your help to those who commented.

onclick Caused by: java.lang.NullPointerException

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);

Trying to add a Facebook Friend Picker, but get null pointer exception

I'm trying to learn and add some basic Facebook functionality to my Android App (an address book). What I'm eventually going to do is import a friends profile picture. I've currently only copy/pasted what I need from the "FriendPickerSample" example project, which is just a button that brings up a friend picker fragment, and, if I remember correctly, it places the names you picked into a list. I've altered it to come up when an imageview is clicked, and it'll just Toast the single name that is chosen. When I try to bring up the friend picker, I get a null pointer exception, and I don't understand why. Here's the log:
FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.JDE.RAB/com.JDE.RAB.PickFriendsActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
at android.app.ActivityThread.access$2300(ActivityThread.java:125)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:4627)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:521)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.facebook.widget.PickerFragment.onCreateView(PickerFragment.java:152)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:871)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1083)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:635)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1431)
at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:523)
at com.JDE.RAB.PickFriendsActivity.onStart(PickFriendsActivity.java:112)
at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1129)
at android.app.Activity.performStart(Activity.java:3781)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2636)
... 11 more
This is the code in my onclick, which is supposed to open the PickFriendsActivity:
if (Session.getActiveSession() == null || Session.getActiveSession().isClosed())
{
Session.openActiveSession(AddNewTabLayoutActivity.this, true, null);
}//endif
FriendPickerApplication application = (FriendPickerApplication) GetMyApplication();
application.setSelectedUsers(null);
Intent intent = new Intent(AddNewTabLayoutActivity.this, PickFriendsActivity.class);
// Note: The following line is optional, as multi-select behavior is the default for
// FriendPickerFragment. It is here to demonstrate how parameters could be passed to the
// friend picker if single-select functionality was desired, or if a different user ID was
// desired (for instance, to see friends of a friend).
//multi-select is OFF
PickFriendsActivity.populateParameters(intent, null, false, true);
Debugger.SendNotification(getApplicationContext(), "Alert", "Alert", "Starting Activity For Result", 3009);
startActivityForResult(intent, 2);
PickFriendsActivity, line 112 is super.onStart(); Should I post the entirety of that activities source code?
Thanks for any help!
You should copy res/layout/pick_friends_activity.xml to your own project too.

Categories

Resources