错误 - 尝试调用虚拟方法' android.text.Editable android.widget.EditText.getText()'在null对象引用上

时间:2018-04-10 07:28:12

标签: java android xml

为什么总会发生这种情况?
如果只有step1,可以正常运行
step1()' ak'中的错误

有人可以帮我解决这个问题吗? 我正在尝试计算输入值
但总是像这样的错误 java.lang.NullPointerException:尝试调用虚方法' android.text.Editable android.widget.EditText.getText()'在空对象引用上

  

CabActivity.java

    public class CabActivity extends AppCompatActivity {
    private TabLayout tabLayout;
    private SectionsPagerAdapter mSectionsPagerAdapter;
    private ViewPager mViewPager;

    EditText R1,R2,S1,S2,T1,T2,aktual;
    TextView H1,H2;
    Double   v11,v12,v21,v22,v31,v32,ak,out1,out2,arus1,arus2,rum1,rum2;
    String   act;

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

        mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
        mViewPager = (ViewPager) findViewById(R.id.container);
        mViewPager.setAdapter(mSectionsPagerAdapter);

        tabLayout = (TabLayout) findViewById(R.id.tabs);
        tabLayout.setupWithViewPager(mViewPager);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                Step1();
                Step2();
            }
        });

    }

    public void Step1(){
        R1     = (EditText) findViewById(R.id.nilaiR1);
        S1     = (EditText) findViewById(R.id.nilaiS1);
        T1     = (EditText) findViewById(R.id.nilaiT1);
        H1     = (TextView) findViewById(R.id.hasil1);
        aktual = (EditText) findViewById(R.id.nilaiAktual);

        v11 = Double.parseDouble(R1.getText().toString());
        v21 = Double.parseDouble(S1.getText().toString());
        v31 = Double.parseDouble(T1.getText().toString());
        ak  = Double.parseDouble(aktual.getText().toString());

        Double[] nilai = {v11,v21,v31};
        Double min=100.00;

        for (int a = 0; a < nilai.length;a++){
            if (nilai[a] < min){
                min=Double.parseDouble(nilai[a].toString());
            }
        }

        out1 = 50000.00*(ak/390.00);
        arus1 = Double.parseDouble(out1.toString())/658.00;
        rum1 = (min/arus1)*100.00;
        H1.setText(Double.toString(rum1));
    }

    public void Step2(){
        R2 = (EditText) findViewById(R.id.nilaiR2);
        S2 = (EditText) findViewById(R.id.nilaiS2);
        T2 = (EditText) findViewById(R.id.nilaiT2);
        H2 = (TextView) findViewById(R.id.hasil2);
        aktual = (EditText) findViewById(R.id.nilaiAktual);

        v12 = Double.parseDouble(R2.getText().toString());
        v22 = Double.parseDouble(S2.getText().toString());
        v32 = Double.parseDouble(T2.getText().toString());
        ak = Double.parseDouble(aktual.getText().toString());

        Double[] nilai = {v12,v22,v32};
        Double min=100.00;

        for (int a = 0; a < nilai.length;a++){
            if (nilai[a] < min){
                min=Double.parseDouble(nilai[a].toString());
            }
        }

        out2 = 50000.00*(ak/390.00);
        arus2 = Double.parseDouble(out2.toString())/658.00;
        rum2 = (min/arus2)*100.00;
        H2.setText(Double.toString(rum2));
    }

    public class SectionsPagerAdapter extends FragmentPagerAdapter {
        public SectionsPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            switch (position) {
                case 0:
                    return new actual();
                case 1:
                    return new step1();
                case 2:
                    return new step2();
            }

            return null;
        }

        @Override
        public int getCount() {
            return 3;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            switch (position) {
                case 0:
                    return "NILAI AKTUAL";
                case 1:
                    return "STEP 1";
                case 2:
                    return "STEP 2";                
            }
            return null;
        }
    }
} 
  

actual.xml

    <?xml version="1.0" encoding="utf-8"?>
<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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:windowSoftInputMode="adjustResize"
    tools:context="com.danki.engineering.MainActivity">

    <TextView
        android:id="@+id/textView2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_marginTop="9dp"
        android:gravity="center"
        android:text="@string/masukkan_tegangan_aktual"
        android:textSize="30sp" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="127dp"
        android:orientation="vertical">

        <EditText
            android:id="@+id/nilaiAktual"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:ems="10"
            android:inputType="numberDecimal" />
    </LinearLayout>

</RelativeLayout>

1 个答案:

答案 0 :(得分:0)

EditText中只有xml id nilaiAktual viewaktual活动绑定的EditText R1,R2,S1,S2,T1,T2, aktualir.attachement,这意味着如果您参考您的活动中的任何其他edittexts都会出现此错误。

在您的活动中,您定义了datas