使用SL4A从python脚本返回结果返回Android应用程序

2024-10-01 13:43:25 发布

您现在位置:Python中文网/ 问答频道 /正文

在我的javaandroid应用程序中调用startActivityForResult()之后,我尝试使用(SL4A)从python脚本中获取结果。但是,我的onActivityResult()中的意图总是为空

从我的Java应用程序运行python脚本

public static Intent buildStartInBackgroundIntent(File script) {
final ComponentName componentName = Constants.SL4A_SERVICE_LAUNCHER_COMPONENT_NAME;
Intent intent = new Intent();
intent.setComponent(componentName);
intent.setAction(Constants.ACTION_LAUNCH_BACKGROUND_SCRIPT);
intent.putExtra(Constants.EXTRA_SCRIPT_PATH, script.getAbsolutePath());
return intent;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.custom_layout_running);
String pythonScript = "/sdcard/sl4a/scripts/pythondecoder.py";
Intent intent = buildStartInBackgroundIntent(new File(pythonScript));
intent.putExtra("serialinput","MessageIn");
startActivityForResult(intent, PYTHONSCRIPTDECODE);
}

完成后接收pythonscript的结果

^{pr2}$

但是,根据活动结果,Logcat Error Msg.

08-27 09:56:41.945: E/AndroidRuntime(29438): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=3050, result=0, data=null} to activity {xxx.xxx.xxx/com.xxx.xxx.CustomLayoutViewer}: java.lang.NullPointerException

这是我的python脚本,由SL4A运行

import android
droid = android.Android()
mIntent = droid.getIntent().result
Extras = mIntent["extras"]
Input = Extras["serialinput"]

Result_OK = -1
resultData = "Returned from SL4a Script!"
droid.setResultString(Result_OK, resultData)

当python脚本退出时,我的onActivityResult被通知为REQUEST=3050。但是,应该返回的意图是空的。我已经在python脚本中调用了setResultString()。你知道会出什么问题吗?在


Tags: 脚本scriptresult意图filexxxconstantsintent