空指针异常

时间:2018-09-08 16:15:49

标签: android listview nullpointerexception android-edittext

我想构建一个应用程序,该应用程序从ListView中的EditText输入。 基本上,我有三列,每列中都有ediText.Now,当我尝试从用户那里获取输入并将其存储在String类型的变量中时,当我运行此应用程序时,由于获取NullPointerException而崩溃。

这是我的MainActivity.java

package com.example.manuj.autocalc;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

    EditText editTotal;
    EditText editItem;
    EditText editAmt;
    String item;
    String amt;
    String total;
    ListView listView;
    CustomAdapter customAdapter;

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

        editItem=findViewById(R.id.itemEdit);
        editTotal=findViewById(R.id.editTotal);
        editAmt=findViewById(R.id.amtEdit);

        item=editItem.getText().toString();
        total=editTotal.getText().toString();
        amt=editAmt.getText().toString();

        final ArrayList<Calc> calcs=new ArrayList<>();
        calcs.add(new Calc("    -","    -","     -"));
        calcs.add(new Calc("    -","    -","     -"));
        calcs.add(new Calc("    -","    -","     -"));
        calcs.add(new Calc("    -","    -","     -"));
        calcs.add(new Calc("    -","    -","     -"));

        listView=findViewById(R.id.listView);
        customAdapter=new CustomAdapter(this,-1,calcs);
        listView.setAdapter(customAdapter);
//        Calc calc=new Calc(item,amt,total);
//        calcs.add(calc);
        customAdapter.notifyDataSetChanged();







    }
}

这是我的CustomAdapter类

package com.example.manuj.autocalc;

import android.content.Context;
import android.support.annotation.NonNull;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.EditText;

import java.util.ArrayList;


public class CustomAdapter extends ArrayAdapter<Calc> {
    public CustomAdapter(Context context, int resource, ArrayList<Calc> calcs) {
        super(context, resource, calcs);
    }
    @Override
    public View getView(int position, View view, ViewGroup parent) {

        Calc calc = getItem(position);

        if (view == null){
            view = LayoutInflater.from(getContext()).inflate(
                    R.layout.list_item, parent, false);
        }

        //get and set the items
        EditText item=(EditText)view.findViewById(R.id.itemEdit);
        item.setText(calc.getmItemEdit());

        //get and set the amt
        EditText amt=view.findViewById(R.id.amtEdit);
        amt.setText(calc.getmAmtEdit());

        //get and set the totalAmt
        EditText total=view.findViewById(R.id.editTotal);
        total.setText(calc.getmTotalEdt());

        return view;
    }
}

这是我的模型课

package com.example.manuj.autocalc;

import android.text.Editable;
import android.widget.EditText;

public class Calc {
    private String mItemEdit;
    private String mTotalEdt;
    private String mAmtEdit;

    Calc(String itemEdit,String totalEdit,String amtEdit){
        this.mItemEdit=itemEdit;
        this.mTotalEdt=totalEdit;
        this.mAmtEdit=amtEdit;
    }

    public String getmItemEdit(){return mItemEdit; }

    public void setmItemEdit(String mItemEdit){
        this.mItemEdit=mItemEdit;
    }

    public String getmTotalEdt(){return mTotalEdt; }

    public void setmTotalEdt(String mTotalEdt){
        this.mTotalEdt=mTotalEdt;
    }

    public String getmAmtEdit(){return mAmtEdit;}

    public void setmAmtEdit(String mAmtEdit){
        this.mAmtEdit=mAmtEdit;
    }

}

这是我的错误

E/AndroidRuntime: FATAL EXCEPTION: main
              Process: com.example.manuj.autocalc, PID: 6618
              java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.manuj.autocalc/com.example.manuj.autocalc.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.text.Editable android.widget.EditText.getText()' on a null object reference
                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2984)
                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3045)
                  at android.app.ActivityThread.-wrap14(ActivityThread.java)
                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1642)
                  at android.os.Handler.dispatchMessage(Handler.java:102)
                  at android.os.Looper.loop(Looper.java:154)
                  at android.app.ActivityThread.main(ActivityThread.java:6776)
                  at java.lang.reflect.Method.invoke(Native Method)
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1518)
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1408)
               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.manuj.autocalc.MainActivity.onCreate(MainActivity.java:36)
                  at android.app.Activity.performCreate(Activity.java:6956)
                  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1126)
                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2927)
                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3045) 
                  at android.app.ActivityThread.-wrap14(ActivityThread.java) 
                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1642) 
                  at android.os.Handler.dispatchMessage(Handler.java:102) 
                  at android.os.Looper.loop(Looper.java:154) 
                  at android.app.ActivityThread.main(ActivityThread.java:6776) 
                  at java.lang.reflect.Method.invoke(Native Method) 
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1518) 
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1408) 

应用程序终止。

2 个答案:

答案 0 :(得分:1)

在加载Activity时,您正在从EditText中读取文本,然后添加了.toString()。首先.getText()返回Null,然后将Null转换为String。因此崩溃。

为避免此崩溃

只放空支票

if(your_edit_text.getText() != null)
 {
   //Then do your operation
 }

答案 1 :(得分:0)

看起来itemItem,editTotal,amtEdit属于您的list_item.xml布局,而不是activity_main.xml。您已经在MainActivity类中初始化了这三个EditText,这可能是它引发错误的原因。但是无论如何,请提供您的xml布局。