-3

I want my app to open WiFi settings on button click but when I do so, it force closes. This is my code:

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

The XML Layout:

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

How can I fix this?

hichris123
  • 10,145
  • 15
  • 56
  • 70
Anonymous
  • 1,389
  • 3
  • 11
  • 16

2 Answers2

1

You do realize that you are setting the Intent action to null. When you execute your click method it would of course get an NPE. Fix that.

JoxTraex
  • 13,423
  • 6
  • 32
  • 45
0

In your XML, you declare an onClick callback on the RadioButton radio0:

android:onClick="togglewifi"

but your Activity does not have a method public void togglewifi(View), causing the exception:

java.lang.IllegalStateException: Could not execute method of the activity
     at     android.view.View$1.onClick(View.java:2144)
laalto
  • 150,114
  • 66
  • 286
  • 303