我的Android应用程序显示此错误。我正在尝试将Fragment类用于我的listMovieActivity.java
Caused by: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
这是我的listMovieActivity.java文件:
package com.example.moviemanager;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
public class listMovieActivity extends FragmentActivity {
private MovieDetailFragment detailFragment = null;
private MovieListFragment listFragment = null;
private FragmentManager manager;
private int selectedItemIndex = -1;
private String title=null;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listmovie);
manager = getSupportFragmentManager();
detailFragment = (MovieDetailFragment) manager.findFragmentById(R.id.detailmovie_fragment);
listFragment = (MovieListFragment) manager.findFragmentById(R.id.listmovie_fragment);
}
public boolean onCreateOptionsMenu(android.view.Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
//getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
protected void onResume() {
super.onResume();
int orientation = getResources().getConfiguration().orientation;
if (orientation == Configuration.ORIENTATION_LANDSCAPE)
showDetails(selectedItemIndex,title);
}
public void showDetails(int selectedItem, String ttl) {
//Log.d("FRAGMENT", "Selected item " + selectedItem);
selectedItemIndex = selectedItem;
title = ttl;
int orientation = getResources().getConfiguration().orientation;
if (orientation == Configuration.ORIENTATION_LANDSCAPE) {
if (detailFragment != null) {
// update entry
detailFragment.updateDetails(selectedItem,ttl);
}
} else {
// show DetailsActivity
Intent intent = new Intent(this, DetailMovie.class);
intent.putExtra("POSITION", selectedItem);
intent.putExtra("TITLE", ttl);
startActivity(intent);
}
}
}
这是错误代码:
04-03 10:00:16.482 854-854/com.example.moviemanager E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.moviemanager/com.example.moviemanager.listMovieActivity}: android.view.InflateException: Binary XML file line #7: Error inflating class fragment
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
at android.app.ActivityThread.access$600(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5041)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.view.InflateException: Binary XML file line #7: Error inflating class fragment
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:270)
at android.app.Activity.setContentView(Activity.java:1881)
at com.example.moviemanager.listMovieActivity.onCreate(listMovieActivity.java:20)
at android.app.Activity.performCreate(Activity.java:5104)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
at android.app.ActivityThread.access$600(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5041)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
at android.support.v4.app.ListFragment.ensureList(ListFragment.java:344)
at android.support.v4.app.ListFragment.onViewCreated(ListFragment.java:145)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:906)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1082)
at android.support.v4.app.FragmentManagerImpl.addFragment(FragmentManager.java:1184)
at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:285)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:676)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:270)
at android.app.Activity.setContentView(Activity.java:1881)
at com.example.moviemanager.listMovieActivity.onCreate(listMovieActivity.java:20)
at android.app.Activity.performCreate(Activity.java:5104)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
at android.app.ActivityThread.access$600(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5041)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
at dalvik.system.NativeStart.main(Native Method)
04-03 10:00:22.282 979-979/com.example.moviemanager E/Trace﹕ error opening trace file: No such file or directory (2)
我能知道我错过了什么吗?我真的需要专业的帮助。谢谢。
我已更新错误代码
这是我的MovieListFragment.java代码
package com.example.moviemanager;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.Toast;
public class MovieListFragment extends ListFragment {
DatabaseHandler dh = new DatabaseHandler(getActivity());
private static final String ROW_ID = "row_id";
private listMovieActivity parent;
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.displaymoviemenu, null);
parent = (listMovieActivity) getActivity();
return v;
}
private void openDB() {
dh.open();
}
private void closeDB() {
dh.close();
}
private void populateListViewFromDB() {
Cursor cursor = dh.getAllRows();
// Allow activity to manage lifetime of the cursor.
// DEPRECATED! Runs on the UI thread, OK for small/short queries.
getActivity().startManagingCursor(cursor);
// Setup mapping from cursor to view fields:
String[] fromFieldNames = new String[]
{dh.KEY_KEY, dh.KEY_TITLE};
int[] toViewIDs = new int[]
{R.id.txtMovieId, R.id.txtMovieTitleRow};
// Create adapter to may columns of the DB onto elemesnt in the UI.
SimpleCursorAdapter myCursorAdapter;
myCursorAdapter = new SimpleCursorAdapter(
getActivity(), // Context
R.layout.movierow, // Row layout template
cursor, // cursor (set of DB records to map)
fromFieldNames, // DB Column names
toViewIDs // View IDs to put information in
);
// Set the adapter for the list view
ListView movieList = (ListView) getView().findViewById(R.id.listViewMovie);
movieList.setAdapter(myCursorAdapter);
}
@Override
public void onListItemClick(ListView list, View view, int position, long id) {
// TODO: show detail depending on activity
String a=list.getItemAtPosition(position).toString();
System.out.println(a);
parent.showDetails(position,a);
}
private void displayToastForId(long idInDB) {
Cursor cursor = dh.getRow(idInDB);
if (cursor.moveToFirst()) {
long idDB = cursor.getLong(dh.COL_ROWID);
String mvkey = cursor.getString(dh.COL_KEY);
String mvtitle = cursor.getString(dh.COL_TITLE);
//String favColour = cursor.getString(DBAdapter.COL_FAVCOLOUR);
String message = "ID: " + idDB + "\n"
+ "Name: " + mvtitle + "\n"
+ "Key#: " + mvkey;
//+ "FavColour: " + favColour;
Toast.makeText(getActivity(), message, Toast.LENGTH_LONG).show();
}
cursor.close();
}
}
这是我的displaymoviemenu.xml代码:
<?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="match_parent" >
<ListView
android:id="@+id/listViewMovie"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:clickable="false" >
</ListView>
</RelativeLayout>
答案 0 :(得分:1)
错误很清楚。 documentation for ListFragment明确指出:
...如果您愿意,可以通过返回自定义片段布局 你自己的视图层次结构来自onCreateView(LayoutInflater,ViewGroup, 束)。为此,视图层次结构必须包含ListView id为“@android:id / list”的对象(如果代码在列表中则列出)
显然,您的片段从onCreateView()
返回的视图层次结构不包含ID为ListView
的{{1}}。
编辑:添加详细信息
在@android:id/list
更改
displaymoviemenu.xml
到
android:id="@+id/listViewMovie"
答案 1 :(得分:0)
将xml
布局修改为:
<?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="match_parent" >
<ListView
android:id="@id/android:list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:clickable="false" >
</ListView>
</RelativeLayout>