我有一些活动,其中一个创建某种被序列化为文件的对象,另一个活动应该在数据目录中显示序列化对象的列表(并且有一个“创建新”按钮,用于启动活动创建一个新对象)
当目录为空时,一切正常,没有崩溃。实际上,该目录不必为空。应该显示的文件都以特定的扩展名结尾,主应用程序会将它们过滤掉。如果我更改文件的扩展名,应用程序不会崩溃。
当我创建其中一个对象时,该对象已正确保存到其中/sdcard/data/org.my.package/files/the_object_file
,但此时,如果我打开应该显示序列化对象列表的活动,我会得到一个android.content.res.Resources$NotFoundException
.
我还必须说这些活动使用片段,并且异常发生在一段代码中,如下所示:
public class ShowObjectsActivity extends FragmentActivity {
public static final int SHOW_CREATE_OBJECT_ACTIVITY = 3;
public static final int CREATE_OBJECT = RESULT_FIRST_USER + 1;
public static final int LOAD_OBJECT = RESULT_FIRST_USER + 2;
public static final String THE_OBJECT = "org.my.package.THE_OBJECT";
private MainObjectsFragment mainFragment = null;
private String[] objectNames = null;
private ObjectSerializer serializer = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.objects);
if (mainFragment == null) {
mainFragment = new MainObjectsFragment();
// Fails on the following .add
getSupportFragmentManager().beginTransaction()
.add(R.id.objects_fragment_container, mainFragment).commit();
}
Intent intent = getIntent();
serializer = new ObjectSerializer(
intent.getStringExtra(TheMainActivity.APP_FILES_DIRECTORY));
this.objectNames = intent.getStringArrayExtra(
TheMainActivity.OBJECTS_NAMES);
if (this.objectNames == null) {
this.objectNames = new String[0];
}
}
@Override
public void onStart() {
super.onStart();
// Set the ArrayAdapter for the ListView
mainFragment.setObjectsAdapter(this,
this.objectNames);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
if (requestCode == SHOW_CREATE_OBJECT_ACTIVITY) {
if (resultCode == RESULT_OK) {
setResult(CREATE_OBJECT, intent);
finish();
}
}
}
public void createNewObject(View view) {
Intent intent = new Intent(this, NewObjectActivity.class);
startActivityForResult(intent, SHOW_CREATE_OBJECT_ACTIVITY);
}
}
objects.xml
布局文件是这个:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/objects_fragment_container"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent" />
的方法TheMainActivity
,它启动应该显示对象列表的活动,如下所示:
public void showObjectsActivity(View view) {
if (!isExternalStorageAvailable()) {
AlertDialog.Builder dialog = new AlertDialog.Builder(this);
dialog.setTitle(R.string.external_storage_not_available_title);
dialog.setMessage(R.string.external_storage_not_available_msg);
dialog.setNeutralButton(R.string.close, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// Empty
}
});
dialog.create().show();
} else {
Intent intent = new Intent(this, ShowObjectsActivity.class);
String app_files_directory = getTheFilesDir();
// When the file is in the directory the following is the array I'd expect.
// an array with the names of the objects
intent.putExtra(OBJECTS_NAMES,
new ObjectSerializer(
app_files_directory).listObjectNames());
intent.putExtra(APP_FILES_DIRECTORY, app_files_directory);
startActivityForResult(intent, SHOW_OBJECTS);
}
}
请注意,MainObjectsFragment
onCreateView
不做任何与名称数组相关的事情。此外,在完全触及意图之前ShowObjectsActivity
发生的崩溃。
这是回溯:
E/AndroidRuntime( 1186): FATAL EXCEPTION: main
E/AndroidRuntime( 1186): android.content.res.Resources$NotFoundException: Resource ID #0x7f050002 type #0x12 is not valid
E/AndroidRuntime( 1186): at android.content.res.Resources.loadXmlResourceParser(Resources.java:2297)
E/AndroidRuntime( 1186): at android.content.res.Resources.getLayout(Resources.java:988)
E/AndroidRuntime( 1186): at android.view.LayoutInflater.inflate(LayoutInflater.java:349)
E/AndroidRuntime( 1186): at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:363)
E/AndroidRuntime( 1186): at android.widget.ArrayAdapter.getView(ArrayAdapter.java:354)
E/AndroidRuntime( 1186): at android.widget.AbsListView.obtainView(AbsListView.java:1523)
E/AndroidRuntime( 1186): at android.widget.ListView.makeAndAddView(ListView.java:1786)
E/AndroidRuntime( 1186): at android.widget.ListView.fillDown(ListView.java:705)
E/AndroidRuntime( 1186): at android.widget.ListView.fillFromTop(ListView.java:762)
E/AndroidRuntime( 1186): at android.widget.ListView.layoutChildren(ListView.java:1639)
E/AndroidRuntime( 1186): at android.widget.AbsListView.onLayout(AbsListView.java:1318)
E/AndroidRuntime( 1186): at android.view.View.layout(View.java:7243)
E/AndroidRuntime( 1186): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1285)
E/AndroidRuntime( 1186): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1161)
E/AndroidRuntime( 1186): at android.widget.LinearLayout.onLayout(LinearLayout.java:1078)
E/AndroidRuntime( 1186): at android.view.View.layout(View.java:7243)
E/AndroidRuntime( 1186): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1285)
E/AndroidRuntime( 1186): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1161)
E/AndroidRuntime( 1186): at android.widget.LinearLayout.onLayout(LinearLayout.java:1078)
E/AndroidRuntime( 1186): at android.view.View.layout(View.java:7243)
E/AndroidRuntime( 1186): at android.widget.FrameLayout.onLayout(FrameLayout.java:369)
E/AndroidRuntime( 1186): at android.view.View.layout(View.java:7243)
E/AndroidRuntime( 1186): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1285)
E/AndroidRuntime( 1186): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1161)
E/AndroidRuntime( 1186): at android.widget.LinearLayout.onLayout(LinearLayout.java:1078)
E/AndroidRuntime( 1186): at android.view.View.layout(View.java:7243)
E/AndroidRuntime( 1186): at android.widget.FrameLayout.onLayout(FrameLayout.java:369)
E/AndroidRuntime( 1186): at android.view.View.layout(View.java:7243)
E/AndroidRuntime( 1186): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1285)
E/AndroidRuntime( 1186): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1161)
E/AndroidRuntime( 1186): at android.widget.LinearLayout.onLayout(LinearLayout.java:1078)
E/AndroidRuntime( 1186): at android.view.View.layout(View.java:7243)
E/AndroidRuntime( 1186): at android.widget.FrameLayout.onLayout(FrameLayout.java:369)
E/AndroidRuntime( 1186): at android.view.View.layout(View.java:7243)
E/AndroidRuntime( 1186): at android.view.ViewRoot.performTraversals(ViewRoot.java:1181)
E/AndroidRuntime( 1186): at android.view.ViewRoot.handleMessage(ViewRoot.java:1906)
E/AndroidRuntime( 1186): at android.os.Handler.dispatchMessage(Handler.java:130)
E/AndroidRuntime( 1186): at android.os.Looper.loop(SourceFile:351)
E/AndroidRuntime( 1186): at android.app.ActivityThread.main(ActivityThread.java:4070)
E/AndroidRuntime( 1186): at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime( 1186): at java.lang.reflect.Method.invoke(Method.java:538)
E/AndroidRuntime( 1186): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:906)
E/AndroidRuntime( 1186): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:664)
E/AndroidRuntime( 1186): at dalvik.system.NativeStart.main(Native Method)
W/ActivityManager( 197): Force finishing activity org.my.package/.ShowObjectsActivity
W/ActivityManager( 197): Force finishing activity org.my.package/.TheMainActivity
难道我做错了什么?为什么数据目录中存在文件会导致ResourceNotFound
错误?
编辑:我想我开始了解错误是如何发生的。的MainObjectsFragment
布局包含一个ListView
. 我提供了一个TextView
来显示视图是否为空,并且 xml 如下所示:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.5">
<ListView
android:id="@+id/main_objects_view"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
<TextView
android:id="@android:id/empty"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="@string/no_objects_available"
android:textSize="22sp"
android:gravity="center" />
</LinearLayout>
<Button
android:id="@+id/new_object_button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/new_object"
android:textSize="18sp"
android:onClick="createNewObject" />
</LinearLayout>
(IntelliJ 声明上面的 XML 有一个未绑定的命名空间''
。所有其他文件看起来完全一样,不会触发警告。我不知道我的文件是否有问题,或者是否是 IntelliJ 中的错误) .
起初我以为错误可能是由于这样一个事实引起的,如果适配器是空的,那么TextView
显示的是而不是ListView
. 但是片段的代码总是搜索ListView
.
我检查了生成的R.java
文件,它确实包含id
具有正确名称的文件。
我在这里也发布了代码MainObjectsFragment
:
public class MainObjectsFragment extends Fragment {
public ListView objectsList;
public Button newObjectButton;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.main_objects_fragment, container, false);
this.objectsList = (ListView) view.findViewById(R.id.main_objects_view);
// Before I only placed this empty TextView via xml. This does not change
// the error at all.
TextView emptyText = new TextView(getActivity());
emptyText.setText(getActivity().getString(R.string.no_objects_available));
emptyText.setTextSize(TypedValue.COMPLEX_UNIT_SP, 22);
emptyText.setGravity(Gravity.CENTER);
this.objectsList.setEmptyView(emptyText);
this.objectsList.setAdapter(
new ArrayAdapter<String>(getActivity(), R.id.main_objects_view,
new String[0])
);
this.newObjectButton = (Button) view.findViewById(R.id.new_object_button);
return view;
}
public void setCharsAdapter(Context ctx, String[] data) {
this.objectsList.setAdapter(
new ArrayAdapter<String>(ctx, R.id.main_objects_view, data));
}
}