java.lang.NullPointerException:尝试在空对象引用上

时间:2019-06-24 06:10:47

标签: java android

  

java.lang.NullPointerException:尝试调用接口方法   空对象上的'boolean java.util.List.add(java.lang.Object)'   参考

我需要对主XML的每个编辑文本进行验证,并且保存按钮也会滚动并且日期显示在日期的文本视图中,保存后,应将人员详细信息添加到列表视图顶部的下一个屏幕中,列表视图中已经有五个默认人的详细信息,我无法做到这一点,请帮帮我,在列表视图中,每个项目都应包含“更新和删除”按钮,单击“更新”按钮应打开并需要在按钮中进行编辑,“删除”意味着删除人的细节从列表视图中。

  

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <ScrollView
        android:id="@+id/scroll_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <LinearLayout
            android:id="@+id/linearlayout1"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

            <TextView
                android:id="@+id/tv_name1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="30dp"
                android:layout_marginTop="15dp"
                android:layout_marginRight="30dp"
                android:padding="15dp"
                android:text="Name:"
                android:textSize="15sp" />

            <EditText
                android:id="@+id/et_name1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="30dp"
                android:layout_marginRight="30dp"
                android:hint="Enter Your Name"
                android:padding="15dp"
                android:textSize="15sp" />

            <TextView
                android:id="@+id/tv_email"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="30dp"
                android:layout_marginRight="30dp"
                android:padding="15dp"
                android:text="Email:"
                android:textSize="15dp" />

            <EditText
                android:id="@+id/et_email"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="30dp"
                android:layout_marginRight="30dp"
                android:hint="Enter Your Mail"
                android:textSize="15dp" />

            <TextView
                android:id="@+id/tv_phoneno"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="30dp"
                android:layout_marginRight="30dp"
                android:padding="15dp"
                android:text="Phone No:"
                android:textSize="15dp" />

            <EditText
                android:id="@+id/et_phoneno"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="30dp"
                android:layout_marginRight="30dp"
                android:hint="Enter your Contact No"
                android:padding="15dp"
                android:textSize="15dp" />

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="horizontal"
                android:padding="15dp"
                android:weightSum="2">

                <TextView
                    android:id="@+id/tv_dob"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="30dp"
                    android:layout_marginRight="30dp"
                    android:layout_weight="1"
                    android:text="DOB:"
                    android:textSize="15dp"

                    />

                <Button
                    android:id="@+id/datepicker1"
                    android:layout_width="0dp"
                    android:layout_height="30dp"
                    android:layout_marginLeft="30dp"
                    android:layout_marginRight="70dp"
                    android:layout_weight="1"
                    android:textSize="15dp">

                </Button>

            </LinearLayout>

            <TextView
                android:id="@+id/tv_qualification"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_marginLeft="30dp"
                android:layout_marginRight="30dp"
                android:padding="15dp"
                android:text="Qualification"
                android:textSize="15dp" />

            <EditText
                android:id="@+id/et_qualification"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="30dp"
                android:layout_marginRight="30dp"
                android:hint="Enter Your Qualification"
                android:padding="15dp"
                android:textSize="15dp" />

            <LinearLayout
                android:id="@+id/linearlayout2"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="horizontal"
                android:padding="15dp"
                android:weightSum="2">

                <TextView
                    android:id="@+id/tv_time"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="30dp"
                    android:layout_marginRight="30dp"
                    android:layout_weight="1"
                    android:text="Time:"
                    android:textSize="15dp"

                    />

                <Button
                    android:id="@+id/timepicker1"
                    android:layout_width="0dp"
                    android:layout_height="30dp"
                    android:layout_marginLeft="30dp"
                    android:layout_marginRight="70dp"
                    android:layout_weight="1"
                    android:textSize="15dp">

                </Button>

            </LinearLayout>

            <Button
                android:id="@+id/button_save"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Save"
                android:layout_gravity="center"
                android:textSize="15dp">

            </Button>
        </LinearLayout>
    </ScrollView>


</RelativeLayout
  

Person.java

package com.example.android.persondetails;

public class Person {
    private String name;
    private String contactno;
    private String email;
    private String qualification;
    private String datepicker;
    private String timepicker;



