-1

I'm still new to programming with xml and programming in general, but what i'm trying to do here is add a spinner. But for some reason i can't get the spinner to work. I tried to add 2 different ones, but the result stays the same. If i add the 1 of the spinners, the application stops. And if i remove the whole spinner code, it at least opens the activity, but its completely blank. Even though i added some textviews in the fragment of the second activity.

Could someone point out what i'm doing wrong here?

This is what i got in my second activity so far.

public class OpenBMI extends Activity {

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

    //Spinner 1
/*   
    // Array 
    String gender[] = {"Male","Female"};

    // Create spinner
    Spinner spinner = (Spinner) findViewById(R.id.inputGender);

    // Application from array to spinner
    ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(this,   android.R.layout.simple_spinner_item, gender);
    spinnerArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 

    // Drop down
    spinner.setAdapter(spinnerArrayAdapter); 
*/

    //Spinner 2
    /*
    Spinner spinGender;

    //Gets spinner from layout
    spinGender = (Spinner) findViewById(R.id.spinGender); 

    //Gets string for spinner
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, getResources()
                        .getStringArray(R.array.inputGender));


    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinGender.setAdapter(adapter);


    //layout voor invoer actions. Bewaren voor verschil bmi tussen man en vrouw
    spinGender.setOnItemSelectedListener(new OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> arg0, View arg1,
                    int position, long id) {
            }

            @Override
            public void onNothingSelected(AdapterView<?> arg0) {
            }
        }); */


}

}

And this is in my fragment

<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="com.example.practicum.OpenBMI$PlaceholderFragment" >


<EditText
    android:id="@+id/inputLength"
    android:layout_width="80dp"
    android:layout_height="wrap_content"
    android:layout_below="@+id/spinGender"
    android:layout_marginTop="45dp"
    android:layout_marginLeft="250dp"
    android:ems="10"
    android:inputType="number" />

<EditText
    android:id="@+id/inputWeight"
    android:layout_width="80dp"
    android:layout_height="wrap_content"
    android:layout_below="@+id/inputLength"
    android:layout_centerVertical="true"
    android:layout_marginTop="45dp"
    android:layout_marginLeft="250dp"
    android:ems="10"
    android:inputType="number" />

<TextView
    android:id="@+id/stringGender"
    android:layout_width="100dp"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/spinGender"
    android:ems="10"/>

<TextView
    android:id="@+id/stringLength"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/inputLength"
    android:ems="10" 
    android:text="@string/lengte"/>

<TextView
    android:id="@+id/StringWeight"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/inputWeight"
    android:ems="10"
    android:text="@string/gewicht"/>

<Spinner
    android:id="@+id/spinGender"
    android:layout_width="80dp"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_alignRight="@+id/inputLength"
    android:layout_marginTop="60dp"
    android:layout_toRightOf="@+id/stringGender"
    android:ems="10"
    android:paddingLeft="100dp" 
    android:popupBackground="@android:color/white"
    android:scrollbars="none"
    android:spinnerMode="dropdown"/>

</RelativeLayout>

And this is placed in my strings

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="app_name">Practicum</string>
    <string name="action_settings">Settings</string>
    <string name="title_activity_open_bmi">OpenBMI</string>
    <string name="bmi">BMI</string>
    <string name="voortgang_streef">Voortgang/Streef</string>
    <string name="dagboek">Dagboek</string>
    <string name="setting">Setting</string>
    <string name="lengte">Lengte</string>
    <string name="gewicht">Gewicht</string>

    <string-array name="inputGender">
        <item>Man</item>
        <item>Vrouw</item>
    </string-array>

</resources>

Logcat (took me a while to add manifest and the logcat cause i'am still figuring android out a bit)

java.lang.RuntimeExceptionUnable to start activity ComponentInfo{com.example.practicum/com.example.practicum.OpenBMI}java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
at android.app.ActivityThread.access$600(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5041)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
at dalvik.system.NativeStart.main(Native Method)    Caused byjava.lang.NullPointerException
at com.example.practicum.OpenBMI.onCreate(OpenBMI.java:55)
at android.app.Activity.performCreate(Activity.java:5104)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)

Manifest

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.practicum"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="10"
    android:targetSdkVersion="19" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.example.practicum.MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name="com.example.practicum.OpenBMI"
        android:label="@string/title_activity_open_bmi" >
    </activity>
</application>

</manifest>
Onik
  • 19,396
  • 14
  • 68
  • 91
TApatty
  • 7
  • 2

3 Answers3

0

In your main activity you should be finding the spinner like you do for the second one:

Spinner genderSpinner = (Spinner) findViewById(R.id.spinGender);

The first spinner in your MainActivity is trying to cast an EditText to a Spinner object (Problem where it is crashing)

It is hard to say because you didn't post the exact error you are getting. Edit your post with your Logcat output and it will be easier to diagnose the problem.

Brandon
  • 693
  • 1
  • 9
  • 23
  • Yea, I commented the whole first spinner to see if the second one was working so I could check if the problem was in my Activity code, or somewhere else. But i got the same problem with the second spinner aswell. i added my manifest and part of the log (which i thought that mattered). – TApatty May 22 '14 at 19:26
  • is activity_open_bmi.xml the layout you are showing on the page? Because I think it has to do with the fact that your contentView is not the fragment you want to use. – Brandon May 22 '14 at 20:15
  • I facepalmed myself so hard here. Thanks allot for pointing that 1 out for me. the contentView was set on my activity_open_bmi.xml layout, when i stuffed everything in my fragment_open_bmi.xml. – TApatty May 22 '14 at 20:44
0

You could do this in your code :

XML

<Spinner
  android:id="@+id/spinner"
  android:layout_width="wrap_content"
  android:layout_height="match_parent" />

Java

public class MainActivity extends Activity implements OnItemSelectedListener{

private Spinner spinner;
private static final String[]paths = {"item 1", "item 2", "item 3"};

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

    spinner = (Spinner)findViewById(R.id.spinner);
    ArrayAdapter<String>adapter = new ArrayAdapter<String>("CLASS-NAME".this,
            android.R.layout.simple_spinner_item,paths);

    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner.setAdapter(adapter);
    spinner.setOnItemSelectedListener(this);

}

public void onItemSelected(AdapterView<?> parent, View v, int position, long id) {

    switch (position) {
        case 0:
            // What ever you want to happen when item 1 selected
            break;
        case 1:
            // What ever you want to happen when item 2 selected
            break;
        case 2:
            // What ever you want to happen when item 3 selected
            break;

    }
}
}
Farouk Touzi
  • 3,451
  • 2
  • 19
  • 25
0

Your spinner is in the fragment layout and not in the activity layout you inflate in onCreate(). That's why it's not found. You should be accessing fragment views in its onCreateView() at earliest.

You also seem to have deleted the PlaceholderFragment fragment transaction generated by the code template that would make the fragment to get eventually instantiated.

  1. Restore the fragment transaction in onCreate().

  2. Move the findViewById() etc. code to the fragment's onCreateView().

See NullPointerException accessing views in onCreate() for more.

Community
  • 1
  • 1
laalto
  • 150,114
  • 66
  • 286
  • 303