Trifecta
Trifecta

Reputation: 67

Android Java Development: App Stopped Working

I have been trying to test Android Application development for a school project. I am testing out 2 radio buttons that will change a text field. I think i got the code correct but I am getting a "Stopped working" whenever i tried to run it. I am using API 11+

Here is my XML File:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/RelativeLayout1"
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=".ServicesActivity" >

<RadioGroup
    android:id="@+id/radioGroupDepart"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true" >

 <RadioButton
    android:id="@+id/radioBtnMalta"
    android:layout_width="100dp"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/textTest"
    android:layout_marginTop="26dp"
    android:text="Malta" />

<RadioButton
    android:id="@+id/radioBtnGozo"
    android:layout_width="100dp"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/radioBtnMalta"
    android:layout_alignBottom="@+id/radioBtnMalta"
    android:layout_alignParentRight="true"
    android:layout_marginRight="40dp"
    android:text="Gozo" />
</RadioGroup>



<TextView
    android:id="@+id/textTest"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="23dp"
    android:text="Large Text"
    android:textAppearance="?android:attr/textAppearanceLarge" />

Here is my .java File:

package com.example.services;

SKIPPING IMPORTS BECAUSE SOMEHOW THEY CAN'T BE DISPLAYED CORRECTLY

public class ServicesActivity extends Activity implements RadioGroup.OnCheckedChangeListener {


//Radio Group Declaration and Edit Text
EditText et1;
RadioButton maltaRB;
RadioButton gozoRB;
RadioGroup gozoMaltaRBGroup;

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

    //Radio Group
    et1=(EditText)findViewById(R.id.textTest);
    maltaRB=(RadioButton)findViewById(R.id.MaltaRadioBtn);
    gozoRB=(RadioButton)findViewById(R.id.GozoRadioBtn);
    gozoMaltaRBGroup=(RadioGroup)findViewById(R.id.radioGroupDepart);
    gozoMaltaRBGroup.setOnCheckedChangeListener(this);
}

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

    return true;
}

TextView textTest = (TextView)findViewById(R.id.textTest);

@Override
public void onCheckedChanged(RadioGroup gozoMaltaRBGroup, int i) {
    if(i==maltaRB.getId()){

        textTest.setText("Leaving from Malta");
    }
    else if(i==gozoRB.getId()){
        textTest.setText("Leaving from Gozo");
    }

}

}

Finally here is my error log from LogCat:

  10-21 15:28:03.802: D/AndroidRuntime(829): Shutting down VM
10-21 15:28:03.802: W/dalvikvm(829): threadid=1: thread exiting with uncaught exception (group=0x414c4700)
10-21 15:28:03.821: E/AndroidRuntime(829): FATAL EXCEPTION: main
10-21 15:28:03.821: E/AndroidRuntime(829): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.services/com.example.services.ServicesActivity}: java.lang.NullPointerException
10-21 15:28:03.821: E/AndroidRuntime(829):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2137)
10-21 15:28:03.821: E/AndroidRuntime(829):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
10-21 15:28:03.821: E/AndroidRuntime(829):  at android.app.ActivityThread.access$600(ActivityThread.java:141)
10-21 15:28:03.821: E/AndroidRuntime(829):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
10-21 15:28:03.821: E/AndroidRuntime(829):  at android.os.Handler.dispatchMessage(Handler.java:99)
10-21 15:28:03.821: E/AndroidRuntime(829):  at android.os.Looper.loop(Looper.java:137)
10-21 15:28:03.821: E/AndroidRuntime(829):  at android.app.ActivityThread.main(ActivityThread.java:5103)
10-21 15:28:03.821: E/AndroidRuntime(829):  at java.lang.reflect.Method.invokeNative(Native Method)
10-21 15:28:03.821: E/AndroidRuntime(829):  at java.lang.reflect.Method.invoke(Method.java:525)
10-21 15:28:03.821: E/AndroidRuntime(829):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
10-21 15:28:03.821: E/AndroidRuntime(829):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
10-21 15:28:03.821: E/AndroidRuntime(829):  at dalvik.system.NativeStart.main(Native Method)
10-21 15:28:03.821: E/AndroidRuntime(829): Caused by: java.lang.NullPointerException
10-21 15:28:03.821: E/AndroidRuntime(829):  at android.app.Activity.findViewById(Activity.java:1853)
10-21 15:28:03.821: E/AndroidRuntime(829):  at com.example.services.ServicesActivity.<init>(ServicesActivity.java:42)
10-21 15:28:03.821: E/AndroidRuntime(829):  at java.lang.Class.newInstanceImpl(Native Method)
10-21 15:28:03.821: E/AndroidRuntime(829):  at java.lang.Class.newInstance(Class.java:1130)
10-21 15:28:03.821: E/AndroidRuntime(829):  at android.app.Instrumentation.newActivity(Instrumentation.java:1061)
10-21 15:28:03.821: E/AndroidRuntime(829):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2128)
10-21 15:28:03.821: E/AndroidRuntime(829):  ... 11 more

Any help will be greatly appreciated!

Upvotes: 0

Views: 268

Answers (2)

Andrew
Andrew

Reputation: 334

You have extra line of code:

TextView textTest = (TextView)findViewById(R.id.textTest);

between your methods onCreateOptionsMenu() and onCheckedChanged(). This line doesn't fit there, it must be placed inside some method. Probably you may divide it by two lines: declaration

TextView textTest;

and initialization

textTest = (TextView)findViewById(R.id.textTest);

but that depends on what you really wanna get.

Upvotes: 1

codeMagic
codeMagic

Reputation: 44571

These lines are returning null

maltaRB=(RadioButton)findViewById(R.id.MaltaRadioBtn);
gozoRB=(RadioButton)findViewById(R.id.GozoRadioBtn);

check the ids. You have different ids in Java than in your layout file.

Change them to

maltaRB=(RadioButton)findViewById(R.id.radioBtnMalta);
gozoRB=(RadioButton)findViewById(R.id.radioBtnGozo);

and that should work. Alternatively, you could change the ids in your xml but either way they must match or you get a NPE when trying to call a function or set a listener on them.

Also, this will return null

TextView textTest = (TextView)findViewById(R.id.textTest);

you need to initialize it inside of a function or it will return null since it will run before setContentView(). Just declare and initialize it as you are your other Views.

Upvotes: 1

Related Questions