我无法输出一个整数到textView

时间:2016-06-02 13:07:55

标签: java android mobile

我正在尝试输入Miles Per Gallon Integer,然后简单地将该号码重新打印给用户。我已经阅读了无数的线程并观看了多个视频,但我仍然遇到同样的错误。我对Android开发非常新,所以非常感谢任何帮助。

package com.example.frank.trialapp;

import android.os.Bundle;
import android.sax.TextElementListener;
import android.support.annotation.IdRes;
import android.support.annotation.Nullable;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import org.w3c.dom.Text;

public class MainActivity extends AppCompatActivity {

private double num1;
private EditText MilesPerGallon;
private TextView MPG;
Button btnCalculate;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    MilesPerGallon = (EditText)findViewById(R.id.txtBox1);
    btnCalculate = (Button)findViewById(R.id.btnCalculate);

    btnCalculate.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if(MilesPerGallon.getText().toString().length() > 0 ) {
                num1 =    
  Double.parseDouble(MilesPerGallon.getText().toString());
                MPG.setText(Double.toString(num1));
            }
            else MPG.setText("failure");
        }
    });


    FloatingActionButton fab = (FloatingActionButton) 
 findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Snackbar.make(view, "Replace with your own action", 
 Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show();
        }
    });
}

@Override
public View findViewById(@IdRes int id) {
    return super.findViewById(id);
}

问题出在MPG.setText(Double.toString(num1));我只是不知道如何解决它......

错误日志:

  

致命的例外:主要                                                                             处理:com.example.frank.trialapp,PID:8293                                                                             显示java.lang.NullPointerException                                                                                 在   com.example.frank.trialapp.MainActivity $ 1.onClick(MainActivity.java:42)                                                                                 在android.view.View.performClick(View.java:4438)                                                                                 在android.view.View $ PerformClick.run(View.java:18422)                                                                                 在android.os.Handler.handleCallback(Handler.java:733)                                                                                 在android.os.Handler.dispatchMessage(Handler.java:95)                                                                                 在android.os.Looper.loop(Looper.java:136)                                                                                 在android.app.ActivityThread.main(ActivityThread.java:5017)                                                                                 at java.lang.reflect.Method.invokeNative(Native Method)                                                                                 在java.lang.reflect.Method.invoke(Method.java:515)                                                                                 在   com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:779)                                                                                 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)                                                                                 在dalvik.system.NativeStart.main(本地方法)

3 个答案:

答案 0 :(得分:0)

您需要在TextView

中初始化onCreate
 MPG= (TextView)findViewById(R.id.yourtextview);

答案 1 :(得分:0)

 MPG= (TextView)findViewById(R.id.yourtextview);
 MPG.setText("yourStringValue");
  if the value was integer
  MPG.setText(yourintValue+"");

答案 2 :(得分:0)

第一次,“MilesPerGallon.getText()”返回null。

在方法“onClick”中添加“if”