2012-08-02 372 views
0

我做了一個android應用程序,其中有一個文本和第一個活動按鈕,當我點擊按鈕它應該去第二個活動,並在網格視圖中查看圖像,但它沒有這樣做,它給了消息意外停止。這是代碼。android應用程序崩潰

mainactivity:

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> 

gameactivity:

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

logcat顯示你什麼?你可以在這裏發佈logcat嗎? – Vinay 2012-08-02 05:37:02

+0

@ karan421你有沒有在清單文件中提到這個活動? – Akshay 2012-08-02 05:37:07

+0

您似乎沒有在主要活動按鈕上添加onClickListener。 – MrYanDao 2012-08-02 05:39:37

回答

1

錯誤:U必須的setContentView(R.layout.gameactivity)首先,其次要使用findViewById(R.id.grid_view)!

+0

你能告訴我你是怎麼發現這個錯誤(假設你要使用logcat的細節) – karan421 2012-08-02 06:47:39

2
  1. 這樣

    Intent intent = new Intent(MainActivity.this, GameActivity.class); 
    startActivity(intent); 
    
  2. ,後爲更好地瞭解您的錯誤日誌消息創建意圖。

  3. 你沒有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)); 
        } 
    } 
    
+0

+1與答案 – Android 2012-08-02 06:16:53

+0

@Rajareddy充分說明我已經加入的logcat ....... – karan421 2012-08-02 06:36:34

+0

你能告訴我gameactivity第20行,並看看我的答案,並按照這些更改,你將解決你的問題.. – 2012-08-02 06:40:53

1

提及任何佈局,你應該調用超在ImageAdapter構造否則你無法初始化BaseAdapter。

+0

我應該在哪裏打電話超級.... – karan421 2012-08-02 14:05:02

+0

public ImageAdapter(Context c){ mContext = c; } – FrancescoAzzola 2012-08-02 19:42:32