java - android.view.InflateException 错误

标签 java android android-fragments

我的 android 应用程序显示此错误。我正在尝试为我的 listMovieActivity.java 使用 Fragment 类

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>

最佳答案

错误很明显。 documentation for ListFragment明确指出:

...if you desire, you can customize the fragment layout by returning your own view hierarchy from onCreateView(LayoutInflater, ViewGroup, Bundle). To do this, your view hierarchy must contain a ListView object with the id "@android:id/list" (or list if it's in code)

显然,您的 fragment 从 onCreateView() 返回的 View 层次结构不包含 ID 为 @android:id/listListView >.


编辑:添加详细信息

displaymoviemenu.xml中修改

android:id="@+id/listViewMovie"

android:id="@android:id/list"

关于java - android.view.InflateException 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29430269/

相关文章:

Android:将列添加到已填充的游标

android - addToBackStack 不适用于 onBackPressed 方法

java - 迭代 int 数组

java - 为什么在我的游戏中生命值在与正方形碰撞后立即减少到 0 而它应该减少 1

java - 如果开关不工作

java - 在测试用例中创建构造函数,使其仅被调用一次

android - ActionBarSherlock 4.2 的构建问题

java - Java 中的双重比较失败

java - 从 `startActivityForResult` 内以 `onClickListener` 开始的 Android Activity 返回字符串返回 null

android - 在两个 ViewPager 小部件的连接处添加 fab Button