1

スピナーを2つ持っています。1 つは高さの単位、もう 1 つは重量の単位です。私のレイアウトには、身長と体重の両方に 2 つの edittext フィールドがあります。特定のスピナーアイテムが選択されているときに非表示にしたいと思います。私はこれが初めてなので、どんな助けでも大歓迎です。前もって感謝します。

IE 高さスピナーで CM が選択されている場合、2 番目の高さ edittext フィールドを非表示にしたいと思います。

私のスピナー値は、文字列ファイルの配列です。

これが私のコードです:

文字列.xml

<string name="weight_kg">KG</string>
<string name="weight_lb">LB</string>
<string name="weight_st_lb">ST + LB</string>
<string name="height_ft_in">FT + IN</string>
<string name="height_cm">CM</string>
<string-array name="weight_spinner">        
    <item>@string/weight_kg</item>
    <item>@string/weight_lb</item>
    <item>@string/weight_st_lb</item>
</string-array>
<string-array name="height_spinner">        
    <item>@string/height_cm</item>
    <item>@string/height_ft_in</item>
</string-array>

Bmi.java

    package com.Health.Kicks.Calcs.Admob;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.support.v4.app.NavUtils;
import android.annotation.TargetApi;
import android.os.Build;

public class Bmi extends Activity {

    EditText height1, weight1, height, weight;
    Spinner height_spinner, weight_spinner;
    String heightInputString, weightInputString;
    Button calculatebmi;

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

            // Show the Up button in the action bar.
        setupActionBar();
        setupSpinners();
    }

    void setupSpinners() {

        height_spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                if (position == 0){
                    height1.setVisibility(View.GONE);
                } else {
                    height1.setVisibility(View.VISIBLE);
                }
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });

    }
    /**
     * Set up the {@link android.app.ActionBar}, if the API is available.
     */
    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    private void setupActionBar() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
            getActionBar().setDisplayHomeAsUpEnabled(true);
        }
    }

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

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case android.R.id.home:
            // This ID represents the Home or Up button. In the case of this
            // activity, the Up button is shown. Use NavUtils to allow users
            // to navigate up one level in the application structure. For
            // more details, see the Navigation pattern on Android Design:
            //
            // http://developer.android.com/design/patterns/navigation.html#up-vs-back
            //
            NavUtils.navigateUpFromSameTask(this);
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

}

LogCat:

