Android 奇怪的错误:在外部存储器中创建文件会将资源id变为无效id
我有一些活动,其中一个创建某种序列化为文件的对象,另一个活动应该在数据目录中显示序列化对象的列表(并且有一个“创建新”按钮,用于启动创建新对象的活动) 当目录为空时,所有工作正常,没有崩溃。实际上,目录不必为空。应该显示的文件都以一个特定的扩展名结束,主应用程序会将它们过滤掉。如果我更改文件的扩展名,应用程序不会崩溃 当我创建其中一个对象时,该对象会正确地保存到Android 奇怪的错误:在外部存储器中创建文件会将资源id变为无效id,android,android-resources,Android,Android Resources,我有一些活动,其中一个创建某种序列化为文件的对象,另一个活动应该在数据目录中显示序列化对象的列表(并且有一个“创建新”按钮,用于启动创建新对象的活动) 当目录为空时,所有工作正常,没有崩溃。实际上,目录不必为空。应该显示的文件都以一个特定的扩展名结束,主应用程序会将它们过滤掉。如果我更改文件的扩展名,应用程序不会崩溃 当我创建其中一个对象时,该对象会正确地保存到/sdcard/data/org.my.package/files/the_object_file中,但此时,如果我打开显示序列化对象列
/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);
}
}
<?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" />
objects.xml
布局文件如下:
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);
}
}
<?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" />
请注意,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如下所示:
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);
}
}
<?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>
我得说它在梅因。您还可以在R.java中查看什么是#0x7f050002。@njzk2我更新了我的问题。如果是listfragment,则需要在it@njzk2我从来没有提到过
列表片段,所以它不是。它是一个普通的片段
,其中有一个列表视图
。当我有时间时,我会尝试使用ListFragment
,但我还是想理解这个错误。我假设布局中存在@android:id/empty