从findViewById使用View时出现NullPointerException

时间:2012-07-05 16:51:42

标签: android android-layout android-intent findviewbyid

我设计了一个Android应用程序,我想在启动画面后查看菜单。我正确触发它并使用ArrayAdapter创建自定义ListView。但是,当我运行模块时,它会在启动后关闭,logcat会指示NullPointerException。但是,我没有做错任何事。请帮帮我! :)

谢谢。

代码:

package com.sliit.droidman.main;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.sliit.droidman.R;

public class MainActivity extends Activity implements OnItemClickListener{

ArrayList<String> Methods = new ArrayList<String>();
ArrayList<String> MethodDiscription = new ArrayList<String>();
ArrayList<Integer> Icons = new ArrayList<Integer>();
ArrayList<Object> Objects = new ArrayList<Object>();

private ListItemAdapter adapter;
ListView lv;

public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
    Toast.makeText(
            this,
            "Title => " + Methods.get(position) + "=> n Description"
                    + MethodDiscription.get(position), Toast.LENGTH_SHORT).show();
}


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main_menu);

    lv = (ListView) findViewById(R.layout.main_menu);
    fillArrayList();
    lv.setOnItemClickListener(this);

}

public void RefreshListView() {
    Objects.clear();
    for (int i = 0; i < Methods.size(); i++) {
        Object obj = new Object();
        Objects.add(obj);
    }
    Log.d("object array", "" + Objects.size());
    adapter = new ListItemAdapter(Objects, 1);
    lv.setAdapter(adapter);
}

private class ListItemAdapter extends ArrayAdapter<Object>{
    ViewHolder HolderView;

    public ListItemAdapter(List<Object> Item, int ID) {
        super(MainActivity.this, R.layout.listitem_row , Item);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflator = getLayoutInflater();
        convertView = inflator.inflate(R.layout.listitem_row, null);

        HolderView = new ViewHolder();
        HolderView.AppEvent = (TextView) convertView.findViewById(R.id.Event);
        HolderView.EventDisc = (TextView) convertView.findViewById(R.id.EventDescription);
        HolderView.iv = (ImageView) convertView.findViewById(R.id.avatar);

        convertView.setTag(HolderView);

        HolderView.AppEvent.setText(Methods.get(position));
        HolderView.EventDisc.setText(MethodDiscription.get(position));
        HolderView.iv.setBackgroundResource(Icons.get(position));

        return convertView;
    }

    private class ViewHolder {
        TextView AppEvent;
        TextView EventDisc;
        ImageView iv;

    }
}

public void fillArrayList() {
    Methods.clear();
    MethodDiscription.clear();
    Icons.clear();

    Methods.add(0,"Applications");
    Methods.add(1,"System Resources");
    Methods.add(2,"Statistics");
    Methods.add(3,"Threats");

    MethodDiscription.add(0, "bla bla bla");
    MethodDiscription.add(1, "bla bla bla");
    MethodDiscription.add(2, "bla bla bla");
    MethodDiscription.add(3, "bla bla bla");

    Icons.add(0, R.drawable.ic_launcher);
    Icons.add(1, R.drawable.ic_launcher);
    Icons.add(2, R.drawable.ic_launcher);
    Icons.add(3, R.drawable.ic_launcher);
}

}

发生错误的地方的logCat:

07-05 01:44:50.678: W/dalvikvm(1907): threadid=1: thread exiting with uncaught             exception (group=0x40a13300)
07-05 01:44:50.728: E/AndroidRuntime(1907): FATAL EXCEPTION: main
07-05 01:44:50.728: E/AndroidRuntime(1907): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.sliit.droidman/com.sliit.droidman.main.MainActivity}:         java.lang.NullPointerException
07-05 01:44:50.728: E/AndroidRuntime(1907):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
07-05 01:44:50.728: E/AndroidRuntime(1907):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
07-05 01:44:50.728: E/AndroidRuntime(1907):     at android.app.ActivityThread.access$600(ActivityThread.java:130)
07-05 01:44:50.728: E/AndroidRuntime(1907):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
07-05 01:44:50.728: E/AndroidRuntime(1907):     at android.os.Handler.dispatchMessage(Handler.java:99)
07-05 01:44:50.728: E/AndroidRuntime(1907):     at android.os.Looper.loop(Looper.java:137)
07-05 01:44:50.728: E/AndroidRuntime(1907):     at android.app.ActivityThread.main(ActivityThread.java:4745)
07-05 01:44:50.728: E/AndroidRuntime(1907):     at java.lang.reflect.Method.invokeNative(Native Method)
07-05 01:44:50.728: E/AndroidRuntime(1907):     at java.lang.reflect.Method.invoke(Method.java:511)
07-05 01:44:50.728: E/AndroidRuntime(1907):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
07-05 01:44:50.728: E/AndroidRuntime(1907):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
07-05 01:44:50.728: E/AndroidRuntime(1907):     at dalvik.system.NativeStart.main(Native Method)
07-05 01:44:50.728: E/AndroidRuntime(1907): Caused by: java.lang.NullPointerException
07-05 01:44:50.728: E/AndroidRuntime(1907):     at com.sliit.droidman.main.MainActivity.onCreate(MainActivity.java:47)
07-05 01:44:50.728: E/AndroidRuntime(1907):     at android.app.Activity.performCreate(Activity.java:5008)
07-05 01:44:50.728: E/AndroidRuntime(1907):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
07-05 01:44:50.728: E/AndroidRuntime(1907):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
07-05 01:44:50.728: E/AndroidRuntime(1907):     ... 11 more

1 个答案:

答案 0 :(得分:5)

lv变量将为null,因为您没有搜索其ID,因此不是:

lv = (ListView) findViewById(R.layout.main_menu);

您应该搜索ListView的ID:

lv = (ListView) findViewById(R.id.the_id_of_the_listview);