Android应用程序部队关闭。 (NullPointerException / ToggleButton)

时间:2011-06-24 00:54:39

标签: android

我的代码:

public class My_Application extends Activity {

public ToggleButton privBtn = (ToggleButton) findViewById(R.id.privacyShow);
public LinearLayout privLay = (LinearLayout) findViewById(R.id.privacyLayout);

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

    privBtn.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            if(privBtn.isChecked()) {
                privLay.setVisibility(LinearLayout.VISIBLE);
            }
            else {
                privLay.setVisibility(LinearLayout.INVISIBLE);
            }
        }
    });
}
}

当我启动应用程序时,它会强制关闭。我在模拟器中使用内置的Dev Tools应用程序调试了应用程序,并看到以下内容:

[2011-06-23 20:27:06 - ddms]null
java.lang.NullPointerException
    at com.android.ddmlib.Client.sendAndConsume(Client.java:572)
    at com.android.ddmlib.HandleHello.sendHELO(HandleHello.java:142)
    at com.android.ddmlib.HandleHello.sendHelloCommands(HandleHello.java:65)
    at com.android.ddmlib.Client.getJdwpPacket(Client.java:671)
    at com.android.ddmlib.MonitorThread.processClientActivity(MonitorThread.java:317)
    at com.android.ddmlib.MonitorThread.run(MonitorThread.java:263)

以及“未找到来源”。错误在:

ActivityThread.performLaunchActivity(ActivityThread$ActivityRecord, Intent) line: 2417

main.xml中

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

<TextView android:layout_width="fill_parent" android:layout_height="wrap_content"
android:text="My Application" android:gravity="center_horizontal"
android:textSize="25dp" android:background="#222" />

<LinearLayout 
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="10dp"
>
<TextView android:layout_width="fill_parent" android:layout_height="wrap_content"
android:text="  Server IP: " android:textSize="20dp" android:textColor="#0F0"
android:layout_weight="0.7" />
<EditText android:layout_width="fill_parent" android:layout_height="wrap_content"
android:hint="eg: 0.0.0.0" android:layout_weight="0.5" android:textSize="20dp"
android:id="@+id/loginIP" />
</LinearLayout>

<LinearLayout 
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="10dp"
>
<TextView android:layout_width="fill_parent" android:layout_height="wrap_content"
android:text="Server Port: " android:textSize="20dp" android:textColor="#0F0"
android:layout_weight="0.7" />
<EditText android:layout_width="fill_parent" android:layout_height="wrap_content"
android:hint="eg: 1234" android:layout_weight="0.5" android:textSize="20dp"
android:id="@+id/loginPort" />
</LinearLayout>

<LinearLayout
android:orientation="horizontal"
android:id="@+id/privacyLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="10dp"
>
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="Privacy Options" android:textSize="20dp" android:textColor="#0F0"
android:layout_weight="0.4" />
<ToggleButton android:layout_width="fill_parent" android:layout_height="wrap_content"
android:checked="false" android:layout_weight="0.6" android:textOff="Show Options"
android:textOn="Hide Options" android:id="@+id/privacyShow" />
</LinearLayout>

<LinearLayout 
android:orientation="vertical"
android:visibility="invisible"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="20dp">
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent" android:layout_height="wrap_content"
>
<TextView android:layout_width="fill_parent" android:layout_height="wrap_content"
android:text="Username: " android:textColor="#0F0" android:textSize="15dp"
android:layout_weight="0.5" />
<EditText android:layout_width="fill_parent" android:layout_height="30dp"
android:id="@+id/privacyUsername" android:textSize="15dp" android:layout_weight="0.2" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent" android:layout_height="wrap_content"
>
<TextView android:layout_width="fill_parent" android:layout_height="wrap_content"
android:text="Password: " android:textColor="#0F0" android:textSize="15dp"
android:layout_weight="0.5" />
<EditText android:layout_width="fill_parent" android:layout_height="30dp"
android:id="@+id/privacyPassword" android:textSize="15dp" android:layout_weight="0.2" />
</LinearLayout>
</LinearLayout>

1 个答案:

答案 0 :(得分:2)

在调用setContentView()之前,您无法设置privBtn和privLay。将这两个陈述移到onCreate()

public class My_Application extends Activity {

    public ToggleButton privBtn;
    public LinearLayout privLay;

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

        privBtn = (ToggleButton) findViewById(R.id.privacyShow);
        privLay = (LinearLayout) findViewById(R.id.privacyLayout);

        privBtn.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                if(privBtn.isChecked()) {
                    privLay.setVisibility(LinearLayout.VISIBLE);
                }
                else {
                    privLay.setVisibility(LinearLayout.INVISIBLE);
                }
            }
        });
    }
}