我正在尝试设置一个切换按钮。
我在xml文件中创建了一个按钮,但是总是在激活setOnCheckedChangeListener(this)时创建;我的应用崩溃了。

如果我将其注释掉,则运行我的应用程序没有问题。

布局:

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:id="@+id/layout">


    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Crunch"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <ScrollView
                android:id="@+id/scrollView1"
                android:layout_width="wrap_content"
                android:layout_height="match_parent" >

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:orientation="vertical" >



    <HorizontalScrollView
        android:id="@+id/horizontalScrollView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="horizontal" >




                <ImageView
        android:id="@+id/imageView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_launcher" />

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_launcher" />

        </LinearLayout>
    </HorizontalScrollView>

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/crunch" />



    <TextView
        android:id="@+id/TVTimer"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="" />

    <ToggleButton
        android:id="@+id/toggleButton1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textOff="Pause"
        android:textOn="Play" />

 </LinearLayout>
            </ScrollView>

</LinearLayout>

Activity :
public class Oberklasse extends Activity implements OnInitListener, OnCheckedChangeListener {

public TextToSpeech tts;
ToggleButton t;
LinearLayout l;


protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    tts = new TextToSpeech(this, this);
    //Start und Stop Button
    t=(ToggleButton) findViewById(R.id.toggleButton1);
    t.setOnCheckedChangeListener(this);
    l=(LinearLayout)findViewById(R.id.layout);

    }




    @Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
    // TODO Auto-generated method stub
    if (isChecked) {
        l.setBackgroundColor(Color.BLUE);
    } else {
        l.setBackgroundColor(Color.BLACK);
    }
}

错误代码:
    03-24 23:23:52.544: E/AndroidRuntime(1787): FATAL EXCEPTION: main
03-24 23:23:52.544: E/AndroidRuntime(1787): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.bikinifigur2/de.basti12354.tage.uebungen.Tag1}: java.lang.NullPointerException
03-24 23:23:52.544: E/AndroidRuntime(1787):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
03-24 23:23:52.544: E/AndroidRuntime(1787):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
03-24 23:23:52.544: E/AndroidRuntime(1787):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
03-24 23:23:52.544: E/AndroidRuntime(1787):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
03-24 23:23:52.544: E/AndroidRuntime(1787):     at android.os.Handler.dispatchMessage(Handler.java:99)
03-24 23:23:52.544: E/AndroidRuntime(1787):     at android.os.Looper.loop(Looper.java:137)
03-24 23:23:52.544: E/AndroidRuntime(1787):     at android.app.ActivityThread.main(ActivityThread.java:5103)
03-24 23:23:52.544: E/AndroidRuntime(1787):     at java.lang.reflect.Method.invokeNative(Native Method)
03-24 23:23:52.544: E/AndroidRuntime(1787):     at java.lang.reflect.Method.invoke(Method.java:525)
03-24 23:23:52.544: E/AndroidRuntime(1787):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
03-24 23:23:52.544: E/AndroidRuntime(1787):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
03-24 23:23:52.544: E/AndroidRuntime(1787):     at dalvik.system.NativeStart.main(Native Method)
03-24 23:23:52.544: E/AndroidRuntime(1787): Caused by: java.lang.NullPointerException
03-24 23:23:52.544: E/AndroidRuntime(1787):     at de.basti12354.tage.uebungen.Oberklasse.onCreate(Oberklasse.java:36)
03-24 23:23:52.544: E/AndroidRuntime(1787):     at de.basti12354.tage.uebungen.Tag1.onCreate(Tag1.java:45)
03-24 23:23:52.544: E/AndroidRuntime(1787):     at android.app.Activity.performCreate(Activity.java:5133)
03-24 23:23:52.544: E/AndroidRuntime(1787):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
03-24 23:23:52.544: E/AndroidRuntime(1787):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
03-24 23:23:52.544: E/AndroidRuntime(1787):     ... 11 more

最佳答案

问题是您在查找和存储对 View 的引用之前尚未调用setContentView(R.layout.<layout-file-name>)

10-04 10:31