2011-06-24 95 views
0

我的代碼:Android應用程序部隊關閉。 (NullPointerException異常/切換按鈕)

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

當我啓動它強制關閉應用程序。我調試,在仿真器中內置的開發工具的應用程序的應用程序,看到以下內容:

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

回答

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); 
       } 
      } 
     }); 
    } 
} 
除了
+1

,如果你想讓他們在全球宣佈他們前面,只是爲它們分配在'的onCreate()' – Gix

+0

^^權。你一定需要privLay至少有你的活動範圍,以便你可以在OnClickListener中使用它。編輯以提供代碼示例。 –

+0

我仍然得到'NullPointerException' ... – D0WNGRADE