-3

I have the problem, that whenever I try to add any Listener onto my ToggleButton, the App is not even starting and just crashing. In this Codeexample I tried it with an setOnCheckedChangeListener.

It is already crashing, when I just set this even without any Listener:

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();
    }

}

As you can see, I try to add the setOnCheckedChangeListener to the tglButton as always. But I don´t understand why the Application is crashing after that.

I created the button in the textview_list.xml layout. Is the problem, that I can´t access that button from my 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>

The ErrorMessage is:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.paulii.myapplication2/com.example.paulii.myapplication2.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference

Maybe some of you can help me out?

Pauli
  • 1
  • 2

1 Answers1

0

The toggle button is present in the ListView item layout textview_list.xml, so you need to add the listener inside your ListView adapter.

Vishnu Haridas
  • 7,355
  • 3
  • 28
  • 43
Deena
  • 41
  • 3