即使我的代码运行正常并退出onCreate,我也会收到此ResourceNotFound异常。
public class MessageSelectionListActivity extends FragmentActivity implements LoaderManager.LoaderCallbacks<Cursor>{
SimpleCursorAdapter mAdapter;
ListView mListView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_message_selection_list);
// Show the Up button in the action bar.
setupActionBar();
//My code
//Create a temporary loading bar while the Uri loads
ProgressBar progressBar = new ProgressBar(this);
progressBar.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
progressBar.setIndeterminate(true);
mListView = (ListView)findViewById(R.id.smsListView);
mListView.setEmptyView(progressBar);
ViewGroup root = (ViewGroup)findViewById(android.R.id.content);
root.addView(progressBar);
//List item population mechanism
Cursor cursorSms = getContentResolver().query(Uri.parse("content://sms/inbox"), null, null, null, null);
String[] fromColumns = {cursorSms.getColumnName(2), cursorSms.getColumnName(11)};
int[] toViews = {R.id.item_title, R.id.item_body};
this.mAdapter = new SimpleCursorAdapter(this, R.id.smsListView, cursorSms, fromColumns, toViews, 0);
this.mListView.setAdapter(this.mAdapter);
getSupportLoaderManager().initLoader(0, null, this);
}
/**
* Set up the {@link android.app.ActionBar}, if the API is available.
*/
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
private void setupActionBar() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
getActionBar().setDisplayHomeAsUpEnabled(true);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.message_selection_list, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
// This ID represents the Home or Up button. In the case of this
// activity, the Up button is shown. Use NavUtils to allow users
// to navigate up one level in the application structure. For
// more details, see the Navigation pattern on Android Design:
//
// http://developer.android.com/design/patterns/navigation.html#up-vs-back
//
NavUtils.navigateUpFromSameTask(this);
return true;
}
return super.onOptionsItemSelected(item);
}
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
return new CursorLoader(this, Uri.parse("content://sms/inbox/"), null, null, null, null );
}
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
mAdapter.swapCursor(data);
}
public void onLoaderReset(Loader<Cursor> loader) {
mAdapter.swapCursor(null);
}
}
Logcat消息如下:
04-08 13:06:49.699:E / AndroidRuntime(2616):致命异常:主要 04-08 13:06:49.699:E / AndroidRuntime(2616):android.content.res.Resources $ NotFoundException:资源ID#0x7f090006类型#0x12无效 04-08 13:06:49.699:E / AndroidRuntime(2616):在android.content.res.Resources.loadXmlResourceParser(Resources.java:1874) 04-08 13:06:49.699:E / AndroidRuntime(2616):在android.content.res.Resources.getLayout(Resources.java:731) 04-08 13:06:49.699:E / AndroidRuntime(2616):在android.view.LayoutInflater.inflate(LayoutInflater.java:318) 04-08 13:06:49.699:E / AndroidRuntime(2616):在android.support.v4.widget.ResourceCursorAdapter.newView(ResourceCursorAdapter.java:106) 04-08 13:06:49.699:E / AndroidRuntime(2616):在android.support.v4.widget.CursorAdapter.getView(CursorAdapter.java:252) 04-08 13:06:49.699:E / AndroidRuntime(2616):在android.widget.AbsListView.obtainView(AbsListView.java:1536) 04-08 13:06:49.699:E / AndroidRuntime(2616):在android.widget.ListView.makeAndAddView(ListView.java:1793) 04-08 13:06:49.699:E / AndroidRuntime(2616):在android.widget.ListView.fillDown(ListView.java:718) 04-08 13:06:49.699:E / AndroidRuntime(2616):在android.widget.ListView.fillFromTop(ListView.java:775) 04-08 13:06:49.699:E / AndroidRuntime(2616):在android.widget.ListView.layoutChildren(ListView.java:1646) 04-08 13:06:49.699:E / AndroidRuntime(2616):在android.widget.AbsListView.onLayout(AbsListView.java:1366) 04-08 13:06:49.699:E / AndroidRuntime(2616):在android.view.View.layout(View.java:7175) 04-08 13:06:49.699:E / AndroidRuntime(2616):在android.widget.RelativeLayout.onLayout(RelativeLayout.java:912) 04-08 13:06:49.699:E / AndroidRuntime(2616):在android.view.View.layout(View.java:7175) 04-08 13:06:49.699:E / AndroidRuntime(2616):在android.widget.FrameLayout.onLayout(FrameLayout.java:338) 04-08 13:06:49.699:E / AndroidRuntime(2616):在android.view.View.layout(View.java:7175) 04-08 13:06:49.699:E / AndroidRuntime(2616):在android.widget.LinearLayout.setChildFrame(LinearLayout.java:1254) 04-08 13:06:49.699:E / AndroidRuntime(2616):在android.widget.LinearLayout.layoutVertical(LinearLayout.java:1130) 04-08 13:06:49.699:E / AndroidRuntime(2616):在android.widget.LinearLayout.onLayout(LinearLayout.java:1047) 04-08 13:06:49.699:E / AndroidRuntime(2616):在android.view.View.layout(View.java:7175) 04-08 13:06:49.699:E / AndroidRuntime(2616):在android.widget.FrameLayout.onLayout(FrameLayout.java:338) 04-08 13:06:49.699:E / AndroidRuntime(2616):在android.view.View.layout(View.java:7175) 04-08 13:06:49.699:E / AndroidRuntime(2616):在android.view.ViewRoot.performTraversals(ViewRoot.java:1146) 04-08 13:06:49.699:E / AndroidRuntime(2616):在android.view.ViewRoot.handleMessage(ViewRoot.java:1866) 04-08 13:06:49.699:E / AndroidRuntime(2616):在android.os.Handler.dispatchMessage(Handler.java:99) 04-08 13:06:49.699:E / AndroidRuntime(2616):在android.os.Looper.loop(Looper.java:123) 04-08 13:06:49.699:E / AndroidRuntime(2616):在android.app.ActivityThread.main(ActivityThread.java:3687) 04-08 13:06:49.699:E / AndroidRuntime(2616):at java.lang.reflect.Method.invokeNative(Native Method) 04-08 13:06:49.699:E / AndroidRuntime(2616):at java.lang.reflect.Method.invoke(Method.java:507) 04-08 13:06:49.699:E / AndroidRuntime(2616):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:867) 04-08 13:06:49.699:E / AndroidRuntime(2616):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625) 04-08 13:06:49.699:E / AndroidRuntime(2616):at dalvik.system.NativeStart.main(Native Method)
我正在使用来自许多不同来源的代码并将事物串在一起,所以我知道我做错了什么,但我似乎无法找到它。
提前致谢。
更新
活动的资源文件如下:
<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=".MessageSelectionListActivity" >
<TextView
android:id="@+id/smsPageTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/sms_page_title"
android:textSize="20sp" />
<TextView
android:id="@+id/smsPageInstructions"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/smsPageTitle"
android:layout_below="@+id/smsPageTitle"
android:text="@string/sms_page_instructions"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textSize="15sp" />
<Button
android:id="@+id/makeExcelFileButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/smsPageInstructions"
android:layout_alignParentBottom="true"
android:layout_alignRight="@+id/smsPageInstructions"
android:text="@string/make_excel_file" />
<ListView
android:id="@+id/smsListView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/makeExcelFileButton"
android:layout_alignLeft="@+id/makeExcelFileButton"
android:layout_below="@+id/smsPageInstructions" />
行布局如下:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:padding="6dip" >
<CheckBox
android:id="@+id/item_check_box"
android:layout_width="wrap_content"
android:layout_height="fill_parent" />
<TextView
android:id="@+id/item_title"
android:layout_width="fill_parent"
android:layout_height="26dip"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_toRightOf="@id/item_check_box"
android:singleLine="true" />
<TextView
android:id="@+id/item_body"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:layout_toRightOf="@id/item_check_box"
android:layout_below="@id/item_title" />
</RelativeLayout>
答案 0 :(得分:0)
所以我能够解决问题。
这是怎么回事。跟踪中的第一条消息给了我线索。 04-08 13:06:49.699:E / AndroidRuntime(2616):FATAL EXCEPTION:main 04-08 13:06:49.699:E / AndroidRuntime(2616):android.content.res.Resources $ NotFoundException:Resource ID#0x7f090006 #0x12类型无效
ID为0x7f090006的资源无效。因此,当我将其追溯到代码时,我使用了资源ID而不是资源名称(R.layout.XXXXXX)。改变它解决了这个问题。