Android Development: java NullPointerException When Trying To getStringExtra() - java

In my main activity, I have the following code that calls my FileBrowser activity:
Intent newFileIntent = new Intent(getBaseContext(), FileBrowser.class);
newFileIntent.putExtra("action", "browseDirectories");
startActivityForResult(newFileIntent, 2);
But when this code executes, my app force closes.
I ran the app again this time with DDMS open to look for the error, and here's what it is:
11-06 22:01:04.892: ERROR/AndroidRuntime(28287): Caused by: java.lang.NullPointerException
11-06 22:01:04.892: ERROR/AndroidRuntime(28287): at com.alexprice.devpad.FileBrowser.<init>(FileBrowser.java:17)
Here is line 17 (located outside of onCreate):
private String action = getIntent().getStringExtra("action");
What's wrong? Can I not use putExtra with startActivityForResult? Can putExtra only be used with startActivity?

Try moving the declaration inside the onCreate(), or any method, this will ensure you have acess to the intent data. Declaring the variable before onCreate() and thus any other method, you won't have acess to the intent extras.
Leave line 17 as private String action;
And inside onCreate()
action = getIntent().getStringExtra("action");

Related

Update an activity after a dialog is dismissed

In my main activity I have an alert that allows users to edit a setting. Once the user selects that setting and it's dismissed I want to call the onCreate() method to refresh the main activity to be updated with the new settings but I'm not sure how to call the main activity from inside the onClick() function.
The dialog is created in a method in the main activity. I could also call a refresh() method I've created inside my class to update everything but that has the same issue of calling a non-static method from a static context.
Try this :
MainActivity.recreate();
The activity will be recreated with new instance so It will recall onCreate again as you needed. It is supported for API 11 or higher.
For APIs lower than 11 use this:
Intent intent = getIntent();
finish();
startActivity(intent);

Start two activities from Android application

I have an application A that wants to start an Activity in another application, B, which I don't own and cannot edit.
If B is already running and visible in recent apps, there's no problem in executing the wanted Activity of B using an Intent.
If B isn't running, I use the following code to execute its main Activity first, and then the one I want to execute:
String bPackage = "com.example.applicationb";
PackageManager pm = getPackageManager(this);
Intent main = pm.getLaunchIntentForPackage(bPackage);
Intent wanted = new Intent();
wanted.setPackage(bPackage);
wanted.setComponent(new ComponentName(bPackage,bPackage+".WantedActivity"));
main.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
wanted.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
wanted.setExtras(mPreviouslyCreatedBundle);
startActivity(main);
startActivity(wanted);
The wanted Activity executes, but after some seconds I get an error and it stops working. Am I setting the Intents in a wrong way?
Make sure u have setted exported="true" for activity you are trying to redirect to another package
Basically my idea is when your second paackage app leave u need :
android.os.process.killprocess(android.os.process.mypid())
and when you launch use flag as start new task :
setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

Android StartActivityForResult bug

I have an android app which passes some results to the next activity using StartActivityForResult. This is working fine on every phone/tablet I've tested it on except for a Motorola MZ601 which is on Android 4.0.4. By debugging through the code on any other device, it runs the below code:
private void showNextPage() {
Intent i = new Intent(this, InstructionsActivity.class);
startActivityForResult(i, TEST_LEFT_ACTIVITY);
}
This then takes me to the onActivityResult method as expected. Now when I debug through the code on the Motorola instead of taking to the onActivityResult method, it takes me to the onCreate method and reinitialises the results to 0 before moving to the onActivityResult method. So it passes null results over.
Can anyone explain to me why it takes me to the onCreate method before taking my to the onActivityResult method only on this device?
Is it a bug in the version of android or is it the device?

using a constructor in an Activity class and trying to get information out from that constructor to display on the associated screen

My activity class contains a constructor that computes some data:
public IPrintPanelActivity(String title, Object data, byte logoChar,
String keyName, boolean printNCopies, boolean showPrintButton) {
/*
* Configure the panel
*/
super();
panelTitle = title;
this.logoChar = logoChar;
if (data != null) {
setTextArea((String) data);
}
//put the display print panel here
SignOnActivity.startMyActivity(context,(String) data,"CORRECT?");
finish();
}
Then I need this data Object (which is actually a string) to display it in a TextView of the associated layout file. The problem is that I don't know how to get data "out of the constructor" to write something like
myTextView.setText(data);
I found an answer to a question that was asked more than 2 years ago and it seems to be what I need. The problem is that I get a NullPointerException for the context variable.
Here's the definition of the static function startMyActivity:
public static void startMyActivity(Context context, String paramA, String paramB) {
// Build extras with passed in parameters
Bundle extras = new Bundle();
extras.putString("PARAM_A", paramA);
extras.putString("PARAM_B", paramB);
// Create and start intent for this activity
Intent intent = new Intent(context,IPrintPanelActivity.class);
intent.putExtras(extras);
context.startActivity(intent);
}
Do I give you enough information? Please let me know and please help me fix the NullPointerException.
Do not create constructors for your Activity classes. The Android OS is responsible for instantiating Activities and will only try to do so with the default constructor. Keep in mind that an Activity may be destroyed and recreated by the system at various times, usually during configuration changes, such as when the device is rotated between portrait and landscape orientations.
Any "arguments" you pass to an Activity should be done using a Bundle in the Intent that starts the Activity. In one of the lifecycle callback methods (e.g. onCreate()) you can call getIntent() and check its extras for data, then do as you wish.
The link you posted where the user Chase create d static method for starting an activity still follows these guidelines. All his method does is compose the Intent and its extras using the arguments of the static method, then calls startActivity using this Intent. He did not create a constructor for the Activity, nor did he ever call new to instantiate an Activity. He just simplified the process of creating the Intent and its extras to start the Activity with the proper data.
You don't have constructors explicitly for Activties. You don't instantiate a Activity class. You only declare the Activity in manifest file.
Please read the answer by Raghav Sood
And i quote Raghav
By treating an Activity as a normal Java class, you end up with a null
context. As most methods in an Activity are called on its Context, you
will get a null pointer exception, which is why your app crashes.
Can i Create the object of a activity in other class?

Android onResume not called after finish() command on different activity

I have 2 Activities. My first activity calls the second activity. Once a button is clicked in the second activity it calls the finish() method on that activity causing it to return to Activity 1. This is fine, but my problem is that when it returns none of the expected methods are called (onResume(), onRestart(), on Start()). Is there anything that I could be missing? Anything that can inhibit the call? I have logs in each of the methods making sure that they aren't called. I have also added the android:noHistory="false" into my manifest to try solving the problem, but it did not fix it.
Here is the way I call the Activity
intent = new Intent(this, ViewEdit.class);
startActivity(intent);
and here is an example of the setup of the log in the methods
public void onStart(Bundle bundle){
super.onStart();
Log.d(MYACT, "On Start");
}
Thanks for any help.

Categories