尝试在空对象引用上调用虚拟方法'android.text.Editable android.widget.EditText.getText()'(单击按钮时崩溃)

时间:2015-09-18 15:16:54

标签: java android

你好我目前是Android工作室的新手,我遇到了这个问题,我完全陷入困境,我没有理想如何解决问题的应用程序工作正常,当我点击转换按钮它崩溃,请帮助

我得到的错误

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.text.Editable android.widget.EditText.getText()' on a null object reference
        at com.example.wxy.sg_cheapskate.MainActivity.onConvertButtonClicked(MainActivity.java:85)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at android.view.View$1.onClick(View.java:4002)
at android.view.View.performClick(View.java:4756)
at android.view.View$PerformClick.run(View.java:19749)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)

应用程序的Xml

<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:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="com.example.wxy.sg_cheapskate.converterActivity">


<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="Currency Converter"
    android:id="@+id/CurrencyConverter"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="139dp"
    android:textSize="30dp"
    android:textStyle="bold"/>

<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:inputType="numberDecimal"
    android:ems="10"
    android:id="@+id/NumberEditText"
    android:hint="Enter Value!"
    android:layout_below="@+id/CurrencyConverter"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:textSize="25dp"/>

<RadioGroup
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/NumberEditText"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:id="@+id/radioGroup"
    android:layout_marginTop="5dp">

    <RadioButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="USD TO SGD"
        android:id="@+id/Ustosg"
        android:checked="false"
        android:textSize="25dp"
        android:layout_marginTop="15dp"/>

    <RadioButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="RINGGIT TO SGD"
        android:id="@+id/Mytosg"
        android:checked="false"
        android:textSize="25dp"
        android:layout_marginTop="15dp"/>

    <RadioButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="YUAN TO SGD"
        android:id="@+id/prctosg"
        android:checked="false"
        android:textSize="25dp"
        android:layout_marginTop="15dp"/>

    <RadioButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="EURO TO SGD"
        android:id="@+id/eutosg"
        android:checked="false"
        android:textSize="25dp"
        android:layout_marginTop="15dp"/>

    <RadioButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="YEN TO SGD"
        android:id="@+id/japtosg"
        android:checked="false"
        android:textSize="25dp"
        android:layout_marginTop="15dp"/>
</RadioGroup>

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Convert"
    android:id="@+id/convertBtn"
    android:layout_below="@+id/radioGroup"
    android:layout_centerHorizontal="true"
    android:onClick="onConvertButtonClicked"
    android:textStyle="bold"
    android:textSize="25dp"
    android:layout_marginTop="10dp"/>

爪哇

public class MainActivity extends AppCompatActivity implements ActionBar.TabListener, NavigationView.OnNavigationItemSelectedListener {





SectionPagerAdapter mSectionsPagerAdapter;

ViewPager mViewPager;

static final String LOG_TAG = "SlidingTabsBasicFragment";

private SlidingTabLayout mSlidingTabLayout;
private DrawerLayout mDrawerLayout;
private ActionBarDrawerToggle mDrawerToggle;
private Toolbar mToolbar;
private NavigationView mDrawer;
private EditText Number;
private RadioButton usd2sgd;
private RadioButton ringgit2sgd;
private RadioButton yuan2sgd;
private RadioButton euro2sgd;
private RadioButton yen2sgd;




protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mToolbar = (Toolbar)findViewById(R.id.app_bar);
    setSupportActionBar(mToolbar);
    mDrawer = (NavigationView) findViewById(R.id.main_drawer);
    mDrawer.setNavigationItemSelectedListener(this);
    mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
    mDrawerToggle = new ActionBarDrawerToggle(this,
            mDrawerLayout,
            mToolbar,
            R.string.drawer_open,
            R.string.drawer_close);
    mDrawerLayout.setDrawerListener(mDrawerToggle);
    mDrawerToggle.syncState();

    mSectionsPagerAdapter = new SectionPagerAdapter(getSupportFragmentManager());
    mViewPager = (ViewPager) findViewById(R.id.pager);
    mViewPager.setAdapter(mSectionsPagerAdapter);
    mSlidingTabLayout = (SlidingTabLayout) findViewById(R.id.sliding_tabs);
    mSlidingTabLayout.setViewPager(mViewPager);

    Number = (EditText)findViewById(R.id.NumberEditText);
    usd2sgd = (RadioButton)findViewById(R.id.Ustosg);
    ringgit2sgd = (RadioButton)findViewById(R.id.Mytosg);
    yuan2sgd = (RadioButton)findViewById(R.id.prctosg);
    euro2sgd = (RadioButton)findViewById(R.id.eutosg);
    yen2sgd = (RadioButton)findViewById(R.id.japtosg);
}

public void onConvertButtonClicked (View v) {
    double value = new Double(Number.getText().toString());

    if (usd2sgd.isChecked())
            value = ConvertMoney.UsTosg(value);
    if (ringgit2sgd.isChecked())
        value = ConvertMoney.MyTosg(value);
    if (yuan2sgd.isChecked())
        value = ConvertMoney.PrcTosg(value);
    if (euro2sgd.isChecked())
        value = ConvertMoney.EuTosg(value);
    if (yen2sgd.isChecked())
        value = ConvertMoney.JapTosg(value);
    Number.setText(new Double(value).toString());

}

3 个答案:

答案 0 :(得分:0)

首先关注李的评论。

其次,将String投射到double,如下所示:

double value = Double.parseDouble("Double string here");

答案 1 :(得分:0)

Shubham试图说的是将变量Number重命名为其他内容,让我们说num然后将Number的每次出现更改为num。原因是,Number是一个java类,不能用作变量名。

num= (EditText) findViewById(R.id.NumberEditText);
...
double value = Double.parseDouble(num.getText());
...
num.setText(String.valueOf(value));

答案 2 :(得分:0)

如果您在未初始化对象的情况下使用对象,则会抛出该异常。检查您是否已正确初始化Number对象。您可以通过执行以下操作来检查Number是否已初始化:

if(Number!=null){
        //stuff here
    }