2017-01-29 15 views
-3

ToggleButtonにListenerを追加しようとすると、アプリケーションが起動せずクラッシュするという問題があります。このコードサンプルでは、​​setOnCheckedChangeListenerを使用して試しました。Android:ToggleButtonにClickListenerを追加するときにプログラムがクラッシュする

すでに私はちょうどでも任意のリスナーなしでこれを設定すると、クラッシュした:

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のボタンにアクセスできないという問題はありますか?

<?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> 

activity_mainにErrorMessageである:

java.lang.RuntimeException:活性ComponentInfo {COMを開始できません。 example.paulii.mya pplication2/com.example.paulii.myapplication2.MainActivity}:nullオブジェクト参照で仮想メソッド 'void android.widget.Button.setOnClickListener(android.view.View $ OnClickListener)'を呼び出そうとしました

多分あなたの何人かが私を助けることができますか?

+0

重複するhttp://stackoverflow.com/questions/28601476/java-lang-nullpointerexception-attempt-to-invoke-virtual-method-int-android-vi – AnixPasBesoin

+0

あなたのコンポーネントは、この画面ではご利用いただけません。その画面のそのボタン –

答えて

0

リストビューの項目レイアウトtextview_list.xmlにトグルボタンがあるので、ListViewアダプタ内にリスナーを追加する必要があります。

+0

コード例を追加してください – Stefan

関連する問題