2013-12-22 41 views
-3

我希望我的應用程序在按鈕點擊時打開WiFi設置,但是當我這樣做時,它強制關閉。這是我的代碼:開啓WiFi設置後關閉應用程序

public class Settings extends Activity { 

    private static Intent ACTION_WIFI_SETTINGS; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_settings); 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.settings, menu); 
     return true; 
    } 

    public void click(View view) { 
     Intent intent = new Intent(Settings.ACTION_WIFI_SETTINGS); 
     startActivity(intent); 
    } 
} 

的XML佈局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    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=".Settings" > 

    <RadioGroup 
     android:id="@+id/radioGroup1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentTop="true" 
     android:layout_marginTop="14dp" > 
    </RadioGroup> 

    <RadioButton 
     android:id="@+id/radio0" 
     android:onClick="togglewifi" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentTop="true" 
     android:checked="true" 
     android:text="Toggle Wifi" /> 

    <RadioButton 
     android:id="@+id/radio1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_below="@+id/radio0" 
     android:text="Toggle Data" /> 

    <Button 
     android:onClick="click" 
     style="?android:attr/buttonStyleSmall" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignLeft="@+id/radio1" 
     android:layout_below="@+id/radio1" 
     android:layout_marginTop="16dp" 
     android:text="Configure" /> 

    <Button 
     android:id="@+id/button2" 
     style="?android:attr/buttonStyleSmall" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:layout_alignParentRight="true" 
     android:text="Done" /> 

    </RelativeLayout> 

的logcat:

12-22 12:29:36.429: E/AndroidRuntime(2813): FATAL EXCEPTION: main 
12-22 12:29:36.429: E/AndroidRuntime(2813): java.lang.IllegalStateException: Could not execute method of the activity 
12-22 12:29:36.429: E/AndroidRuntime(2813):  at  android.view.View$1.onClick(View.java:2144) 
12-22 12:29:36.429: E/AndroidRuntime(2813):  at android.view.View.performClick(View.java:2485) 
12-22 12:29:36.429: E/AndroidRuntime(2813):  at android.view.View$PerformClick.run(View.java:9080) 
12-22 12:29:36.429: E/AndroidRuntime(2813):  at android.os.Handler.handleCallback(Handler.java:587) 
12-22 12:29:36.429: E/AndroidRuntime(2813):  at android.os.Handler.dispatchMessage(Handler.java:92) 

我怎樣才能解決這個問題?

+0

檢查權限在android系統AndroidManifest .xml以及後登錄錯誤。 –

+0

任何logcat要發佈? – csmckelvey

+0

請不要投下來,我已經發布logcat – Anonymous

回答

1

你意識到你正在將Intent操作設置爲null。當你執行你的點擊方法時,它當然會得到一個NPE。修復。

+0

查看編輯.......... – Anonymous

0

在你的XML,你聲明的RadioButtonradio0onClick回調:

android:onClick="togglewifi" 

但你Activity沒有一個方法public void togglewifi(View),導致異常:

java.lang.IllegalStateException: Could not execute method of the activity 
    at  android.view.View$1.onClick(View.java:2144) 
+0

看起來很清楚我的佈局中有兩個onclicks – Anonymous

+0

@Anonymous是的,活動沒有'togglewifi' – laalto

+0

in xml-「點擊」類文件中的 – Anonymous

相關問題