最初のアクティビティにテキストとボタンがあり、ボタンをクリックすると2番目のアクティビティに移動してグリッドビューで画像を表示するAndroidアプリケーションを作成しましたが、それを行わないと、予期せず停止したというメッセージが表示されます。これがコードです。
主な活動:
package com.karan.myjigsaw;
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.content.Intent;
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void playGame(View view) {
Intent intent = new Intent(this, GameActivity.class);
startActivity(intent);
}
}
main.xml
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"
android:orientation="vertical">
<TextView
android:layout_margin="120dip"
android:layout_width="200dp"
android:layout_height="150dp"
android:text="@string/welcome_note"
android:layout_centerHorizontal="true"
tools:context=".MainActivity" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button_play"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:onClick = "playGame" />
</RelativeLayout>
ゲームアクティビティ:
public class GameActivity extends Activity {
ImageView image;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = getIntent();
//create grid view
GridView gridView = new GridView(this);
gridView = (GridView)findViewById(R.id.grid_view);
// Instance of ImageAdapter Class
gridView.setAdapter(new ImageAdapter(this));
}
}
gameactivity.xml:
<?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/grid_view"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:numColumns="auto_fit"
android:columnWidth="90dp"
android:horizontalSpacing="10dp"
android:verticalSpacing="10dp"
android:gravity="center"
android:stretchMode="columnWidth" >
</GridView>
イメージビューアダプタクラス
public class ImageAdapter extends BaseAdapter{
private Context mContext;
// references to our images
private Integer[] mThumbIds = {R.drawable.imagesl_01,R.drawable.imagesl_02,
R.drawable.imagesl_03,R.drawable.imagesl_04,R.drawable.imagesl_05,
R.drawable.imagesl_06,R.drawable.imagesl_07,R.drawable.imagesl_8,
R.drawable.imagesl_9};
public ImageAdapter(Context c) {
mContext = c;
}
public int getCount() {
return mThumbIds.length;
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return 0;
}
// create a new ImageView for each item referenced by the Adapter
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null) { // if it's not recycled, initialize some attributes
imageView = new ImageView(mContext);
imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(8, 8, 8, 8);
} else {
imageView = (ImageView) convertView;
}
imageView.setImageResource(mThumbIds[position]);
return imageView;
}
}
logcat:
08-02 00:24:30.160: D/AndroidRuntime(286): Shutting down VM
08-02 00:24:30.160: W/dalvikvm(286): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
08-02 00:24:30.190: E/AndroidRuntime(286): FATAL EXCEPTION: main
08-02 00:24:30.190: E/AndroidRuntime(286): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.bellurbis.karan.myjigsaw/com.bellurbis.karan.myjigsaw.GameActivity}: java.lang.NullPointerException
08-02 00:24:30.190: E/AndroidRuntime(286): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
08-02 00:24:30.190: E/AndroidRuntime(286): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
08-02 00:24:30.190: E/AndroidRuntime(286): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
08-02 00:24:30.190: E/AndroidRuntime(286): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
08-02 00:24:30.190: E/AndroidRuntime(286): at android.os.Handler.dispatchMessage(Handler.java:99)
08-02 00:24:30.190: E/AndroidRuntime(286): at android.os.Looper.loop(Looper.java:123)
08-02 00:24:30.190: E/AndroidRuntime(286): at android.app.ActivityThread.main(ActivityThread.java:4627)
08-02 00:24:30.190: E/AndroidRuntime(286): at java.lang.reflect.Method.invokeNative(Native Method)
08-02 00:24:30.190: E/AndroidRuntime(286): at java.lang.reflect.Method.invoke(Method.java:521)
08-02 00:24:30.190: E/AndroidRuntime(286): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
08-02 00:24:30.190: E/AndroidRuntime(286): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
08-02 00:24:30.190: E/AndroidRuntime(286): at dalvik.system.NativeStart.main(Native Method)
08-02 00:24:30.190: E/AndroidRuntime(286): Caused by: java.lang.NullPointerException
08-02 00:24:30.190: E/AndroidRuntime(286): at com.bellurbis.karan.myjigsaw.GameActivity.onCreate(GameActivity.java:20)
08-02 00:24:30.190: E/AndroidRuntime(286): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
08-02 00:24:30.190: E/AndroidRuntime(286): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
08-02 00:24:30.190: E/AndroidRuntime(286): ... 11 more