我有一个问题,然后尝试用AppWidgetManager.ACTION_APPWIDGET_PICK
设置一个意图在Android AppWidget生态系统内部有一个问题,我可以从日志中看到。
所以我做错了?
请参阅下面的示例代码和堆栈跟踪
public class NPEDemoActivity extends Activity {
private final static int HOST_CODE = 1024;
private AppWidgetHost host;
private AppWidgetManager manager;
private int PICK_WIDGET_RC = 1;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
host = new AppWidgetHost(this, HOST_CODE);
host.startListening();
manager = AppWidgetManager.getInstance(this);
Button b = (Button)findViewById(R.id.Button01);
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int nextId = host.allocateAppWidgetId();
Intent pickIntent = new Intent(AppWidgetManager.ACTION_APPWIDGET_PICK);
pickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, nextId);
startActivityForResult(pickIntent, PICK_WIDGET_RC);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
}
}
来自log cat的堆栈跟踪:
10-15 17:33:06.873: ERROR/AndroidRuntime(821): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.android.settings/com.android.settings.AppWidgetPickActivity}: java.lang.NullPointerException
10-15 17:33:06.873: ERROR/AndroidRuntime(821): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2401)
10-15 17:33:06.873: ERROR/AndroidRuntime(821): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2417)
10-15 17:33:06.873: ERROR/AndroidRuntime(821): at android.app.ActivityThread.access$2100(ActivityThread.java:116)
10-15 17:33:06.873: ERROR/AndroidRuntime(821): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1794)
10-15 17:33:06.873: ERROR/AndroidRuntime(821): at android.os.Handler.dispatchMessage(Handler.java:99)
10-15 17:33:06.873: ERROR/AndroidRuntime(821): at android.os.Looper.loop(Looper.java:123)
10-15 17:33:06.873: ERROR/AndroidRuntime(821): at android.app.ActivityThread.main(ActivityThread.java:4203)
10-15 17:33:06.873: ERROR/AndroidRuntime(821): at java.lang.reflect.Method.invokeNative(Native Method)
10-15 17:33:06.873: ERROR/AndroidRuntime(821): at java.lang.reflect.Method.invoke(Method.java:521)
10-15 17:33:06.873: ERROR/AndroidRuntime(821): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
10-15 17:33:06.873: ERROR/AndroidRuntime(821): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:549)
10-15 17:33:06.873: ERROR/AndroidRuntime(821): at dalvik.system.NativeStart.main(Native Method)
10-15 17:33:06.873: ERROR/AndroidRuntime(821): Caused by: java.lang.NullPointerException
10-15 17:33:06.873: ERROR/AndroidRuntime(821): at com.android.settings.AppWidgetPickActivity.putAppWidgetItems(AppWidgetPickActivity.java:170)
10-15 17:33:06.873: ERROR/AndroidRuntime(821): at com.android.settings.AppWidgetPickActivity.putCustomAppWidgets(AppWidgetPickActivity.java:132)
10-15 17:33:06.873: ERROR/AndroidRuntime(821): at com.android.settings.AppWidgetPickActivity.getItems(AppWidgetPickActivity.java:208)
10-15 17:33:06.873: ERROR/AndroidRuntime(821): at com.android.settings.ActivityPicker.onCreate(ActivityPicker.java:99)
10-15 17:33:06.873: ERROR/AndroidRuntime(821): at com.android.settings.AppWidgetPickActivity.onCreate(AppWidgetPickActivity.java:63)
10-15 17:33:06.873: ERROR/AndroidRuntime(821): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1123)
10-15 17:33:06.873: ERROR/AndroidRuntime(821): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2364)
另一个小问题,com.android.settings中的android项目是哪个? (我被窗户困住,所以不能不幸地使用回购) 帮助真正贬值。
答案 0 :(得分:1)
在Android设置中看起来像是一个错误,如果您尝试在没有指定任何额外小部件的情况下选择appwidget,那么您将失败,在Google代码中填写了一个问题link
答案 1 :(得分:0)
编辑:说得太早,没看到它是ViewListener方法。不知道是否还需要。
原始回答:
您应该在覆盖中调用Acitivy的onClick
。
来自Android docs:“任何活动生命周期方法的实现应始终首先调用超类版本。
希望能解决问题。
答案 2 :(得分:0)
您确定了解决方法吗?我见过实现appwidget选择器的应用程序在他们的活动中运行appwidget主机,我现在收到这个确切的错误,说明EXTRA_CUSTOM_INFO不存在。我正在测试Launcher源代码中的一些内容,如果我至少可以解决错误,我会报告。