主要活动中的广播接收器
public class SyncBroadcastReceiver extends BroadcastReceiver {
//giving a unique action name
public static final String broadcastAction = "com.dastecsolution.zoos.contentsync.BROADCAST";
@Override
public void onReceive(Context context, Intent intent) {
String msg = intent.getStringExtra("key") + ": " + intent.getStringExtra("value");
intentFilter = new IntentFilter();
intentFilter.addAction(intent.getAction());
//Add it to the list or do whatever you wish to
ListView syncListView = (ListView) findViewById(R.id.syncLogListView); //it becomes null
ArrayList<String> bodyarr = new ArrayList<String>();
bodyarr.add(msg);
ArrayAdapter<String> arrayAdpt = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1,android.R.id.text1, bodyarr);
try{
syncListView.setAdapter(arrayAdpt); // null pointer exception here
}
catch(Exception e)
{
Log.d("error",e.getMessage());
}
// Toast.makeText(context, msg, Toast.LENGTH_LONG).show();
}
布局代码
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.dastecsolution.zoos.SyncActivity">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/LeftSyncMain"
android:layout_alignParentEnd="true">
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="@+id/LeftSyncMenu"
android:gravity="center_vertical">
<Button
android:layout_width="150dp"
android:layout_height="60dp"
android:text="Sync Content"
android:id="@+id/syncContentBtn"
android:layout_gravity="right" />
<Button
android:layout_width="150dp"
android:layout_height="60dp"
android:text="Sync Images"
android:id="@+id/syncImagesBtn"
android:layout_gravity="left"/>
</LinearLayout>
<LinearLayout
android:id="@+id/syncLogLinearLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="16dp">
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/syncLogListView" />
</LinearLayout>
</LinearLayout>
</RelativeLayout>
日志
12-09 10:53:19.199 14412-14412/? I/art: Late-enabling -Xcheck:jni
12-09 10:53:19.219 14412-14412/com.manish.package D/TidaProvider: TidaProvider()
12-09 10:53:19.279 14412-14412/com.manish.package W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
12-09 10:53:20.099 14412-14412/com.manish.package E/SQLiteLog: (1) no such column: season_year
12-09 10:53:20.129 14412-14412/com.manish.package I/ViewRootImpl: CPU Rendering VSync enable = true
12-09 10:53:20.139 14412-14412/com.manish.package D/ActivityThreadInjector: clearCachedDrawables.
12-09 10:53:20.619 14412-14426/com.manish.package W/SQLiteConnectionPool: A SQLiteConnection object for database '/storage/emulated/0/DB.db' was leaked! Please fix your application to end transactions in progress properly and to close the database when it is no longer needed.
12-09 10:53:20.619 14412-14426/com.manish.package W/SQLiteConnectionPool: A SQLiteConnection object for database '/storage/emulated/0/DB.db' was leaked! Please fix your application to end transactions in progress properly and to close the database when it is no longer needed.
12-09 10:53:21.029 14412-14412/com.manish.package I/Choreographer: Skipped 52 frames! The application may be doing too much work on its main thread.
12-09 10:53:21.149 14412-14412/com.manish.package I/Timeline: Timeline: Activity_idle id: android.os.BinderProxy@224b06a7 time:6197910
大家好我是android新手。无法找到,为什么我的应用程序在设置ListView适配器时崩溃。但Toast正在使用相同的代码块。
也无法在日志中找到任何错误。想要通过广播接收器
设置此ListView和同步日志答案 0 :(得分:0)
BroadcastReceiver是一个响应系统范围广播公告的Android应用程序组件。想象一下,外部电源与设备连接/断开,屏幕开启/关闭,电池电量不足或图像捕获等事件。所有这些事件都源于该系统。
您必须使用片段活动活动来显示列表并将适配器设置为该列表。