android - ListView 到 Gridview 导致 RuntimeException : Your content must have a ListView whose id attribute is 'android.R.id.list'

标签 android listview gridview android-arrayadapter retrofit

我有一个应用程序,它在运行时在 ListView 中显示图像列表(参见屏幕截图)。现在我想更改 GridView(而不是 ListView),我收到上述错误并且我不确定如何解决这个问题。我认为它不像 setListAdapter 方法,因为它只适用于 ListView 但不适用于 GridView。

截图如下:

enter image description here

我正在使用改造从网络上抓取图像。

//这是它不喜欢的代码:

public void success(MoviesResponse moviesresponse, Response response) {
   movieList = moviesresponse.getResults();

   adapter adapt = new adapter(getApplicationContext(),R.layout.item_file,movieList);

   setListAdapter(adapt);

}

这是我的 activity_main:

<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" tools:context=".MainActivity">

   <GridView
       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:columnWidth="150dp"
       android:numColumns="auto_fit"
       android:verticalSpacing="0dp"
       android:horizontalSpacing="0dp"
       android:stretchMode="columnWidth"
       tools:context=".MainActivityFragment"
       android:id="@+id/gridview_movie">
   </GridView>

</RelativeLayout>

这是堆栈跟踪:

 E/AndroidRuntime: FATAL EXCEPTION: main
                                                                    Process: com.jays.moviesapp, PID: 28134
                                                                    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.jays.moviesapp/com.jays.moviesapp.MainActivity}: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
                                                                        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184)
                                                                        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
                                                                        at android.app.ActivityThread.access$800(ActivityThread.java:135)
                                                                        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
                                                                        at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                        at android.os.Looper.loop(Looper.java:136)
                                                                        at android.app.ActivityThread.main(ActivityThread.java:5001)
                                                                        at java.lang.reflect.Method.invokeNative(Native Method)
                                                                        at java.lang.reflect.Method.invoke(Method.java:515)
                                                                        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
                                                                        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
                                                                        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.app.ListActivity.onContentChanged(ListActivity.java:243)
                                                                        at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:293)
                                                                        at android.app.Activity.setContentView(Activity.java:1929)
                                                                        at com.jays.moviesapp.MainActivity.onCreate(MainActivity.java:32)
                                                                        at android.app.Activity.performCreate(Activity.java:5231)
                                                                        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
                                                                        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)
                                                                        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233) 
                                                                        at android.app.ActivityThread.access$800(ActivityThread.java:135) 
                                                                        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196) 
                                                                        at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                        at android.os.Looper.loop(Looper.java:136) 
                                                                        at android.app.ActivityThread.main(ActivityThread.java:5001) 
                                                                        at java.lang.reflect.Method.invokeNative(Native Method) 
                                                                        at java.lang.reflect.Method.invoke(Method.java:515) 
                                                                        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785) 
                                                                        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601) 
                                                                        at dalvik.system.NativeStart.main(Native Method) 

更新:这是请求的 MainActivity 和适配器

这是完整的 MainActivty:

public class MainActivity extends ListActivity {




    List<Movie> movieList;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        RestAdapter restAdapter = new RestAdapter.Builder()
                .setEndpoint("http://api.themoviedb.org/3")
                .setRequestInterceptor(new RequestInterceptor() {
                    @Override
                    public void intercept(RequestFacade request) {
                        request.addEncodedQueryParam("sort_by", "popularity.des");
                        request.addEncodedQueryParam("api_key","YOURAPIKEY");
                    }
                })
                .setLogLevel(RestAdapter.LogLevel.FULL)
                .build();

        MovieAPI service = restAdapter.create(MovieAPI.class);

        service.getPopularMovies(new Callback<MoviesResponse>() {
            @Override
            public void success(MoviesResponse moviesresponse, Response response) {
                movieList = moviesresponse.getResults();

                movieList = moviesresponse.getResults();

                adapter adapt = new adapter(getApplicationContext(),R.layout.item_file,movieList);

                setListAdapter(adapt);
            }

            @Override
            public void failure(RetrofitError error) {
                Toast.makeText(MainActivity.this, error.getMessage(), Toast.LENGTH_LONG).show();
            }
        });


    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);

        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

最佳答案

问题是您正在使用 ListActivity,即。您的 Activity 扩展了 ListActivity,因此使用 GridView 将不起作用。

相反,您应该使用普通的 Activity,然后使用布局中的 GridView。

在你的 onCreate 中这样的东西应该可以解决问题:

setContentView(R.layout.main);
GridView mGridView = (GridView) findViewById(R.id.gridview_movie);

adapter adapt = new adapter(getApplicationContext(),R.layout.item_file, movieList);
mGridView.setAdapter(adapt);

关于android - ListView 到 Gridview 导致 RuntimeException : Your content must have a ListView whose id attribute is 'android.R.id.list' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34465011/

相关文章:

带有 CheckBoxListCell 的 Javafx ListView 不适用于拖放

c# - ObjectListView 不显示所选项目的选择颜色

android - 在 ChipGroup 中设置 Chip 文本的问题

android - 如何在android中的LinearLayout中居中ListView

Android GridView 将某些项目设置为不可点击

jquery - Rowcommand 没有为 gridview 中的 linkbutton 触发?

asp.net - 如何将表格行添加到 GridView 的标题部分?

java - 在 Android 中更改自定义 View 的宽度和高度

android - 如何在sinch有效负载中发送额外数据

java - 将布局转换为图像,如何获得良好的质量?