Android Application force closes. (NullPointerException / ToggleButton)
My Code:
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);
}
}
});
}
}
When I launch the application it force closes. I debugged the application with the built-in Dev Tools application in the emulator, and see the following:
[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.getJdwpPac开发者_JS百科ket(Client.java:671)
at com.android.ddmlib.MonitorThread.processClientActivity(MonitorThread.java:317)
at com.android.ddmlib.MonitorThread.run(MonitorThread.java:263)
As well as a "Source not found." error at:
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>
You can't set the privBtn and privLay before the setContentView()
is called. Move those two statements into your 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);
}
}
});
}
}
精彩评论