我是Android环境中的新手,因此我想知道这种情况下的最佳做法。
我在Android中有一个扫描仪应用程序,根据每次扫描的结果,我有方法startActivityForResult
,我可以根据结果调用不同的活动。
我的代码的初始版本......我在每个Activity中复制/粘贴相同的方法,显然这是一种犯罪,我知道它很糟糕。
现在有了更多时间,我想更正这一部分,为此,我使用方法startActivityForResult
创建了一个独立活动,并想在每个活动中调用此方法,但没有工作: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)
我正在考虑使用Fragments,但最后我认为这是另一回事..
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);
}
答案 0 :(得分:0)
正如评论中所提到的,你不能只是实例化一个活动。
您应该将控制活动更改为创建适当意图的工厂
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
}
}
然后你的onActivityResult会开始活动
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
}
}