我有一个小部件应用程序,没有问题
Android Lollipop或较低的操作系统.当我将nexus 5升级到Marshmallow(android 6.0)时,我意识到我的小部件在加载自定义行列表视图时遇到问题. Widget有一个动态textView和一个动态listView. textView工作得很好;但是listView行项目没有希望.当我添加小部件时,行处于加载阶段.它与Lollipop或更低版本的设备完全相同,但不适用于Marshmallow.我相信棉花糖正在引发一个我无法找到它的问题.我希望有人告诉我我做错了什么并帮助我解决这个问题.我很感激所有的帮助.谢谢.
WidgetProvider.class:
@Override public void onUpdate(Context context,AppWidgetManager appWidgetManager,int[] appWidgetIds) { System.out.println("WidgetProvider.onUpdate()"); SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); final boolean isMaviKart = MainUtils.getCardType(context).equals(Constants.CARD_TYPE_MAVI); try { //set widget id ComponentName thisWidget = new ComponentName(context,WidgetProvider.class); int[] allWidgetIds = appWidgetManager.getAppWidgetIds(thisWidget); MainUtils.setWidgetID(prefs,allWidgetIds[allWidgetIds.length - 1]); for (int i = 0; i < appWidgetIds.length; i++) { Intent intentService = new Intent(context,WidgetService.class); intentService.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,appWidgetIds[i]); intentService.setData(Uri.parse(intentService.toUri(Intent.URI_INTENT_SCHEME))); RemoteViews widget = new RemoteViews(context.getPackageName(),R.layout.widget); widget.setRemoteAdapter(appWidgetIds[i],R.id.list_view,intentService); Intent clickIntent = new Intent(context,MainActivity.class); PendingIntent clickPI = PendingIntent.getActivity(context,clickIntent,PendingIntent.FLAG_UPDATE_CURRENT); widget.setPendingIntentTemplate(R.id.list_view,clickPI); if (isMaviKart) { widget.setTextViewText(R.id.kalanPara,String.valueOf(MainUtils.getTokenPass(prefs))); widget.setTextViewText(R.id.currency_tl," Geçiş"); } else { widget.setTextViewText(R.id.kalanPara,String.valueOf(MainUtils.getTotalMoney(prefs))); widget.setTextViewText(R.id.currency_tl," TL"); } appWidgetManager.updateAppWidget(appWidgetIds[i],widget); } } catch (Exception e) { e.printStackTrace(); Toast.makeText(context,context.getResources().getString(R.string.error_occured),Toast.LENGTH_SHORT).show(); } super.onUpdate(context,appWidgetManager,appWidgetIds); } @Override public void onReceive(Context context,Intent intent) { super.onReceive(context,intent); System.out.println("WidgetProvider.onReceive()"); String key = intent.getStringExtra(Constants.EXTRA); if (key != null) Log.e("TEST",key); }
WidgetViewsFactory.class:
@Override public RemoteViews getViewAt(int position) { Intent intent = new Intent(); Bundle extras = new Bundle(); rowButton = context.getResources().getIdentifier("widget_button","id",context.getPackageName()); rowText = context.getResources().getIdentifier("widget_textview",context.getPackageName()); RemoteViews row = new RemoteViews(context.getPackageName(),R.layout.row); // set BUTTON NAMES row.setTextViewText(rowButton,getFeeTypes(position)); // simple rows: configureRowsInListview(row,position,context.getResources().getColor(R.color.DeepSkyBlue),View.VISIBLE); extras.putString(Constants.EXTRA,getEventKey(position)); intent.putExtras(extras); row.setOnClickFillInIntent(rowButton,intent); row.setOnClickFillInIntent(rowText,intent); return row; }
WidgetService.class:
public class WidgetService extends RemoteViewsService { @Override public RemoteViewsFactory onGetViewFactory(Intent intent) { return(new WidgetViewsFactory(this.getApplicationContext())); } }
MainActivity.class :(关键字在Marshmallow上返回null)
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); prefs = PreferenceManager.getDefaultSharedPreferences(this); String keyWord = getIntent().getStringExtra(Constants.EXTRA); if (keyWord == null) { keyWord = "null"; Log.e("onCreate() ","No data Arrived yet!"); } } private void updateWidgetScreen(String updateData,String currencyType) { AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(this); RemoteViews remoteViews = new RemoteViews(getPackageName(),R.layout.widget); ComponentName thisWidget = new ComponentName(this,WidgetProvider.class); remoteViews.setTextViewText(R.id.kalanPara,updateData); remoteViews.setTextViewText(R.id.currency_tl,currencyType); Intent intent = new Intent(this,WidgetService.class); intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,MainUtils.getWidgetID(prefs)); intent.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME))); remoteViews.setRemoteAdapter(MainUtils.getWidgetID(prefs),intent); Intent clickIntent = new Intent(this,MainActivity.class); PendingIntent clickPI = PendingIntent.getActivity(this,PendingIntent.FLAG_UPDATE_CURRENT); remoteViews.setPendingIntentTemplate(R.id.list_view,clickPI); appWidgetManager.updateAppWidget(thisWidget,remoteViews); Log.e("updateWidgetScreen",updateData+" updateData - widgetid: "+ MainUtils.getWidgetID(prefs)); }
解决方法
我发现了我犯的错误.我有一个带有按钮的自定义列表视图,其中包含样式android:style / Widget.Material.Button.当我删除Button的样式时,它解决了我的问题.
<Button android:id="@+id/widget_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:text="Button" android:layout_marginRight="10dp" android:gravity="center" android:padding="8dp" android:textColor="@color/White" **style="@style/MaterialButton"** /> <style name="MaterialButton" parent="android:style/Widget.Material.Button"> <item name="android:background">@drawable/raised_button_background</item> </style>