我不知道为什么这段代码会给我一个带有gridview的NullPointerException
public class MainActivity extends Activity {
private GridView listaVirus;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Plagas.indicializaBD(this);
BaseAdapter adaptador = new AdaptadorCursorPlagas(this,
Plagas.listado());
listaVirus = (GridView) listaVirus.findViewById(R.id.gridViewMain);
listaVirus.setAdapter(adaptador);
listaVirus.setOnItemClickListener(new OnItemClickListener(){
public void onItemClick(AdapterView<?> parent,View v,int position,long id){
Bundle b=new Bundle();
Intent i= new Intent(MainActivity.this, PlantasFragmentActivity.class);
i.putExtra("id", id);
startActivity(i);
}
});
}
public class AdaptadorCursorPlagas extends CursorAdapter {
private LayoutInflater inflador; // Crea Layouts a partir del XML
TextView nombre;
ImageView foto;
private Context contexto;
public AdaptadorCursorPlagas(Context contexto, Cursor c) {
super(contexto, c, false);
}
public View getView(int position, View vista, ViewGroup padre,Cursor c) {
vista=padre;
if(vista==null){
inflador = (LayoutInflater) contexto
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
vista = inflador.inflate(R.layout.elemento_lista_bichos, padre, false);
}
nombre = (TextView) vista.findViewById(R.id.nombre_lista_bichos);
foto = (ImageView) vista.findViewById(R.id.foto_lista_bichos);
nombre.setText(c.getString(c.getColumnIndex("nombre")));
foto.setImageResource(c.getColumnIndex("foto"));
foto.setScaleType(ImageView.ScaleType.FIT_END);
return vista;
}
@Override
public View newView(Context contexto, Cursor c, ViewGroup padre) {
View vista=padre;
if(vista==null){
inflador = (LayoutInflater) contexto
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
vista = inflador.inflate(R.layout.elemento_lista_bichos, padre, false);
}
return vista;
}
@Override
public void bindView(View vista, Context contexto, Cursor c) {
nombre = (TextView) vista.findViewById(R.id.nombre_lista_bichos);
foto = (ImageView) vista.findViewById(R.id.foto_lista_bichos);
nombre.setText(c.getString(c.getColumnIndex("nombre")));
foto.setImageResource(c.getColumnIndex("foto"));
foto.setScaleType(ImageView.ScaleType.FIT_END);
}
}
这是我的logcat:
11-30 03:30:22.474: I/art(2013): Late-enabling -Xcheck:jni
11-30 03:30:22.860: D/AndroidRuntime(2013): Shutting down VM
11-30 03:30:22.861: E/AndroidRuntime(2013): FATAL EXCEPTION: main
11-30 03:30:22.861: E/AndroidRuntime(2013): Process: com.example.antiplagas, PID: 2013
11-30 03:30:22.861: E/AndroidRuntime(2013): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.antiplagas/com.example.antiplagas.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.widget.GridView.findViewById(int)' on a null object reference
11-30 03:30:22.861: E/AndroidRuntime(2013): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
11-30 03:30:22.861: E/AndroidRuntime(2013): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
11-30 03:30:22.861: E/AndroidRuntime(2013): at android.app.ActivityThread.access$800(ActivityThread.java:144)
11-30 03:30:22.861: E/AndroidRuntime(2013): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
11-30 03:30:22.861: E/AndroidRuntime(2013): at android.os.Handler.dispatchMessage(Handler.java:102)
11-30 03:30:22.861: E/AndroidRuntime(2013): at android.os.Looper.loop(Looper.java:135)
11-30 03:30:22.861: E/AndroidRuntime(2013): at android.app.ActivityThread.main(ActivityThread.java:5221)
11-30 03:30:22.861: E/AndroidRuntime(2013): at java.lang.reflect.Method.invoke(Native Method)
11-30 03:30:22.861: E/AndroidRuntime(2013): at java.lang.reflect.Method.invoke(Method.java:372)
11-30 03:30:22.861: E/AndroidRuntime(2013): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
11-30 03:30:22.861: E/AndroidRuntime(2013): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
11-30 03:30:22.861: E/AndroidRuntime(2013): Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.widget.GridView.findViewById(int)' on a null object reference
11-30 03:30:22.861: E/AndroidRuntime(2013): at com.example.antiplagas.MainActivity.onCreate(MainActivity.java:36)
11-30 03:30:22.861: E/AndroidRuntime(2013): at android.app.Activity.performCreate(Activity.java:5933)
11-30 03:30:22.861: E/AndroidRuntime(2013): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
11-30 03:30:22.861: E/AndroidRuntime(2013): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
11-30 03:30:22.861: E/AndroidRuntime(2013): ... 10 more
11-30 03:48:07.792: D/AndroidRuntime(2503): Shutting down VM
11-30 03:48:07.794: E/AndroidRuntime(2503): FATAL EXCEPTION: main
11-30 03:48:07.794: E/AndroidRuntime(2503): Process: com.example.antiplagas, PID: 2503
11-30 03:48:07.794: E/AndroidRuntime(2503): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.antiplagas/com.example.antiplagas.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.widget.GridView.findViewById(int)' on a null object reference
11-30 03:48:07.794: E/AndroidRuntime(2503): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
11-30 03:48:07.794: E/AndroidRuntime(2503): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
11-30 03:48:07.794: E/AndroidRuntime(2503): at android.app.ActivityThread.access$800(ActivityThread.java:144)
11-30 03:48:07.794: E/AndroidRuntime(2503): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
11-30 03:48:07.794: E/AndroidRuntime(2503): at android.os.Handler.dispatchMessage(Handler.java:102)
11-30 03:48:07.794: E/AndroidRuntime(2503): at android.os.Looper.loop(Looper.java:135)
11-30 03:48:07.794: E/AndroidRuntime(2503): at android.app.ActivityThread.main(ActivityThread.java:5221)
11-30 03:48:07.794: E/AndroidRuntime(2503): at java.lang.reflect.Method.invoke(Native Method)
11-30 03:48:07.794: E/AndroidRuntime(2503): at java.lang.reflect.Method.invoke(Method.java:372)
11-30 03:48:07.794: E/AndroidRuntime(2503): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
11-30 03:48:07.794: E/AndroidRuntime(2503): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
11-30 03:48:07.794: E/AndroidRuntime(2503): Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.widget.GridView.findViewById(int)' on a null object reference
11-30 03:48:07.794: E/AndroidRuntime(2503): at com.example.antiplagas.MainActivity.onCreate(MainActivity.java:36)
11-30 03:48:07.794: E/AndroidRuntime(2503): at android.app.Activity.performCreate(Activity.java:5933)
11-30 03:48:07.794: E/AndroidRuntime(2503): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
11-30 03:48:07.794: E/AndroidRuntime(2503): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
11-30 03:48:07.794: E/AndroidRuntime(2503): ... 10 more
11-30 03:48:13.028: I/Process(2503): Sending signal. PID: 2503 SIG: 9
对不起,如果有些事情不清楚,但这是我的第一篇文章。感谢
答案 0 :(得分:0)
在你的代码行中:
错误 (GridView) listaVirus.findViewById(R.id.gridViewMain);
listaVirus应该是包含GridView的xml布局。
但是你不必使用listaVirus.findViewById(R.id.gridViewMain);
只需没有listaVirus。(即)
GridView listaVirus =(GridView)findViewById(R.id.gridViewMain);
答案 1 :(得分:0)
你必须在活动上找到ViewById()而不是GridView。
从下面的行中删除updatenumbers()
def update_numbers(x, y, hiddenfield):
if 0 <= x < len(hiddenfield) and 0 <= y < len(hiddenfield[x]):
if hiddenfield[x][y] != 'M':
hiddenfield[x][y] += 1