当我尝试在android ToggleButton中放置onclicklistener时获取nullpointerexception

时间:2014-04-03 15:58:05

标签: java android nullpointerexception togglebutton

当我尝试在android中的toggleButton中的一个监听器中放一个toast时,获取空指针异常可以有人帮助我吗?

以下是代码:

private ToggleButton locationToggle;
    private Context context;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
        setContentView(R.layout.settings);
        getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,
                R.layout.title_bar);

        context = getApplicationContext();

        sensorAdapter = new SensorAdapter();

        ListView sensorListview = (ListView) findViewById(R.id.listViewSettings);
        sensorListview.setAdapter(sensorAdapter);

        locationToggle = (ToggleButton) findViewById(R.id.toggleButton1);

        // attach an OnClickListener
        locationToggle.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(context, "CARREGOU", Toast.LENGTH_LONG).show();
                // your click actions go here
            }
        });

这是错误日志:

04-03 16:57:29.511: E/AndroidRuntime(10552): FATAL EXCEPTION: main
04-03 16:57:29.511: E/AndroidRuntime(10552): java.lang.RuntimeException: Unable to start activity ComponentInfo{main.inSituApp/main.inSituApp.Settings}: java.lang.NullPointerException
04-03 16:57:29.511: E/AndroidRuntime(10552):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
04-03 16:57:29.511: E/AndroidRuntime(10552):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
04-03 16:57:29.511: E/AndroidRuntime(10552):    at android.app.ActivityThread.access$600(ActivityThread.java:130)
04-03 16:57:29.511: E/AndroidRuntime(10552):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
04-03 16:57:29.511: E/AndroidRuntime(10552):    at android.os.Handler.dispatchMessage(Handler.java:99)
04-03 16:57:29.511: E/AndroidRuntime(10552):    at android.os.Looper.loop(Looper.java:137)
04-03 16:57:29.511: E/AndroidRuntime(10552):    at android.app.ActivityThread.main(ActivityThread.java:4745)
04-03 16:57:29.511: E/AndroidRuntime(10552):    at java.lang.reflect.Method.invokeNative(Native Method)
04-03 16:57:29.511: E/AndroidRuntime(10552):    at java.lang.reflect.Method.invoke(Method.java:511)
04-03 16:57:29.511: E/AndroidRuntime(10552):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
04-03 16:57:29.511: E/AndroidRuntime(10552):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
04-03 16:57:29.511: E/AndroidRuntime(10552):    at dalvik.system.NativeStart.main(Native Method)
04-03 16:57:29.511: E/AndroidRuntime(10552): Caused by: java.lang.NullPointerException
04-03 16:57:29.511: E/AndroidRuntime(10552):    at main.inSituApp.Settings.onCreate(Settings.java:64)
04-03 16:57:29.511: E/AndroidRuntime(10552):    at android.app.Activity.performCreate(Activity.java:5008)
04-03 16:57:29.511: E/AndroidRuntime(10552):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
04-03 16:57:29.511: E/AndroidRuntime(10552):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
04-03 16:57:29.511: E/AndroidRuntime(10552):    ... 11 more

我无法弄清楚这有什么问题。始终为空指针异常

togglebuttons位于listitem.xml文件中。

编辑:

我更改了代码以初始化适配器内的togglebuttons。这是代码:

public class SensorAdapter extends BaseAdapter {

    List<sensorItem> sensorList = getDataForListView();

    private ToggleButton locationToggle, photosToggle, soundToggle,
    orientationToggle, messagesToggle, batteryToggle, calendarToggle,
    accelerometerToggle, lightToggle, contactsToggle;

    @Override
    public int getCount() {
        return sensorList.size();
    }

    @Override
    public sensorItem getItem(int arg0) {
        return sensorList.get(arg0);
    }

    @Override
    public long getItemId(int arg0) {
        return arg0;
    }

    @Override
    public View getView(int arg0, View arg1, ViewGroup arg2) {

        if (arg1 == null) {
            LayoutInflater inflater = (LayoutInflater) Settings.this
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            arg1 = inflater.inflate(R.layout.listitem, arg2, false);
        }

        // TextView sensorName = (TextView)
        // arg1.findViewById(R.id.textView1);
        TextView sensorDescription = (TextView) arg1
                .findViewById(R.id.textView2);
        ImageView sensorImage = (ImageView) arg1
                .findViewById(R.id.imageView1);

        locationToggle = (ToggleButton) findViewById(R.id.toggleButton1);
        photosToggle = (ToggleButton) findViewById(R.id.toggleButton1);
        soundToggle = (ToggleButton) findViewById(R.id.toggleButton1);
        orientationToggle = (ToggleButton) findViewById(R.id.toggleButton1);
        messagesToggle = (ToggleButton) findViewById(R.id.toggleButton1);
        batteryToggle = (ToggleButton) findViewById(R.id.toggleButton1);
        calendarToggle = (ToggleButton) findViewById(R.id.toggleButton1);
        accelerometerToggle = (ToggleButton) findViewById(R.id.toggleButton1);
        lightToggle = (ToggleButton) findViewById(R.id.toggleButton1);
        contactsToggle = (ToggleButton) findViewById(R.id.toggleButton1);

        // attach an OnClickListener
        locationToggle.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(context, "CARREGOU", Toast.LENGTH_LONG).show();
                // your click actions go here
            }
        });

        sensorItem sensor = sensorList.get(arg0);

        // sensorName.setText(sensor.sensorName);
        sensorDescription.setText(sensor.sensorDescription);
        sensorImage.setBackground(sensor.sensorImage);

        return arg1;
    }

    public sensorItem getCodeLearnChapter(int position) {
        return sensorList.get(position);
    }

}

