I have created a widget which has a clickable part which opens an activity showing all details instead of just one detail. But something odd happens.
Let me explain; the widget works perfectly, but when you create a widget, the button is not responding. So, when you create another widget, you will have 2 in total. Now, the button kind of works... it responds to my click but it crashes. If I completely reboot my phone and then use the button on the widget, it succesfully opens?
Here's my 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);
}
}
This is the NullpointerException users are receiving:
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)
Also, helpful to know: When it works, it only opens the activity with the variables from 1 widget, instead of the widget you clicked on. It always opens the same one.
// Edit:
Added the onCreate() class by request. Nothing wrong with this one, since it works when opening the DetailsActivity by clicking on a button in another activity, it's only having issues with the widget.
@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);
}
}