user2386575 user2386575 - 6 months ago 17
Android Question

Android RuntimeException Eclipse

Hi everybody i'm trying to do a Unit converter but when I run it it stop and show me that


05-15 15:30:45.237: D/AndroidRuntime(333): Shutting down VM
05-15 15:30:45.237: W/dalvikvm(333): threadid=1: thread exiting with uncaught exception (group=0x40015560)
05-15 15:30:45.288: E/AndroidRuntime(333): FATAL EXCEPTION: main
05-15 15:30:45.288: E/AndroidRuntime(333): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{org.alaenvargut.conversor/org.alaenvargut.conversor.MainActivity}: java.lang.NullPointerException
05-15 15:30:45.288: E/AndroidRuntime(333): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1569)
05-15 15:30:45.288: E/AndroidRuntime(333): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
05-15 15:30:45.288: E/AndroidRuntime(333): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
05-15 15:30:45.288: E/AndroidRuntime(333): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
05-15 15:30:45.288: E/AndroidRuntime(333): at android.os.Handler.dispatchMessage(Handler.java:99)
05-15 15:30:45.288: E/AndroidRuntime(333): at android.os.Looper.loop(Looper.java:123)
05-15 15:30:45.288: E/AndroidRuntime(333): at android.app.ActivityThread.main(ActivityThread.java:3683)
05-15 15:30:45.288: E/AndroidRuntime(333): at java.lang.reflect.Method.invokeNative(Native Method)
05-15 15:30:45.288: E/AndroidRuntime(333): at java.lang.reflect.Method.invoke(Method.java:507)
05-15 15:30:45.288: E/AndroidRuntime(333): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
05-15 15:30:45.288: E/AndroidRuntime(333): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
05-15 15:30:45.288: E/AndroidRuntime(333): at dalvik.system.NativeStart.main(Native Method)
05-15 15:30:45.288: E/AndroidRuntime(333): Caused by: java.lang.NullPointerException
05-15 15:30:45.288: E/AndroidRuntime(333): at android.app.Activity.findViewById(Activity.java:1647)
05-15 15:30:45.288: E/AndroidRuntime(333): at org.alaenvargut.conversor.MainActivity.(MainActivity.java:13)
05-15 15:30:45.288: E/AndroidRuntime(333): at java.lang.Class.newInstanceImpl(Native Method)
05-15 15:30:45.288: E/AndroidRuntime(333): at java.lang.Class.newInstance(Class.java:1409)
05-15 15:30:45.288: E/AndroidRuntime(333): at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
05-15 15:30:45.288: E/AndroidRuntime(333): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1561)
05-15 15:30:45.288: E/AndroidRuntime(333): ... 11 more


this is my main code

package org.alaenvargut.conversor;

import android.os.Bundle;
import android.app.Activity;
import android.widget.Spinner;
import android.widget.ArrayAdapter;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;


public class MainActivity extends Activity implements OnItemSelectedListener{
public Spinner SUni = (Spinner) findViewById(R.id.spinner1);
public Spinner SFm = (Spinner) findViewById(R.id.spinner2);
public Spinner STo = (Spinner) findViewById(R.id.spinner3);
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ArrayAdapter AUni = ArrayAdapter.createFromResource(this, R.array.Unidades, android.R.layout.simple_selectable_list_item);
AUni.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
SUni.setAdapter(AUni);
SUni.setOnItemSelectedListener(this);
}
public void onItemSelected(AdapterView<?> parent, View view,int pos, long id)
{
if (parent.getId() == R.id.spinner1)
{
switch((int)id)
{
case(1):
{
}
case(2):
{
ArrayAdapter AFm = ArrayAdapter.createFromResource(this, R.array.Velocidad, android.R.layout.simple_selectable_list_item);
AFm.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
SFm.setAdapter(AFm);

}
default:
}
}
}

public void onNothingSelected(AdapterView<?> parent)
{
// Another interface callback
}




}


and this is my 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=".MainActivity" >

<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="19dp"
android:text="@string/principal" />

<Spinner
android:id="@+id/spinner1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/textView1"
android:layout_marginTop="15dp" />

<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/spinner1"
android:layout_below="@+id/spinner1"
android:layout_marginTop="18dp"
android:text="@string/fm"
android:textAppearance="?android:attr/textAppearanceMedium" />

<Spinner
android:id="@+id/spinner2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView2"
android:layout_centerHorizontal="true" />

<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/spinner2"
android:layout_below="@+id/spinner2"
android:text="@string/to"
android:textAppearance="?android:attr/textAppearanceMedium" />

<Spinner
android:id="@+id/spinner3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView3"
android:layout_centerHorizontal="true" />

<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/spinner3"
android:layout_below="@+id/spinner3"
android:layout_marginTop="30dp"
android:text="@string/con" />

<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/button1"
android:layout_alignRight="@+id/spinner3"
android:layout_marginRight="29dp"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />




and this is my xml file from values

> <?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="Unidades">
<item >Volumen</item>
<item >Velocidad</item>
</string-array>
<string-array name="Velocidad">
<item >Km/h</item>
<item >M/h</item>
</string-array>
</resources>


any idea about the exception?

Answer

You just have to initialize your Spinners after setting your layout in your onCreate() method not before that. Just try as below: ​

public Spinner SUni;
public Spinner SFm;
public Spinner STo;

@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    SUni = (Spinner) findViewById(R.id.spinner1);
    SFm = (Spinner) findViewById(R.id.spinner2);
    STo = (Spinner) findViewById(R.id.spinner3);
}