2

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


}

ここにチュートリアルをコピーしました。

ログは次のとおりです。

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
4

1 に答える 1

15

原因:java.lang.ClassCastException:android.widget.GridLayoutをandroid.widget.GridViewにキャストできません

GridLayoutXMLで宣言していますが、それを処理して(キャストしてみてください)、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>
于 2012-08-07T10:40:48.807 に答える