2012-08-07 68 views
2

我想創建一個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 
+0

任何錯誤日誌顯示? – Qw4z1 2012-08-07 10:24:40

+0

提供logcat的錯誤日誌 – 2012-08-07 10:25:53

回答

13

造成的:java.lang.ClassCastException:android.widget.GridLayout不能轉換到android.widget.GridView

您聲明GridLayout在XML中,但將它對待(試圖將其轉換爲)GridView

This:

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