应用程序在按下下一个按钮时使用Intent传递数据时崩溃(getStringExtra)

时间:2018-03-09 12:43:40

标签: java android nullpointerexception

我想将所有数据5(EditText)从活动1移动到2

公共类MainActivity扩展了Activity

 private EditText fName_ET;
    private EditText lName_ET;
    private EditText phoneNum_ET;
    private EditText eMail_ET;
    private EditText deviceModel_ET;
    private EditText nameDevice_ET;
    private Button nextPage;



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

       fName_ET = (EditText)findViewById(R.id.fName_ET);
       lName_ET = (EditText)findViewById(R.id.lName_ET);
       phoneNum_ET = (EditText)findViewById(R.id.phone_ET);
       eMail_ET = (EditText)findViewById(R.id.mail_ET);
       nameDevice_ET = (EditText)findViewById(R.id.name_device);
       deviceModel_ET = (EditText)findViewById(R.id.model_device);
       nextPage = (Button)findViewById(R.id.next_btn);

        nextPage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(MainActivity.this, Main2Activity.class);
                String fName =  fName_ET.getText().toString().trim();
                String lName =  lName_ET.getText().toString().trim();
                String email = eMail_ET.getText().toString().trim();
                String phone = phoneNum_ET.getText().toString().trim();
                String nameDevice = nameDevice_ET.getText().toString().trim();
                String modelDevice =deviceModel_ET.getText().toString().trim();
                intent.putExtra("modelDevice",modelDevice);
                intent.putExtra("fName", fName);
                intent.putExtra("lName", lName);
                intent.putExtra("phone", phone);
                intent.putExtra("email", email);
                intent.putExtra("nameDevice", nameDevice);
                startActivity(intent);
            }
        });

公共类Main2Activity扩展Activity

{

 package com.example.eranp.cp;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;

import java.sql.Date;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;

public class Main2Activity extends Activity {


    private EditText pro_device_det;
    private Button saveDataBase;
    private DatabaseReference databaseCustomer, databaseDevice, databaseProblem;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main3);

        databaseCustomer = FirebaseDatabase.getInstance().getReference("customers");
        databaseDevice = FirebaseDatabase.getInstance().getReference("device");
        databaseProblem = FirebaseDatabase.getInstance().getReference("problem");
        saveDataBase = (Button) findViewById(R.id.save_btn);


        pro_device_det = (EditText) findViewById(R.id.pro_device_det);
        Intent intent = getIntent();
        final String fName = intent.getStringExtra("fName");
        final String lName = intent.getStringExtra("lName");
        final String phone = intent.getStringExtra("phone");
        final String email = intent.getStringExtra("email");
        final String modelDevice = intent.getStringExtra("modelDevice");
        final String nameDevice = intent.getStringExtra("nameDevice");

        saveDataBase.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String proDeviceDet = pro_device_det.getText().toString().trim();
                String shortProDeviceDet = string3Words(proDeviceDet);
                DateFormat df = new SimpleDateFormat("d MMM yyyy, HH:mm");
                String date = df.format(Calendar.getInstance().getTime());
                if (!TextUtils.isEmpty(proDeviceDet)) {
                    String id = databaseCustomer.push().getKey();
                    Customer customer = new Customer(id, fName, lName, email, phone);
                    databaseCustomer.child(id).setValue(customer);
                    Device device = new Device(id, nameDevice, modelDevice);
                    databaseDevice.child(id).setValue(device);
                    Problem problem = new Problem(id, date, proDeviceDet, 0, shortProDeviceDet);

                    //  Toast.makeText(this , "Customer added", Toast.LENGTH_LONG).show();
                } else {
                    //  Toast.makeText(this, "Please write on an empty cell", Toast.LENGTH_LONG).show();
                }

            }
        });


    }

    private String string3Words(String s) {
        String[] splitted = s.split("\\s+");
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < splitted.length; i++) {
            sb.append(splitted[i]);
            if (i == 3) {
                break;
            }
        }
        String newS = sb.toString();
        return newS;
    }
}

我按下时记录:

  

down VM 03-09 14:47:16.903 3476-3476 / com.example.eranp.clientpage   E / AndroidRuntime:致命异常:主要                                                                               处理:com.example.eranp.clientpage,PID:3476                                                                               java.lang.RuntimeException:无法启动活动   ComponentInfo {com.example.eranp.clientpage / com.example.eranp.clientpage.Main2Activity}:   java.lang.NullPointerException:尝试调用虚方法'void   android.widget.Button.setOnClickListener(android.view.View $ OnClickListener)”   在null对象引用上                                                                                   在   android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2924)                                                                                   在   android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2985)                                                                                   在android.app.ActivityThread.-wrap14(ActivityThread.java)                                                                                   在   android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1635)                                                                                   在android.os.Handler.dispatchMessage(Handler.java:102)                                                                                   在android.os.Looper.loop(Looper.java:154)                                                                                   在android.app.ActivityThread.main(ActivityThread.java:6692)                                                                                   at java.lang.reflect.Method.invoke(Native Method)                                                                                   在   com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1468)                                                                                   在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1358)                                                                                引起:java.lang.NullPointerException:尝试调用虚拟   方法'无效   android.widget.Button.setOnClickListener(android.view.View $ OnClickListener)”   在null对象引用上                                                                                   在   com.example.eranp.clientpage.Main2Activity.onCreate(Main2Activity.java:50)                                                                                   在android.app.Activity.performCreate(Activity.java:6912)                                                                                   在   android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1126)                                                                                   在   android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2877)                                                                                   在   android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2985)                                                                                   在android.app.ActivityThread.-wrap14(ActivityThread.java)                                                                                   在   android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1635)                                                                                   在android.os.Handler.dispatchMessage(Handler.java:102)                                                                                   在android.os.Looper.loop(Looper.java:154)                                                                                   在android.app.ActivityThread.main(ActivityThread.java:6692)                                                                                   at java.lang.reflect.Method.invoke(Native Method)                                                                                   在   com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1468)                                                                                   在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1358)

为什么当我按下应用程序崩溃的下一个按钮? 谢谢你的帮助!

2 个答案:

答案 0 :(得分:1)

您正尝试在Main2Activity中的按钮上设置单击侦听器。很可能按钮无法从第二个活动的布局中膨胀,因为它不存在。

  

android.widget.Button.setOnClickListener(android.view.View $ OnClickListener)&#39;   在null对象引用   com.example.eranp.clientpage.Main2Activity.onCreate(Main2Activity.java:50)   在

答案 1 :(得分:0)

根据您的错误日志,您会因此而收到错误

  

尝试调用虚方法'void   android.widget.Button.setOnClickListener(android.view.View $ OnClickListener)”   在com.example.eranp.clientpage.Main2Activity.onCreate(Main2Activity.java:50)上的空对象引用上

saveDataBase = (Button) findViewById(R.id.save_btn);
saveDataBase.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
             // code...
            }
});

检查天气按钮ID是否正确,是否与活动的布局相同。