1

我有两个旋转器。一个用于高度单位,第二个用于重量单位。在我的布局上有两个高度和重量的编辑文本字段。我想在选择某个微调器项目时隐藏一个。我是新手,所以任何帮助将不胜感激。提前致谢。

IE 如果在高度微调器中选择了 CM,我想隐藏第二个高度编辑文本字段。

我的微调器值是我的字符串文件中的一个数组。

这是我的代码:

字符串.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);
    }

}

日志猫:

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

为每个微调器设置setOnItemSelectedListener为您完成这项工作......

IE 如果在高度微调器中选择了 CM,我想隐藏第二个高度编辑文本字段。

更新:您还需要设置 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 回答