我创建了一个小部件,它有一个可点击的部分,可打开一个活动,显示所有细节,而不只是一个细节。但奇怪的事情发生了。
让我解释一下;窗口小部件工作正常,但是当您创建窗口小部件时,该按钮没有响应。 因此,当您创建另一个小部件时,您将共有2个小部件。现在,按钮类型有效...它响应我的点击但它崩溃了。 如果我完全重新启动手机然后使用小部件上的按钮,它会成功打开吗?
这是我的onUpdate():
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
for (int appWidgetId : appWidgetIds) {
updateAppWidget(context, appWidgetManager, appWidgetId);
Intent statsIntent = new Intent(context, DetailsActivity.class);
Bundle bundle = new Bundle();
bundle.putString("name", WidgetConfigureActivity.loadTitle(context, appWidgetId));
bundle.putString("id", WidgetConfigureActivity.Id(context, appWidgetId));
bundle.putString("avatarurl", WidgetConfigureActivity.loadAvatar(context, appWidgetId));
statsIntent.putExtras(bundle);
PendingIntent statsPendingIntent = PendingIntent.getActivity(context, appWidgetId, statsIntent, PendingIntent.FLAG_UPDATE_CURRENT);
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget);
views.setOnClickPendingIntent(R.id.openInAppButton, statsPendingIntent);
ComponentName componentName = new ComponentName(context.getPackageName(), WidgetProvider.class.getName());
appWidgetManager.updateAppWidget(componentName, views);
}
}
这是用户正在接收的NullpointerException:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: it.bjarn.android.myapp, PID: 2764
java.lang.RuntimeException: Unable to start activity ComponentInfo{it.bjarn.android.myapp/it.bjarn.android.myapp.activities.DetailsActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.os.BaseBundle.getString(java.lang.String)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2665)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.os.BaseBundle.getString(java.lang.String)' on a null object reference
at it.bjarn.android.myapp.activities.DetailsActivity.onCreate(DetailsActivity.java:111)
at android.app.Activity.performCreate(Activity.java:6679)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2618)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
另外,有助于了解:当它工作时,它仅使用来自1个窗口小部件的变量而不是您单击的窗口小部件打开活动。它总是打开同一个。
//编辑:
按请求添加了onCreate()类。这个没有错,因为它通过点击另一个活动中的按钮打开DetailsActivity时有效,它只是与小部件有问题。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
preferences = PreferenceManager.getDefaultSharedPreferences(this);
Methods.setLanguage(this, preferences.getString("app_language", Locale.getDefault().getLanguage()));
setContentView(R.layout.activity_details);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
window = getWindow();
mContext = this;
mApplication = getApplication();
int mIntervalMultiply = Integer.parseInt(preferences.getString("app_sync_frequency", "5"));
mInterval = 1000 * mIntervalMultiply;
Bundle bundle = getIntent().getExtras();
CharSequence title = bundle.getString("name");
CharSequence id = bundle.getString("id");
String avatar = bundle.getString("avatarurl");
mChannelTitle = title != null ? title.toString() : null;
mChannelId = id != null ? id.toString() : null;
if (avatar != null) mChannelAvatar = avatar;
if (mChannelTitle.isEmpty() || mChannelId.isEmpty()) {
Intent search = new Intent(this, DetailsActivity.class);
startActivity(search);
Toast.makeText(this, R.string.error_connection_incorrect_response, Toast.LENGTH_LONG).show();
}
this.setTitle(title);
mDbHelper = new DBHelper(this);
//noinspection ConstantConditions
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
mDescriptionView = (TextView) findViewById(R.id.descriptionContent);
mCountOldView = (TextView) findViewById(R.id.oldCount);
mTotalViewsView = (TextView) findViewById(R.id.viewsCount);
mAvatarView = (ImageView) findViewById(R.id.avatarView);
mBannerView = (ImageView) findViewById(R.id.banner);
mCountView = (TickerView) findViewById(R.id.count);
mHandler = new Handler();
getInitialData(this);
final FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
if (mDbHelper.isInFavorites(mChannelId)) {
fab.setImageResource(R.drawable.ic_star_amber_a400_24dp);
} else {
fab.setImageResource(R.drawable.ic_star_grey_400_24dp);
}
}
答案 0 :(得分:0)
根据您的日志文件....问题是您在onCreate中的空对象引用上调用getString。我怀疑在崩溃时,首选项为空。您的代码中的那一行是111吗?引起:java.lang.NullPointerException:尝试调用虚方法' java.lang.String android.os.BaseBundle.getString(java.lang.String)'在it.bjarn.android.myapp.activities.DetailsActivity.onCreate(D etailsActivity.java:111)上的空对象引用上。将呼叫分开并检查偏好是否正常。我在调用' getString'之前,在else语句中输入一个日志,这将验证答案。调用getSharedPreferences时确保您的上下文有效。 (抱歉格式不佳,我在手机上接听电话!)
答案 1 :(得分:0)
Activity的getIntent()将返回创建活动的Intent。如果在按下窗口小部件时已创建活动,您将获得创建活动的原始Intent。它可能会点击上一个小部件的额外内容,或者根本没有。
您应该将活动创建与意图处理分开。请参阅Activity.onNewIntent()回调。
(你的应用程序崩溃的原因可能是
CharSequence title = bundle.getString("name");
当bundle在上述情况下为null时)