I'm a newbie in Android environment, therefore I want to know the best practice in this case.
I have a scanner App in Android, depending the result of each scan I have the method startActivityForResult
where I can call different Activities depending of the result.
The initial version of my code... I copy/paste the same method in each Activity, obviously this is a crime, and I know it's awful.
Now with more time, I want to correct this part, for that, I created a Independent Activity with the method startActivityForResult
, with the idea to call this method in each Activity, but didn't work: NullPointerException
.
@Override
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
if (requestCode == SCANNER_REQUEST_CODE) {
// Handle scan intent
if (resultCode == Activity.RESULT_OK) {
ControllerActivity controllerActivity = new ControllerActivity ();
controllerActivity.getActivity(requestCode, resultCode, intent);
} else if (resultCode == Activity.RESULT_CANCELED) {
// Handle cancel
}
} else {
// Handle other intents
}
}
04-15 17:31:09.279: W/dalvikvm(923): threadid=1: thread exiting with uncaught exception (group=0x40018578)
04-15 17:31:09.289: E/AndroidRuntime(923): FATAL EXCEPTION: main
04-15 17:31:09.289: E/AndroidRuntime(923): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=123, result=-1, data=Intent { act=client.android.SCAN flg=0x80000 (has extras) }} to activity {reader/reader.MainActivity}: java.lang.NullPointerException
04-15 17:31:09.289: E/AndroidRuntime(923): at android.app.ActivityThread.deliverResults(ActivityThread.java:2536)
04-15 17:31:09.289: E/AndroidRuntime(923): at android.app.ActivityThread.handleSendResult(ActivityThread.java:2578)
I was thinking to use Fragments, but finally I thought this is for another thing..
What is the best practise in this cases in Android?
public class ControllerActivity extends Activity {
public void getActivity (int requestCode, int resultCode, Intent intent) {
String contents = intent.getStringExtra("SCAN_RESULT");
Data data = new Data(contents);
Utils utils = new Utils ();
String formatName = intent.getStringExtra("SCAN_RESULT_FORMAT");
Intent reportActivity = new Intent(this, ReportActivity.class);
Intent decodeActivity = new Intent(this, DecodeActivity.class);
if (data.Matrix (contents, formatName)){
reportActivity.putExtra("IdValue", data.getId());
reportActivity.putExtra("PerfValue", data.getPerf());
reportActivity.putExtra("MpIdValue", data.getMpId());
startActivity(reportActivity);
}else if(data.isPool(contents, formatName)){
decodeActivity.putExtra("MandValue",dataMatrix.getMand());
decodeActivity.putExtra("MaterialValue", decodeActivity.getMaterialNummer());
startActivity(decodeActivity);
}
As mentioned in comments you cannot just instantiate an Activity.
You should change your control activity to a factory that creates the appropiate Intent
class ControllerIntentBuilder
{
public static Intent create(Content context, int requestCode, int resultCode, Intent resultIntent)
{
String contents = resultIntent.getStringExtra("SCAN_RESULT");
Data data = new Data(contents);
String formatName = intent.getStringExtra("SCAN_RESULT_FORMAT");
if (data.Matrix(contents, formatName))
{
Intent activityIntent = new Intent(context, ReportActivity.class);
activityIntent.putExtra("IdValue", data.getId());
activityIntent.putExtra("PerfValue", data.getPerf());
activityIntent.putExtra("MpIdValue", data.getMpId());
return activityIntent;
}
// and so on for other conditions
}
}
Then your onActivityResult will the one starting the activity
public void onActivityResult(int requestCode, int resultCode, Intent intent)
{
if (requestCode == SCANNER_REQUEST_CODE) {
// Handle scan intent
if (resultCode == Activity.RESULT_OK) {
Intent intent = ControllerIntentBuilder.create(this, requestCode, resultCode, intent);
startActivity(intent);;
} else if (resultCode == Activity.RESULT_CANCELED) {
// Handle cancel
}
} else {
// Handle other intents
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.