[英]Android Helper Class to call several Activities
I'm a newbie in Android environment, therefore I want to know the best practice in this case. 我是Android环境中的新手,因此我想了解这种情况下的最佳做法。
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. 我在Android中有一个扫描仪应用程序,具体取决于每次扫描的结果,我具有方法
startActivityForResult
,在其中可以根据结果调用不同的Activity。
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. 我的代码的初始版本...我在每个Activity中复制/粘贴相同的方法,显然这是犯罪,而且我知道这很糟糕。
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
. 现在,我有更多时间要更正此部分,为此,我使用
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)
I was thinking to use Fragments, but finally I thought this is for another thing.. 我当时想使用Fragments,但最终我认为这是另一回事。
What is the best practise in this cases in Android? 在这种情况下,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 然后,您的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
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.