09-07 09:19:55.366: E/AndroidRuntime(1056): FATAL EXCEPTION: main
09-07 09:19:55.366: E/AndroidRuntime(1056): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.Health.Kicks.Calcs.Admob/com.Health.Kicks.Calcs.Admob.Bmi}: java.lang.NullPointerException
09-07 09:19:55.366: E/AndroidRuntime(1056):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
09-07 09:19:55.366: E/AndroidRuntime(1056):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
09-07 09:19:55.366: E/AndroidRuntime(1056):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
09-07 09:19:55.366: E/AndroidRuntime(1056):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
09-07 09:19:55.366: E/AndroidRuntime(1056):     at android.os.Handler.dispatchMessage(Handler.java:99)
09-07 09:19:55.366: E/AndroidRuntime(1056):     at android.os.Looper.loop(Looper.java:137)
09-07 09:19:55.366: E/AndroidRuntime(1056):     at android.app.ActivityThread.main(ActivityThread.java:5103)
09-07 09:19:55.366: E/AndroidRuntime(1056):     at java.lang.reflect.Method.invokeNative(Native Method)
09-07 09:19:55.366: E/AndroidRuntime(1056):     at java.lang.reflect.Method.invoke(Method.java:525)
09-07 09:19:55.366: E/AndroidRuntime(1056):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
09-07 09:19:55.366: E/AndroidRuntime(1056):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
09-07 09:19:55.366: E/AndroidRuntime(1056):     at dalvik.system.NativeStart.main(Native Method)
09-07 09:19:55.366: E/AndroidRuntime(1056): Caused by: java.lang.NullPointerException
09-07 09:19:55.366: E/AndroidRuntime(1056):     at com.Health.Kicks.Calcs.Admob.Bmi.setupSpinners(Bmi.java:35)
09-07 09:19:55.366: E/AndroidRuntime(1056):     at com.Health.Kicks.Calcs.Admob.Bmi.onCreate(Bmi.java:30)
09-07 09:19:55.366: E/AndroidRuntime(1056):     at android.app.Activity.performCreate(Activity.java:5133)
09-07 09:19:55.366: E/AndroidRuntime(1056):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
09-07 09:19:55.366: E/AndroidRuntime(1056):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
09-07 09:19:55.366: E/AndroidRuntime(1056):     ... 11 more
09-07 09:19:55.366: E/AndroidRuntime(1056): FATAL EXCEPTION: main
09-07 09:19:55.366: E/AndroidRuntime(1056): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.Health.Kicks.Calcs.Admob/com.Health.Kicks.Calcs.Admob.Bmi}: java.lang.NullPointerException
09-07 09:19:55.366: E/AndroidRuntime(1056):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
09-07 09:19:55.366: E/AndroidRuntime(1056):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
09-07 09:19:55.366: E/AndroidRuntime(1056):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
09-07 09:19:55.366: E/AndroidRuntime(1056):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
09-07 09:19:55.366: E/AndroidRuntime(1056):     at android.os.Handler.dispatchMessage(Handler.java:99)
09-07 09:19:55.366: E/AndroidRuntime(1056):     at android.os.Looper.loop(Looper.java:137)
09-07 09:19:55.366: E/AndroidRuntime(1056):     at android.app.ActivityThread.main(ActivityThread.java:5103)
09-07 09:19:55.366: E/AndroidRuntime(1056):     at java.lang.reflect.Method.invokeNative(Native Method)
09-07 09:19:55.366: E/AndroidRuntime(1056):     at java.lang.reflect.Method.invoke(Method.java:525)
09-07 09:19:55.366: E/AndroidRuntime(1056):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
09-07 09:19:55.366: E/AndroidRuntime(1056):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
09-07 09:19:55.366: E/AndroidRuntime(1056):     at dalvik.system.NativeStart.main(Native Method)
09-07 09:19:55.366: E/AndroidRuntime(1056): Caused by: java.lang.NullPointerException
09-07 09:19:55.366: E/AndroidRuntime(1056):     at com.Health.Kicks.Calcs.Admob.Bmi.setupSpinners(Bmi.java:35)
09-07 09:19:55.366: E/AndroidRuntime(1056):     at com.Health.Kicks.Calcs.Admob.Bmi.onCreate(Bmi.java:30)
09-07 09:19:55.366: E/AndroidRuntime(1056):     at android.app.Activity.performCreate(Activity.java:5133)
09-07 09:19:55.366: E/AndroidRuntime(1056):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
09-07 09:19:55.366: E/AndroidRuntime(1056):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
09-07 09:19:55.366: E/AndroidRuntime(1056):     ... 11 more
4

1 に答える 1

0

for each スピナーを設定して、setOnItemSelectedListenerこの作業を行います...

IE 高さスピナーで CM が選択されている場合、2 番目の高さ edittext フィールドを非表示にしたいと思います。

UPDATE:EditTexts、Spinner、およびボタンも設定する必要があります

EditText height1, weight1, height, weight;
Spinner height_spinner, weight_spinner;
String heightInputString, weightInputString;
Button calculatebmi;

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

    // set your class members as they start out null.
    // do this for all of them 
    height1 = (EditText) findViewById(R.id.idofheight1inxml);
    height_spinner = (Spinner) findViewById(R.id.idofheightspinnerinxml); 
    ....

        // Show the Up button in the action bar.
    setupActionBar();
    setupSpinners();
}


void setupSpinners(){
    height_spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
        //I.E. if in the height spinner CM is selected I would like to hide the second height edittext field. 
        // I'm not sure if this is meant to be "height1" or "height"
            if (position == 0){
                height.setVisibility(View.GONE);
            } else {
                height.setVisibility(View.VISIBLE);
            }
        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {

        }
    });

    // if you want to add similar logic for weight spinner, do that with this : 
    weight_spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            // put your code here for weight spinner
        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {

        }
    });
}
于 2013-09-06T20:25:02.537 に答える