0

我制作了一个 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);

}

}

主要的.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));
}

}

游戏活动.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;
    }

    }

日志猫:

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
4

3 回答 3

2
  1. create intent like this

    Intent intent = new Intent(MainActivity.this, GameActivity.class);
    startActivity(intent);
    
  2. and post log message for better understanding of your error.

  3. you did not mention any layout in GameActivity

    public class GameActivity extends Activity {
    
    ImageView image;
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.gameactivity)
    
       GridView  gridView = (GridView)findViewById(R.id.grid_view);       
       gridView.setAdapter(new ImageAdapter(this));
      }
    }
    
于 2012-08-02T05:51:10.330 回答
1

您应该在 ImageAdapter 构造函数中调用 super,否则您无法初始化 BaseAdapter。

于 2012-08-02T12:36:08.847 回答
1

错误:你必须先设置ContentView(R.layout.gameactivity),然后才能使用findViewById(R.id.grid_view)!

于 2012-08-02T05:40:42.387 回答