2014-05-21 98 views
-1

我想在GridViewLayout中顯示一些國家標誌(100x75)。這是logcat的輸出。空指針異常無法啓動活動ComponentInfo {}

05-21 09:46:51.357: E/AndroidRuntime(1304): FATAL EXCEPTION: main 
05-21 09:46:51.357: E/AndroidRuntime(1304): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.gridviewtesting/com.example.gridviewtesting.MainActivity}: java.lang.NullPointerException 
05-21 09:46:51.357: E/AndroidRuntime(1304):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180) 
05-21 09:46:51.357: E/AndroidRuntime(1304):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230) 
05-21 09:46:51.357: E/AndroidRuntime(1304):  at android.app.ActivityThread.access$600(ActivityThread.java:141) 
05-21 09:46:51.357: E/AndroidRuntime(1304):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234) 
05-21 09:46:51.357: E/AndroidRuntime(1304):  at android.os.Handler.dispatchMessage(Handler.java:99) 
05-21 09:46:51.357: E/AndroidRuntime(1304):  at android.os.Looper.loop(Looper.java:137) 
05-21 09:46:51.357: E/AndroidRuntime(1304):  at android.app.ActivityThread.main(ActivityThread.java:5041) 
05-21 09:46:51.357: E/AndroidRuntime(1304):  at java.lang.reflect.Method.invokeNative(Native Method) 
05-21 09:46:51.357: E/AndroidRuntime(1304):  at java.lang.reflect.Method.invoke(Method.java:511) 
05-21 09:46:51.357: E/AndroidRuntime(1304):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 
05-21 09:46:51.357: E/AndroidRuntime(1304):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 
05-21 09:46:51.357: E/AndroidRuntime(1304):  at dalvik.system.NativeStart.main(Native Method) 
05-21 09:46:51.357: E/AndroidRuntime(1304): Caused by: java.lang.NullPointerException 
05-21 09:46:51.357: E/AndroidRuntime(1304):  at com.example.gridviewtesting.MainActivity.onCreate(MainActivity.java:20) 
05-21 09:46:51.357: E/AndroidRuntime(1304):  at android.app.Activity.performCreate(Activity.java:5104) 
05-21 09:46:51.357: E/AndroidRuntime(1304):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080) 
05-21 09:46:51.357: E/AndroidRuntime(1304):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144) 
05-21 09:46:51.357: E/AndroidRuntime(1304):  ... 11 more 

我的GridView項目的xml文件。一個使用ImageAdapter

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:id="@+id/LinearLayout1" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="vertical" 
android:paddingBottom="@dimen/activity_vertical_margin" 
android:paddingLeft="@dimen/activity_horizontal_margin" 
android:paddingRight="@dimen/activity_horizontal_margin" 
android:paddingTop="@dimen/activity_vertical_margin" 
tools:context=".MainActivity" > 

<GridView 
    android:id="@+id/gridView" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:numColumns="auto_fit" 
    android:horizontalSpacing="1dp" 
    android:verticalSpacing="1dp" 
    android:gravity="center" 
    android:columnWidth="90dp" 
    > 
</GridView> 

</LinearLayout> 

MainActivity文件...

public class MainActivity extends Activity { 
GridView gridView; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    gridView.setAdapter(new ImageAdapter(getApplicationContext())); 

} 
    } 

最後的ImageAdapter文件。

public class ImageAdapter extends BaseAdapter { 
int [] images = {R.drawable.belgium,R.drawable.bosnia,R.drawable.croatia, 
     R.drawable.england,R.drawable.france,R.drawable.germany}; 
private Context context; 

public ImageAdapter(Context applicationContext) { 
    // TODO Auto-generated constructor stub 
    context = applicationContext; 
} 

@Override 
public int getCount() { 
    // TODO Auto-generated method stub 
    return images.length; 
} 

@Override 
public Object getItem(int position) { 
    // TODO Auto-generated method stub 
    return null; 
} 

@Override 
public long getItemId(int position) { 
    // TODO Auto-generated method stub 
    return 0; 
} 

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 
    // TODO Auto-generated method stub 
    ImageView iv; 
    if(convertView!=null){ 
     iv = (ImageView)convertView; 
    }else{ 
     iv = new ImageView(context); 
     iv.setLayoutParams(new GridView.LayoutParams(100,75)); 
     iv.setScaleType(ScaleType.CENTER_CROP); 
     iv.setPadding(0,0,8,8); 
    } 
    iv.setImageResource(images[position]); 
    return iv; 
    } 
    } 
+0

在哪一行,你是獲得空指針異常? –

+0

現在已經修復。 – Theo

回答

2

我想你忘了初始化GriView就像你onCreate(...)setContentView(R.layout.activity_main);

griView =(GriView)findViewById(R.id.gridView); 

你有NPE那是因爲你的gridViewNULL在以下位置

gridView.setAdapter(new ImageAdapter(getApplicationContext())); 
+0

謝謝你這位先生。我確實忘了初始化GridView。現在正在工作。祝你今天愉快。 – Theo

+0

@Theo喲歡迎budy! –

相關問題