    public Person(String name, String contactno, String email, String qualification, String datepicker, String timepicker) {
        this.name = name;
        this.contactno = contactno;
        this.qualification=qualification;
        this.datepicker=datepicker;
        this.email=email;
        this.timepicker=timepicker;


    }

    public String getName() {
        return name == null ? "" : name;
    }

    public void setName(String name) {
        this.name = name;
    }


    public String  getContactno() {
        return contactno;
    }

    public void  setContactno(String contactno){
        this.contactno=contactno;
    }
    public String getEmail() {
        return email;
    }

    public String getQualification() {
        return qualification;
    }

    public String getDatepicker() {
        return datepicker;
    }

    public String getTimepicker() {
        return timepicker;
    }
}
  

SecondActivity.java

package com.example.android.persondetails;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import static java.util.Calendar.AM;

public class SecondActivity extends AppCompatActivity {
    private ListView lvContact;
    private Button addBtn,dleBtn;
    private PersonListAdapter adapter;
    private List<Person> mPersonList;


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

       ArrayList<Person> mPersonList = new ArrayList<Person>();
        addBtn = (Button) findViewById(R.id.update);
        dleBtn=(Button)findViewById(R.id.delete);
        lvContact = (ListView) findViewById(R.id.listView);

        addMethod();
        setAdapter();

        Intent intent = getIntent();
        String text;
        text = (String) intent.getStringExtra(MainActivity.EXTRA_NAME);
        String text1 = (String) intent.getStringExtra(MainActivity.EXTRA_EMAIL);
        String text3 = (String) intent.getStringExtra(MainActivity.EXTRA_PHONE_NUMBER);
        String text4 = (String) intent.getStringExtra(MainActivity.EXTRA_DOB);
        String text5 = (String) intent.getStringExtra(MainActivity.EXTRA_TIME);
        String text2 = (String)intent.getStringExtra(MainActivity.EXTRA_QUALIFICATION);

        Log.d("PersonDetails", text + " " + text1 + "" + text2 + "" + text3 + "" + text4 + "" + text5);

    }

    private void addMethod() {

        mPersonList.add(new Person("Narendra", "83330072", "narendraprasad616@gmail.com", "btech", "14 / 06 / 1996", "5-30 AM"));
        mPersonList.add(new Person("Banu", "715452", "naredra@gmail.com", "btech", "14 / 06 / 1996", "5-45 AM"));
        mPersonList.add(new Person("Kalyan", "83330072", "narendraprad616@gmail.com", "btech", "14 / 06 / 1998", "5-30 AM"));
        mPersonList.add(new Person("Teja", "8775564", "narendraprasa16@gmail.com", "btech", "14 / 06 / 1990", "5-30 AM"));
        mPersonList.add(new Person("Manu", "86423", "narendraprad616@gmail.com", "btech", "14 / 06 / 1996", "5-30 AM"));

    }


    private void setAdapter() {
        adapter = new PersonListAdapter(SecondActivity.this, mPersonList);
        lvContact.setAdapter(adapter);
    }
}
  

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.android.persondetails">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme"
        >
        <activity android:name=".SecondActivity">
    </activity>
        <activity android:name=".MainActivity"
            android:parentActivityName=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>
  

PersonListAdapter.java

package com.example.android.persondetails;


import android.content.Context;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import java.util.List;

public class PersonListAdapter extends BaseAdapter {

    private Context mContext;
    private List<Person> mPersonList;

    PersonListAdapter(Context mContext, List<Person> mPersonList) {
        this.mContext = mContext;
        this.mPersonList = mPersonList;
    }



    @Override
    public int getCount() {
        return mPersonList.size();
    }

    @Override
    public Object getItem(int position) {
        return mPersonList.get(position);
    }


