Android 为简单列表视图设置适配器时应用程序崩溃
主要活动中的广播接收器Android 为简单列表视图设置适配器时应用程序崩溃,android,listview,Android,Listview,主要活动中的广播接收器 public class SyncBroadcastReceiver extends BroadcastReceiver { //giving a unique action name public static final String broadcastAction = "com.dastecsolution.zoos.contentsync.BROADCAST"; @Override public void onRecei
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();
}
大家好,我是android新手。无法找到设置ListView适配器时我的应用程序崩溃的原因。但是,Toast在同一代码块中工作
在日志中也找不到任何错误。要通过广播接收器使用同步日志设置此列表视图广播接收器是一个Android应用程序组件,用于响应系统范围的广播公告。想象一个事件,如外部电源与设备连接/断开、屏幕打开/关闭、电池电量不足或拍摄图片。所有这些事件都源于系统 您必须使用片段活动的活动来显示列表,并将适配器设置为该列表
首先要弄清楚一件事,为什么您没有收到任何错误日志!在安卓显示器后面的搜索框内,是否已经有任何特定的搜索词?如果是,则从搜索框中删除该词,然后在此处删除错误日志,这样我们就可以找出确切的问题。嗨,Radhey。。已更新广播接收器代码。syncListView设置为空。ListView-syncListView=(ListView)findViewById(R.id.syncLogListView)//它变为nullbrother,希望从logcat获得完整堆栈跟踪或错误日志,并进行更新。只需在类级别全局声明您的#ListView syncListView,而不是在广播接收器内部,并引用#syncListView=(ListView)findViewById(R.id.syncLogListView),与#ArrayAdapter arrayAdpt和#ArrayList bodyarr相同。如果仍然出现错误,请将我回复。@Manish KumarNot成功。。。。。
<?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