需要输入时,不会显示Android布局

时间:2016-03-13 14:46:03

标签: android

我有一个使用多个活动的应用。一项活动是手动输入一些值,我在该活动中有四个按钮。单击其中一个按钮时,它会启动一个新活动以获取输入。

问题是当运行此活动的get_input()函数时,不会显示活动的布局。它应该从EditText获取十进制值。但我一直收到运行时错误" Invalid Float"""。

如果我注释掉get_input()函数,那么活动的布局就会很好。

这是xml文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/manually_entry" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="Please enter the decimal Latitude"
        android:singleLine="true"
        />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="Negative is South"
        android:singleLine="true"
        />

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:inputType="numberDecimal|numberSigned"
        android:id="@+id/enter_decimal_number" />

</LinearLayout>

这是调用活动:

@Override
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    setContentView(R.layout.lat_long_manual_entry);

}

// manual entry dialog buttons
public void enter_lat_dec_deg(View v) {

    Intent intent = new Intent(this.getApplicationContext(), ManuallyEnterNumbers.class);
    startActivity(intent);

}


And here is the called activity that requests the actual input from the user :

    package com.example.david.gpstest;

import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.TextView;

/**
 * Created by david on 06/03/16.
 */
public class ManuallyEnterNumbers extends Activity {

    private EditText input;

    private float decimal_input;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.manually_enter_numbers);

    }


    @Override
    protected void onStart() {
        super.onStart();
    }


    @Override
    protected void onResume() {
        super.onResume();
        get_input();
     }


        public void get_input() {

            input = (EditText) findViewById(R.id.enter_decimal_number);
            input.setOnEditorActionListener(new TextView.OnEditorActionListener() {
                @Override
                public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                    if (actionId == EditorInfo.IME_ACTION_DONE) {

                        decimal_input = Float.valueOf(input.getText().toString());

                        return true;
                    }
                    return false;
                }
            });

    }

}

我在onStart()方法中显示了函数get_input(),但是IO也在onResume()方法中尝试了它,并且它也不起作用。

非常感谢任何帮助。

                 Thank You.

2 个答案:

答案 0 :(得分:0)

  (input.getText()的toString())

Float.valueOf;

这里新手的常见错误是在尝试转换之前缺乏字符串清理。即使,(逗号)而不是作为小数部分分隔符输入的.(点),也会引发异常。因此,您只需要使字符串可转换(即通过检查内部的内容,将,替换为.等),更不用说您应该期待异常来使用try/catch来捕获它以避免应用你现在面临崩溃。

答案 1 :(得分:0)

大卫,你好,这是你自己。基本上你可以忘记清理输入。因为您的xml中有android:inputType="numberDecimal|numberSigned",所以您将获得一个带逗号的数字小键盘,并禁用空格。所以用户可以输入的是小数点和减号。正是你想要的。其次,您可以忘记onStart()onResume()方法,并将所有内容都放在onCreate()中,然后它就会起作用。你的xml绝对没问题。您的调用活动也绝对正常,尽管使用intent.putExtra()intent.getExtra()将decimal_input值传递回调用活动是个好主意。我已经发布了下面的调用活动和被调用活动的代码来帮助你。

    public class ManualEntry extends Activity {

//    private View layout;

//    private PopupWindow enter_manual_dec_deg;

//    LayoutInflater inflater;


//    private EditText input;

//    private float decimal_input;

//    private String result;

    static final int REQUEST_CODE = 1;

    String button_pressed;


    /**
     * Called when the activity is first created.
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.lat_long_manual_entry);

    }

    @Override
    protected void onResume() {
        super.onResume();
    }


    // manual entry dialog buttons
    public void enter_lat_dec_deg(View v) {

        Intent intent = new Intent(this.getApplicationContext(), ManuallyEnterNumbers.class);

        button_pressed = "lat_dec_deg";

        intent.putExtra("Button pressed", button_pressed);
        startActivityForResult(intent, REQUEST_CODE);

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_CODE) {
            if (resultCode == RESULT_OK) {
                double result = data.getDoubleExtra("returnData", 0.0d);

                if (result < -180 || result > 180) {
                    Toast.makeText(this, "Value outside range - Range is -180 to 180", Toast.LENGTH_LONG).show();
                }
                else {
                    DecimalFormat var = new DecimalFormat("###.########");

                    Toast.makeText(this, var.format(result), Toast.LENGTH_LONG).show();

                    disable_button(button_pressed);
                }
            }
        }
    }

    void disable_button(String pressed_button) {
        Button dec_deg_button = (Button) findViewById(R.id.button_lat_dec_deg);
        Button d_m_s_button = (Button) findViewById(R.id.button_lat_deg_min_sec);

        if (pressed_button != null) {
            if (pressed_button == "lat_dec_deg" || button_pressed == "lat_d_m_s") {
                dec_deg_button.setEnabled(false);
                d_m_s_button.setEnabled(false);
            }
        }
    }

}

   /*    
    public void enter_lat_d_m_s (View v) {

    }

    public void enter_lon_dec_deg(View v) {

    }

    public void enter_lon_d_m_s(View v) {

    }
*/

和被叫活动

    package com.example.david.gpstest;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

/**
 * Created by david on 06/03/16.
 */
public class ManuallyEnterNumbers extends Activity {

    private EditText input;

    private String inValue;

    private double decimal_input;

    private Bundle extras;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.manually_enter_numbers);

        extras = getIntent().getExtras();

        if (extras != null) {
            String buttonPressed = extras.getString("Button pressed");

            if (buttonPressed.contentEquals(buttonPressed) ) {
                ((TextView) findViewById(R.id.enter_decimal_number)).setHint("Decimal degrees of Latitude");

                TextView title_text = (TextView) findViewById(R.id.manual_entry_hint);
                title_text.setText("Please enter the decimal Latitude");

                TextView hint_text = (TextView) findViewById(R.id.manual_entry_hint);
                hint_text.setText("(Negative is South)");

            }
        }

        get_input();

    }


    @Override
    protected void onStart() {
        super.onStart();
//        get_input();

    }


    @Override
    protected void onResume() {
        super.onResume();

//        get_input();
     }


        public void get_input() {

            input = (EditText) findViewById(R.id.enter_decimal_number);

            input.setOnEditorActionListener(new TextView.OnEditorActionListener(){
                @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                    if (actionId == EditorInfo.IME_ACTION_DONE) {
                        // do stuff here

                        inValue = input.getText().toString();

                        if (inValue.isEmpty()) {
                            Toast.makeText(getApplicationContext(), "Enter a value", Toast.LENGTH_SHORT).show();
                        }
                        else {
                            // some input
                            decimal_input = Double.parseDouble(inValue);

                            Intent returnIntent = getIntent();
                            returnIntent.putExtra("returnData", decimal_input);
                            setResult(RESULT_OK, returnIntent);
                            finish();


                        }

                    }
                    return true;
                }

            });


/*
            Intent returnIntent = getIntent();
            returnIntent.putExtra("returnData", decimal_input);
            setResult(RESULT_OK, returnIntent);
            finish();
*/
    }



}

我希望这会对你有所帮助。

                   David.
相关问题