Android小部件ListView项目点击不起作用
Posted
技术标签:
【中文标题】Android小部件ListView项目点击不起作用【英文标题】:Android widget ListView item clicks not working 【发布时间】:2019-01-13 09:32:29 【问题描述】:所以我试图设置一个带有列表的小部件,但点击不起作用,所以我知道要做到这一点,我需要一个模板待定意图,因为单个项目不能有自己的,然后添加到待定意图使用填充意图的模板,我已将所有这些添加到我的应用程序中,但它仍然无法正常工作,我遵循了建议 here 和 here 但是当我点击一个项目时,它刚刚突出显示它有注册了一次点击,但我确实得到了这个 日志猫
08-06 17:02:08.001 2163-3666/? W/Binder: Binder call failed.
java.lang.NullPointerException: Attempt to read from field
'com.android.server.appwidget.AppWidgetServiceImpl$ProviderId
com.android.server.appwidget.AppWidgetServiceImpl$Provider.id'
on a null object reference
at
com.android.server.appwidget.AppWidgetServiceImpl$AppWidgetManagerLocal
.getHostedWidgetPackages(AppWidgetServiceImpl.java:4800)
at
com.android.server.accessibility.AccessibilityManagerService$SecurityPolicy
.resolveValidReportedPackageLocked(AccessibilityManagerService.java:4637)
at com.android.server.accessibility.AccessibilityManagerService
.sendAccessibilityEvent(AccessibilityManagerService.java:531)
at android.view.accessibility.IAccessibilityManager$Stub
.onTransact(IAccessibilityManager.java:71)
at android.os.Binder.execTransact(Binder.java:690)
这是我的代码,我的 onUpdate 方法调用了一个静态更新小部件方法,该方法定义了我的模板待处理意图,以及我的 onReceive 方法
AppWidgetProvider
public class MessageMeAppWidget extends AppWidgetProvider
private static final String TAG = "MsgMeWdgt";
@Override
public void onReceive(Context context, Intent intent)
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
if (intent.getAction().equals(Constants.LAUNCH_ACTIVITY))
int appWidgetId = intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,
AppWidgetManager.INVALID_APPWIDGET_ID);
//String recipientId = intent.getStringExtra(Constants.FS_ID);
//String recipientName = intent.getStringExtra(Constants.FS_NAME);
Intent i = new Intent(context, MessageListActivity.class);
Bundle b = new Bundle();
//b.putString(Constants.FS_ID, recipientId);
//b.putString(Constants.FS_ID, recipientName);
i.putExtras(b);
Bundle bundle = intent.getExtras();
String toastText = "position is " + bundle.getInt(Constants.LAUNCH_ACTIVITY);
Toast.makeText(context,toastText,Toast.LENGTH_SHORT).show();
//context.startActivity(i);
else if(intent.getAction().equals(Constants.DONT_LAUNCH_ACTIVITY))
//Toast.makeText(context,"",Toast.LENGTH_SHORT).show();
super.onReceive(context, intent);
public static void updateAppWidget(Context context, AppWidgetManager appWidgetManager,
int appWidgetId)
SharedPreferences sharedPreferences = context.getSharedPreferences(Constants.SHARED_PREFS,Context.MODE_PRIVATE);
boolean userSignedIn = sharedPreferences.getBoolean(Constants.SIGNED_IN, false);
if (userSignedIn)
//move everything in here after i know its working
Intent intent = new Intent(context, MessagingService.class);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
intent.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME)));
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_list_view);
//populates data
remoteViews.setRemoteAdapter(R.id.widget_list,intent);
//remoteViews.setRemoteAdapter(appWidgetId, R.id.widget_list, intent);
remoteViews.setEmptyView(R.id.widget_list,R.id.empty);
Intent launchIntent = new Intent(context, WidgetAdapter.class);
launchIntent.setAction(Constants.LAUNCH_ACTIVITY);
launchIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
intent.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME)));
PendingIntent launchPendingIntent = PendingIntent.getBroadcast(context, 0, launchIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
remoteViews.setPendingIntentTemplate(R.id.widget_list, launchPendingIntent);
appWidgetManager.updateAppWidget(appWidgetId, remoteViews);
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds)
// There may be multiple widgets active, so update all of them
Log.d(TAG,"on update");
for (int appWidgetId : appWidgetIds)
updateAppWidget(context, appWidgetManager, appWidgetId);
那我的widget服务是标准的
public class MessagingService extends RemoteViewsService
private String TAG = "MsgSrvc";
@Override
public RemoteViewsFactory onGetViewFactory(Intent intent)
Log.d(TAG,"item clicked");
return new WidgetAdapter(this.getApplicationContext(), intent);
然后在我的小部件适配器的 getViewAt 方法中,我像这样填写我的填充意图
Bundle extras = new Bundle();
extras.putInt(Constants.LAUNCH_ACTIVITY, position);
Intent fillInIntent = new Intent();
fillInIntent.putExtras(extras);
remoteViews.setOnClickFillInIntent(R.id.holder, fillInIntent);
在我的清单中,我正在注册我的小部件服务和接收器
<receiver android:name="com.sealstudios.aimessage.MessageMeAppWidget">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<meta-data
android:name="android.appwidget.provider"
android:resource="@xml/message_me_app_widget_info" />
</receiver>
<service android:name=".MessagingService"
android:permission="android.permission.BIND_REMOTEVIEWS" />
任何帮助表示赞赏
【问题讨论】:
【参考方案1】:我解决了这个问题,因为意图指向了错误的类,所以
Intent launchIntent = new Intent(context, WidgetAdapter.class);
应该是
Intent launchIntent = new Intent(context, MessageMeAppWidget.class);
【讨论】:
以上是关于Android小部件ListView项目点击不起作用的主要内容,如果未能解决你的问题,请参考以下文章
我的Android进阶之旅------>Android中ListView中嵌套(ListView)控件时item的点击事件不起作的问题解决方法
带有 ListView 的 Android 可点击小部件在 ListItems 上不可点击