    @RequiresApi(api = Build.VERSION_CODES.P)
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = LayoutInflater.from(mContext).inflate(R.layout.second_layout, parent, false);
            holder.tv_name = (TextView) convertView.findViewById(R.id.tv_name1);
            holder.tv_contactno = (TextView) convertView.findViewById(R.id.tv_phoneno);
            holder.tv_email=(TextView)convertView.findViewById(R.id.tv_email);
            holder.tv_dob=(TextView)convertView.findViewById(R.id.tv_dob);
            holder.tv_qualification=(TextView)convertView.findViewById(R.id.tv_qualification);
            holder.tv_time=(TextView)convertView.findViewById(R.id.tv_time);

            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.tv_name.setText(mPersonList.get(position).getName());
        holder.tv_contactno.setText(mPersonList.get(position).getContactno());
        holder.tv_email.setText(mPersonList.get(position).getEmail());
        holder.tv_dob.setText(mPersonList.get(position).getDatepicker());
        holder.tv_qualification.setText(mPersonList.get(position).getQualification());
        holder.tv_time.setText(mPersonList.get(position).getTimepicker());
        return convertView;

    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    class ViewHolder {
        TextView tv_name;
        TextView tv_email;
        TextView tv_contactno;
        TextView tv_dob;
        TextView tv_qualification;
        TextView tv_time;

    }

}
  

second_layout.xml

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

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/tv_name1"
        android:text="Name"
        android:textSize="15dp"
        />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/tv_phoneno"
        android:layout_below="@+id/tv_name1"
        android:text="contactno"
        android:textSize="15dp"
        />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/tv_phoneno"
        android:id="@+id/tv_email"
        android:text="Email"
        android:textSize="15dp"
        />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/tv_dob"
        android:layout_below="@+id/tv_email"
        android:text="DOB"
        android:textSize="15dp"
        />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/tv_qualification"
        android:layout_below="@+id/tv_dob"
        android:text="Qualification"
        android:textSize="15dp"
        />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/tv_time"
        android:layout_below="@+id/tv_qualification"
        android:text="Time"
        android:textSize="15dp"
        />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal"
        android:layout_marginTop="120dp"
        android:weightSum="2"
        >

        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_below="@+id/tv_time"
            android:layout_weight="1"
            android:textSize="15dp"
            android:text="Delete"
            android:padding="10dp"
            android:id="@+id/delete"
            android:layout_marginLeft="10dp"
            />


        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:textSize="15dp"
            android:text="Upadate"
            android:padding="10dp"
            android:id="@+id/update"
             android:layout_marginLeft="10dp"
            />

    </LinearLayout>


</RelativeLayout>
  

list_view.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".Person">

    <ListView
        android:id="@+id/listView"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    </ListView>

</RelativeLayout>

MainActivity.java

package com.example.android.persondetails;

import android.app.DatePickerDialog; import android.app.TimePickerDialog; import android.content.Intent; import android.icu.text.SimpleDateFormat; import android.os.Build; import android.support.annotation.RequiresApi; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.DatePicker; import android.widget.EditText; import android.widget.TextView; import android.widget.TimePicker;

import java.text.ParseException; import java.util.Calendar; import java.util.Date;

