GridView崩溃

时间:2012-08-07 10:20:09

标签: android android-gridview

我想创建一个GridView并用数字填充它。我创建了我的GridView,然后我填充了它,然后我成功构建了它,但是当我运行它时,应用程序崩溃了,我收到了以下错误“不幸的是,......已经停止了。”

布局:

<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" >

    <GridLayout
    android:id="@+id/gridView1"
    android:numColumns="auto_fit"
    android:gravity="center"
    android:columnWidth="50dp"
    android:stretchMode="columnWidth"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" > 
    </GridLayout>

</RelativeLayout>

main.java

package main.java;

import android.os.Bundle;
import android.app.Activity;
import android.view.*;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.GridView;
import android.widget.TextView;
import android.widget.Toast;


public class MainActivity extends Activity {

    static final String[] numbers = new String[] { 
        "A", "B", "C", "D", "E",
        "F", "G", "H", "I", "J",
        "K", "L", "M", "N", "O",
        "P", "Q", "R", "S", "T",
        "U", "V", "W", "X", "Y", "Z"};



    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);



        GridView g = (GridView) findViewById(R.id.gridView1);
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1 , numbers);
        g.setAdapter(adapter);

        g.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View v,
                int position, long id) {
               Toast.makeText(getApplicationContext(),
                ((TextView) v).getText(), Toast.LENGTH_SHORT).show();
            }

        });

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }


}

我复制了教程here

以下是日志:

08-07 11:36:37.858: E/Trace(614): error opening trace file: No such file or directory (2)
08-07 11:36:38.587: D/AndroidRuntime(614): Shutting down VM
08-07 11:36:38.587: W/dalvikvm(614): threadid=1: thread exiting with uncaught exception (group=0x40a13300)
08-07 11:36:38.597: E/AndroidRuntime(614): FATAL EXCEPTION: main
08-07 11:36:38.597: E/AndroidRuntime(614): java.lang.RuntimeException: Unable to start activity ComponentInfo{main.java/main.java.MainActivity}: java.lang.ClassCastException: android.widget.GridLayout cannot be cast to android.widget.GridView
08-07 11:36:38.597: E/AndroidRuntime(614):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
08-07 11:36:38.597: E/AndroidRuntime(614):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
08-07 11:36:38.597: E/AndroidRuntime(614):  at android.app.ActivityThread.access$600(ActivityThread.java:130)
08-07 11:36:38.597: E/AndroidRuntime(614):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
08-07 11:36:38.597: E/AndroidRuntime(614):  at android.os.Handler.dispatchMessage(Handler.java:99)
08-07 11:36:38.597: E/AndroidRuntime(614):  at android.os.Looper.loop(Looper.java:137)
08-07 11:36:38.597: E/AndroidRuntime(614):  at android.app.ActivityThread.main(ActivityThread.java:4745)
08-07 11:36:38.597: E/AndroidRuntime(614):  at java.lang.reflect.Method.invokeNative(Native Method)
08-07 11:36:38.597: E/AndroidRuntime(614):  at java.lang.reflect.Method.invoke(Method.java:511)
08-07 11:36:38.597: E/AndroidRuntime(614):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
08-07 11:36:38.597: E/AndroidRuntime(614):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
08-07 11:36:38.597: E/AndroidRuntime(614):  at dalvik.system.NativeStart.main(Native Method)
08-07 11:36:38.597: E/AndroidRuntime(614): Caused by: java.lang.ClassCastException: android.widget.GridLayout cannot be cast to android.widget.GridView
08-07 11:36:38.597: E/AndroidRuntime(614):  at main.java.MainActivity.onCreate(MainActivity.java:32)
08-07 11:36:38.597: E/AndroidRuntime(614):  at android.app.Activity.performCreate(Activity.java:5008)
08-07 11:36:38.597: E/AndroidRuntime(614):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
08-07 11:36:38.597: E/AndroidRuntime(614):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
08-07 11:36:38.597: E/AndroidRuntime(614):  ... 11 more
08-07 11:36:42.507: I/Process(614): Sending signal. PID: 614 SIG: 9

1 个答案:

答案 0 :(得分:14)

  

引起:java.lang.ClassCastException:android.widget.GridLayout无法强制转换为android.widget.GridView

您在XML中声明GridLayout,但请将其(尝试将其转换为)GridView

此:

<GridLayout
android:id="@+id/gridView1"
android:numColumns="auto_fit"
android:gravity="center"
android:columnWidth="50dp"
android:stretchMode="columnWidth"
android:layout_width="fill_parent"
android:layout_height="fill_parent" > 
</GridLayout>

应该是:

<GridView
android:id="@+id/gridView1"
android:numColumns="auto_fit"
android:gravity="center"
android:columnWidth="50dp"
android:stretchMode="columnWidth"
android:layout_width="fill_parent"
android:layout_height="fill_parent" > 
</GridView>