我正在为我的应用程序使用支持库.在我的FragmentActivity中,我使用AsyncTask从Internet下载数据.在onPreExecute()方法中我添加了一个Fragment,在onPostExecute()方法中我再次删除它.当两者之间的方向发生变化时,我得到了上述异常.请看一下细节:
private class onFriendAddedAsyncTask extends AsyncTask<String, Void, String> {
DummyFragment dummyFragment;
FragmentManager fm;
FragmentTransaction ft;
@Override
protected void onPreExecute() {
Log.v("MyFragmentActivity", "onFriendAddedAsyncTask/onPreExecute");
dummyFragment = DummyFragment.newInstance();
fm = getSupportFragmentManager();
ft = fm.beginTransaction();
ft.add(dummyFragment, "dummy_fragment");
ft.commit();
}
@Override
protected void onPostExecute(String result) {
Log.v("MyFragmentActivity", "onFriendAddedAsyncTask/onPostExecute");
ft = fm.beginTransaction();
ft.remove(dummyFragment);
ft.commit();
}
@Override
protected String doInBackground(String... name) {
Log.v("MyFragmentActivity", "onFriendAddedAsyncTask/doInBackground");
...
}
Run Code Online (Sandbox Code Playgroud)
我得到了以下LogCut:
01-05 23:54:19.958: V/MyFragmentActivity(12783): onFriendAddedAsyncTask/onPreExecute
01-05 23:54:19.968: V/DummyFragment(12783): onAttach
01-05 23:54:19.968: V/DummyFragment(12783): onCreate
01-05 23:54:19.968: V/MyFragmentActivity(12783): onFriendAddedAsyncTask/doInBackground
01-05 23:54:19.973: V/DummyFragment(12783): onCreateView …Run Code Online (Sandbox Code Playgroud) android exception android-asynctask illegalstateexception android-fragments
我尝试创建自定义通知但获得以下异常:
FATAL EXCEPTION: main
android.app.RemoteServiceException: Bad notification posted from package com.my.app: Couldn't expand RemoteViews for: StatusBarNotification(pkg=com.my.app user=UserHandle{0} id=1 tag=null score=0: Notification(pri=0 contentView=com.my.app/0x7f03001b vibrate=null sound=null defaults=0x0 flags=0x2 kind=[null]))
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1423)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5103)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
Run Code Online (Sandbox Code Playgroud)
我的代码看起来像这样:
Intent intent = new Intent(this, MyFragmentActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent launchIntent = PendingIntent.getActivity(this, 0, intent, 0);
RemoteViews notificationView = new RemoteViews(this.getPackageName(), R.layout.notification_layout);
notificationView.setImageViewBitmap(R.id.notification_icon, icon);
notificationView.setTextViewText(R.id.present_text, "Text Test");
Notification.Builder builder = new Notification.Builder(getApplicationContext()); …Run Code Online (Sandbox Code Playgroud) 我通过以下方式构建通知:
NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();
inboxStyle.setBigContentTitle(title);
for (int position = 0; position < onlineCounter; position++) {
inboxStyle.addLine(onlineName.get(position) + " is online now");
}
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(getApplicationContext());
notificationBuilder.setStyle(inboxStyle);
notificationBuilder.setContentTitle(title);
notificationBuilder.setContentText(contentText);
notificationBuilder.setNumber(cursor.getCount());
notificationBuilder.setSmallIcon(R.drawable.ic_stat_notify);
notificationBuilder.setColor(getResources().getColor(R.color.notification_color));
notificationBuilder.setLargeIcon(icon);
notificationBuilder.setContentIntent(launchIntent);
notificationBuilder.setDeleteIntent(clearIntent);
notificationBuilder.setDefaults(property);
notificationBuilder.setAutoCancel(true);
Run Code Online (Sandbox Code Playgroud)
当两行或多行附加到inboxStyle时,通知将展开,并在打开通知抽屉时自动显示所有附加行.

但是,如果仅添加一行,则不会展开通知并且该行不可见.如何使线条自动可见?

notifications android android-notifications android-5.0-lollipop