【问题标题】:Android: When adding an ClickListener to an ToggleButton the Program crashAndroid:将 ClickListener 添加到 ToggleButton 时程序崩溃
【发布时间】:2017-01-29 15:17:52
【问题描述】:

我有一个问题,每当我尝试将任何侦听器添加到我的 ToggleButton 时,应用程序甚至都没有启动并且只是崩溃了。在此代码示例中,我使用 setOnCheckedChangeListener 进行了尝试。

它已经崩溃了,当我没有任何监听器时我只是设置了这个:

tglButton = (ToggleButton)findViewById(R.id.tglBtn);

MainActivity.java

package com.example.paulii.myapplication2;

import android.content.Intent;
import android.database.Cursor;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.Toast;
import android.widget.ToggleButton;

public class MainActivity extends AppCompatActivity {

    ListView mListView;
    Button button_stpd;
    DBAdapter myDB;
    Button btn_delete;
    ToggleButton tglButton;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        openDB();
        populateListView();

        button_stpd = (Button)findViewById(R.id.btn_createNewAlarm);

        button_stpd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, createNewWecker.class);
                startActivity(intent);
            }
        });

        mListView = (ListView)findViewById(R.id.stationList);
        mListView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
                myDB.deleteRow(id);
                populateListView();
                return false;
            }
        });

        btn_delete = (Button)findViewById(R.id.btn_delete);
        btn_delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                myDB.deleteAll();
                populateListView();
            }
        });

    tglButton = (ToggleButton)findViewById(R.id.tglBtn);
        tglButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                Toast.makeText(getApplicationContext(), String.valueOf(buttonView.isChecked()),Toast.LENGTH_SHORT).show();
            }
        });


    }

    public void populateListView(){
        Cursor cursor = myDB.getAllRows();

        String[] fromFieldNames = new String[]{DBAdapter.KEY_TASK};

        int[] toViewIDs = new int[]{R.id.textStation};

        SimpleCursorAdapter myCursorAdapter;
        myCursorAdapter = new SimpleCursorAdapter(getBaseContext(), R.layout.textview_list, cursor, fromFieldNames, toViewIDs,0);
        ListView myList = (ListView)findViewById(R.id.stationList);
        myList.setAdapter(myCursorAdapter);

    }


    @Override
    protected void onStop() {
        super.onStop();
    }

    private void openDB(){
        myDB = new DBAdapter(this);
        myDB.open();
    }

    private void closeDB(){
        myDB.close();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        closeDB();
    }

}

如您所见,我尝试像往常一样将 setOnCheckedChangeListener 添加到 tglButton。但我不明白为什么应用程序在那之后崩溃了。

我在 textview_list.xml 布局中创建了按钮。问题是我无法从 MainActivity 访问该按钮吗?

activity_main

 <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/activity_testwindow"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        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="com.example.paulii.myapplication2.MainActivity">

        <LinearLayout
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:weightSum="1"
            android:layout_alignParentTop="true"
            android:layout_alignParentStart="true">

            <LinearLayout
                android:orientation="vertical"
                android:layout_width="match_parent"
                android:layout_height="282dp"
                android:layout_weight="1.07"
                android:weightSum="1">

                <RelativeLayout
                    android:orientation="vertical"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_weight="0.72"
                    android:weightSum="1"
                    tools:ignore="NestedWeights,UselessParent"
                    android:layout_marginBottom="10dp">

                    <ListView
                        android:layout_width="match_parent"
                        android:id="@+id/stationList"
                        android:layout_gravity="left"
                        tools:ignore="RtlHardcoded"
                        android:layout_height="400dp" />

                    <Button
                        android:text="Delete All"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:id="@+id/btn_delete"
                        android:layout_alignBottom="@+id/btn_createNewAlarm"
                        android:layout_alignParentEnd="true" />

                    <Button
                        android:text="@string/create_new_alarm"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:id="@+id/btn_createTimePicker"
                        android:layout_alignParentBottom="true"
                        android:layout_alignParentStart="true" />


                </RelativeLayout>

            </LinearLayout>

        </LinearLayout>

    </RelativeLayout>

textview_list.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="horizontal" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:weightSum="1">

    <TextView
        android:text="TextView"
        android:layout_width="150dp"
        android:id="@+id/textStation"
        android:layout_height="68dp"
        android:layout_weight="0.87" />

    <ToggleButton
        android:text="ToggleButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/tglBtn"
        android:focusable="false" />

</LinearLayout>

错误消息是:

java.lang.RuntimeException:无法启动活动 ComponentInfo{com.example.paulii.myapplication2/com.example.paulii.myapplication2.MainActivity}:java.lang.NullPointerException:尝试调用虚拟方法 'void android.widget .Button.setOnClickListener(android.view.View$OnClickListener)' 在空对象引用上

也许你们中的一些人可以帮助我?

【问题讨论】:

标签: android


【解决方案1】:

切换按钮出现在 ListView 项目布局textview_list.xml 中,因此您需要在 ListView 适配器中添加监听器。

【讨论】:

    猜你喜欢
    • 2022-06-19
    • 1970-01-01
    • 1970-01-01
    • 2017-12-22
    • 2021-02-11
    • 1970-01-01
    • 2012-11-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多