import static android.content.Intent.EXTRA_EMAIL; import static android.content.Intent.EXTRA_PHONE_NUMBER; import static android.os.DropBoxManager.EXTRA_TIME; import static android.security.KeyChain.EXTRA_NAME;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    public static final String EXTRA_NAME = "com.example.android.persondetails.EXTRA_NAME";
    public static final String EXTRA_EMAIL = "com.example.android.persondetails.EXTRA_EMAIL";
    public static final String EXTRA_PHONE_NUMBER = "com.example.android.persondetails.EXTRA_PHONE_NUMBER";
    public static final String EXTRA_DOB = "com.example.android.persondetails.EXTRA_DOB";
    public static final String EXTRA_QUALIFICATION = "com.example.android.persondetails.EXTRA_QUALIFICATION";
    public static final String EXTRA_TIME = "com.example.android.persondetails.EXTRA_TIME";
    Button button1, button2, button3;
    TextView textView1, textView2, textView3, textView4, textView5, textView6;
    EditText editText1, editText2, editText3, editText4;
    private int mYear, mMonth, mDay, mHour, mMinute, format;


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

        button3 = (Button) findViewById(R.id.button_save);
        textView1 = (TextView) findViewById(R.id.tv_name1);
        textView2 = (TextView) findViewById(R.id.tv_email);
        textView3 = (TextView) findViewById(R.id.tv_phoneno);
        textView4 = (TextView) findViewById(R.id.tv_dob);
        textView5 = (TextView) findViewById(R.id.tv_qualification);
        textView6 = (TextView) findViewById(R.id.tv_time);
        button1 = (Button) findViewById(R.id.datepicker1);
        button2 = (Button) findViewById(R.id.timepicker1);


        button1.setOnClickListener(this);
        button2.setOnClickListener(this);
        button3.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {

        if (v == button1) {

            final Calendar c = Calendar.getInstance();
            mYear = c.get(Calendar.YEAR);
            mMonth = c.get(Calendar.MONTH);
            mDay = c.get(Calendar.DAY_OF_MONTH);
            format = c.get(Calendar.AM_PM);


            DatePickerDialog datePickerDialog = new DatePickerDialog(this,
                    new DatePickerDialog.OnDateSetListener() {

                        @Override
                        public void onDateSet(DatePicker view, int year,
                                              int monthOfYear, int dayOfMonth) {

                            textView4.setText(dayOfMonth + "-" + (monthOfYear + 1) + "-" + year);

                        }
                    }, mYear, mMonth, mDay);
            datePickerDialog.show();
        }
        if (v == button2) {

            final Calendar c = Calendar.getInstance();
            mHour = c.get(Calendar.HOUR_OF_DAY);
            mMinute = c.get(Calendar.MINUTE);
            format = c.get(Calendar.AM_PM);
            final TimePickerDialog timePickerDialog;
            timePickerDialog = new TimePickerDialog(this,
                    new TimePickerDialog.OnTimeSetListener() {

                        @RequiresApi(api = Build.VERSION_CODES.N)
                        @Override
                        public void onTimeSet(TimePicker timePicker, int selectedHour, int selectedMinute) {


                            String time = selectedHour + ":" + selectedMinute;

                            SimpleDateFormat fmt = new SimpleDateFormat("HH:mm");
                            Date date = null;
                            try {
                                date = fmt.parse(time);
                            } catch (ParseException e) {

                                e.printStackTrace();
                            }

                            SimpleDateFormat fmtOut = new SimpleDateFormat("hh:mm aa");

                            String formattedTime = fmtOut.format(date);

                            textView6.setText(formattedTime);
                        }
                    }, mHour, mMinute, false);
            timePickerDialog.setTitle("Select Time");
            timePickerDialog.show();
        }


            if (v == button3) {
                EditText editText1 = (EditText) findViewById(R.id.et_name1);
                String text = editText1.getText().toString();
                EditText editText2 = (EditText) findViewById(R.id.et_email);
                String text1 = editText2.getText().toString();
                EditText editText3 = (EditText) findViewById(R.id.et_phoneno);
                String number = String.valueOf(editText3.getText().toString());
                Button button1 = (Button) findViewById(R.id.datepicker1);
                String number1 = button1.getText().toString();
                EditText editText4 = (EditText) findViewById(R.id.et_qualification);
                String text2 = editText4.getText().toString();
                Button button2 = (Button) findViewById(R.id.timepicker1);
                String number2 = button2.getText().toString();
                Intent intent = new Intent(MainActivity.this, SecondActivity.class);
                intent.putExtra(EXTRA_NAME, text);
                intent.putExtra(EXTRA_EMAIL, text1);
                intent.putExtra(EXTRA_PHONE_NUMBER, number);
                intent.putExtra(EXTRA_DOB, number1);
                intent.putExtra(EXTRA_QUALIFICATION, text2);
                intent.putExtra(EXTRA_TIME, number2);
                startActivity(intent);

            }


        }
    }

1 个答案:

答案 0 :(得分:0)

我注意到以下评论中提到的一个主要问题。

    public class SecondActivity extends AppCompatActivity {
    private ListView lvContact;
    private Button addBtn,dleBtn;
    private PersonListAdapter adapter;
    private List<Person> mPersonList;


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

        //Error as creating a new object mPersonList which is local to onCreate()
        //Finally class variable mPersonList will be null still.

        //ArrayList<Person> mPersonList = new ArrayList<Person>(); //Logical Error 

        //Solution: Use class variable instead of creating a new variable. So, just replace the above commented line with below one.
        mPersonList = new ArrayList<Person>();

        addBtn = (Button) findViewById(R.id.update);

        ..... continue
    }

希望它能解决您的问题。