但现在我收到了这个错误。

04-03 17:12:39.906: E/AndroidRuntime(11012): FATAL EXCEPTION: main
04-03 17:12:39.906: E/AndroidRuntime(11012): java.lang.NullPointerException
04-03 17:12:39.906: E/AndroidRuntime(11012):    at main.inSituApp.Settings$SensorAdapter.getView(Settings.java:200)
04-03 17:12:39.906: E/AndroidRuntime(11012):    at android.widget.AbsListView.obtainView(AbsListView.java:2271)
04-03 17:12:39.906: E/AndroidRuntime(11012):    at android.widget.ListView.measureHeightOfChildren(ListView.java:1244)
04-03 17:12:39.906: E/AndroidRuntime(11012):    at android.widget.ListView.onMeasure(ListView.java:1156)
04-03 17:12:39.906: E/AndroidRuntime(11012):    at android.view.View.measure(View.java:15172)
04-03 17:12:39.906: E/AndroidRuntime(11012):    at android.widget.RelativeLayout.measureChild(RelativeLayout.java:602)
04-03 17:12:39.906: E/AndroidRuntime(11012):    at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:415)
04-03 17:12:39.906: E/AndroidRuntime(11012):    at android.view.View.measure(View.java:15172)
04-03 17:12:39.906: E/AndroidRuntime(11012):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4816)
04-03 17:12:39.906: E/AndroidRuntime(11012):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
04-03 17:12:39.906: E/AndroidRuntime(11012):    at android.view.View.measure(View.java:15172)
04-03 17:12:39.906: E/AndroidRuntime(11012):    at android.widget.LinearLayout.measureVertical(LinearLayout.java:833)
04-03 17:12:39.906: E/AndroidRuntime(11012):    at android.widget.LinearLayout.onMeasure(LinearLayout.java:574)
04-03 17:12:39.906: E/AndroidRuntime(11012):    at android.view.View.measure(View.java:15172)
04-03 17:12:39.906: E/AndroidRuntime(11012):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4816)
04-03 17:12:39.906: E/AndroidRuntime(11012):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
04-03 17:12:39.906: E/AndroidRuntime(11012):    at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2148)
04-03 17:12:39.906: E/AndroidRuntime(11012):    at android.view.View.measure(View.java:15172)
04-03 17:12:39.906: E/AndroidRuntime(11012):    at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1850)
04-03 17:12:39.906: E/AndroidRuntime(11012):    at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1102)
04-03 17:12:39.906: E/AndroidRuntime(11012):    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1275)
04-03 17:12:39.906: E/AndroidRuntime(11012):    at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1000)
04-03 17:12:39.906: E/AndroidRuntime(11012):    at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4214)
04-03 17:12:39.906: E/AndroidRuntime(11012):    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)
04-03 17:12:39.906: E/AndroidRuntime(11012):    at android.view.Choreographer.doCallbacks(Choreographer.java:555)
04-03 17:12:39.906: E/AndroidRuntime(11012):    at android.view.Choreographer.doFrame(Choreographer.java:525)
04-03 17:12:39.906: E/AndroidRuntime(11012):    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711)
04-03 17:12:39.906: E/AndroidRuntime(11012):    at android.os.Handler.handleCallback(Handler.java:615)
04-03 17:12:39.906: E/AndroidRuntime(11012):    at android.os.Handler.dispatchMessage(Handler.java:92)
04-03 17:12:39.906: E/AndroidRuntime(11012):    at android.os.Looper.loop(Looper.java:137)
04-03 17:12:39.906: E/AndroidRuntime(11012):    at android.app.ActivityThread.main(ActivityThread.java:4745)
04-03 17:12:39.906: E/AndroidRuntime(11012):    at java.lang.reflect.Method.invokeNative(Native Method)
04-03 17:12:39.906: E/AndroidRuntime(11012):    at java.lang.reflect.Method.invoke(Method.java:511)
04-03 17:12:39.906: E/AndroidRuntime(11012):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
04-03 17:12:39.906: E/AndroidRuntime(11012):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
04-03 17:12:39.906: E/AndroidRuntime(11012):    at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:2)

你有

setContentView(R.layout.settings);

你说

  

togglebuttons位于 listitem.xml 文件中。

所以这个

locationToggle = (ToggleButton) findViewById(R.id.toggleButton1);

未能导致NullPointerExceptionToogleButton不属于settings.xml,您可以在当前视图层次结构中找到视图。