空指针异常无法启动活动ComponentInfo {}

时间:2014-05-21 10:15:04

标签: android gridview nullpointerexception runtimeexception

我正在尝试在GridViewLayout中显示一些国家/地区标志(100x75)。这是logcat的输出。

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文件。

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

使用ImageAdapter的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;
  }
   }

1 个答案:

答案 0 :(得分:2)

我认为您忘记在GriView

之后的onCreate(...)中初始化setContentView(R.layout.activity_main);
    griView =(GriView)findViewById(R.id.gridView);

您已NPE因为gridView位于NULL以下位置

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