I have a Android file whose code is follwed..I had problems with Spinners...
public class first extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Spinner spincity;
String[] citynames={"select","Ahmedabad","Vadodara","Surat","Gandhinagar","Himatnagar",
"Surendranagar","Navsari","Valsad","Viramgham"};
spincity=(Spinner) findViewById(R.id.spincity);
ArrayAdapter<String> a=new ArrayAdapter<String>(this.getApplicationContext(), android.R.layout.simple_spinner_item,citynames);
a.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
spincity.setAdapter(a);
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
}
}
The layout file .xml is as below:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/ScrollView01"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<RelativeLayout
android:id="@+id/RelativeLayout01"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="@+id/city"
android:text="City :"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:gravity="left"
/>
<Spinner
android:id="@+id/spincity"
android:layout_width="220dp"
android:layout_height="wrap_content">
</Spinner>
</LinearLayout>
</RelativeLayout>
</ScrollView>
This code gives a NullPointerException
. I have tried my best but couldn't resolve it.
The Logcat is as follow::
02-23 16:04:19.554: ERROR/AndroidRuntime(1154): FATAL EXCEPTION: main
02-23 16:04:19.554: ERROR/AndroidRuntime(1154): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.spinners.changeimage/com.spinners.changeimage.first}: java.lang.NullPointerException
02-23 16:04:19.554: ERROR/AndroidRuntime(1154): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1569)
02-23 16:04:19.554: ERROR/AndroidRuntime(1154): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
02-23 16:04:19.554: ERROR/AndroidRuntime(1154): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
02-23 16:04:19.554: ERROR/AndroidRuntime(1154): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
02-23 16:04:19.554: ERROR/AndroidRuntime(1154): at android.os.Handler.dispatchMessage(Handler.java:99)
02-23 16:04:19.554: ERROR/AndroidRuntime(1154): at android.os.Looper.loop(Looper.java:123)
02-23 16:04:19.554: ERROR/AndroidRuntime(1154): at android.app.ActivityThread.main(ActivityThread.java:3683)
02-23 16:04:19.554: ERROR/AndroidRuntime(1154): at java.lang.reflect.Method.invokeNative(Native Method)
02-23 16:04:19.554: ERROR/AndroidRuntime(1154): at java.lang.reflect.Method.invoke(Method.java:507)
02-23 16:04:19.554: ERROR/AndroidRuntime(1154): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
02-23 16:04:19.554: ERROR/AndroidRuntime(1154): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
02-23 16:04:19.554: ERROR/AndroidRuntime(1154): at dalvik.system.NativeStart.main(Native Method)
02-23 16:04:19.554: ERROR/AndroidRuntime(1154): Caused by: java.lang.NullPointerException
02-23 16:04:19.554: ERROR/AndroidRuntime(1154): at android.content.ContextWrapper.getResources(ContextWrapper.java:80)
02-23 16:04:19.554: ERROR/AndroidRuntime(1154): at com.spinners.changeimage.first.<init>(first.java:17)
02-23 16:04:19.554: ERROR/AndroidRuntime(1154): at java.lang.Class.newInstanceImpl(Native Method)
02-23 16:04:19.554: ERROR/AndroidRuntime(1154): at java.lang.Class.newInstance(Class.java:1409)
02-23 16:04:19.554: ERROR/AndroidRuntime(1154): at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
02-23 16:04:19.554: ERROR/AndroidRuntime(1154): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1561)
02-23 16:04:19.554: ERROR/AndroidRuntime(1154): ... 11 more
No comments:
Post a Comment