我想在Android中使用Intent将所有详细信息传递给另一个活动,但是我的以下代码生成错误

时间:2018-09-02 01:58:43

标签: android android-intent

但是当我运行以下代码时,按下按钮后它显示错误“不幸的是,应用已停止” 下面是我的第一个activity.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"
android:orientation="vertical"
tools:context=".MainActivity">

<EditText
    android:id="@+id/editText"
    android:layout_width="wrap_content"
    android:layout_height="36dp"
    android:layout_alignParentStart="true"
    android:layout_alignParentTop="true"
    android:layout_marginTop="22dp"
    android:layout_weight="1"
    android:ems="10"
    android:hint="Name:"
    android:inputType="textPersonName"
    android:layout_alignParentLeft="true" />

<RadioGroup
    android:layout_width="279dp"
    android:layout_height="76dp"
    android:layout_alignParentStart="true"
    android:layout_alignParentTop="true"
    android:layout_marginTop="87dp"
    android:layout_alignParentLeft="true">

    <RadioButton
        android:id="@+id/radioButton3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Male" />

    <RadioButton
        android:id="@+id/radioButton4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Female" />
</RadioGroup>

<EditText
    android:id="@+id/editText3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentStart="true"
    android:layout_alignParentTop="true"
    android:layout_marginTop="186dp"
    android:ems="10"
    android:hint="Address:"
    android:inputType="textPersonName"
    android:layout_alignParentLeft="true" />

<EditText
    android:id="@+id/editText4"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentStart="true"
    android:layout_marginBottom="217dp"
    android:ems="10"
    android:hint="Contact no:"
    android:inputType="textPersonName"
    android:layout_alignParentLeft="true" />

<EditText
    android:id="@+id/editText5"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentStart="true"
    android:layout_marginBottom="156dp"
    android:ems="10"
    android:hint="E-mail ID"
    android:inputType="textEmailAddress"
    android:layout_alignParentLeft="true" />

<CheckBox
    android:id="@+id/checkBox"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentStart="true"
    android:layout_alignTop="@+id/checkBox2"
    android:layout_marginStart="130dp"
    android:text="Dancing"
    android:layout_alignParentLeft="true"
    android:layout_marginLeft="130dp" />

<CheckBox
    android:id="@+id/checkBox3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentEnd="true"
    android:layout_alignTop="@+id/checkBox"
    android:layout_marginEnd="51dp"
    android:text="Reading"
    android:layout_alignParentRight="true"
    android:layout_marginRight="51dp" />

<CheckBox
    android:id="@+id/checkBox4"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentStart="true"
    android:layout_marginBottom="66dp"
    android:layout_marginStart="70dp"
    android:text="Singing"
    android:layout_alignParentLeft="true"
    android:layout_marginLeft="70dp" />

<CheckBox
    android:id="@+id/checkBox2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentStart="true"
    android:layout_marginBottom="115dp"
    android:text="Playing"
    android:layout_alignParentLeft="true" />

<Button
    android:id="@+id/button"
    android:layout_width="178dp"
    android:layout_height="wrap_content"
    android:layout_below="@+id/checkBox4"
    android:layout_marginTop="-66dp"
    android:layout_toEndOf="@+id/checkBox2"
    android:text="Submit"
    android:layout_toRightOf="@+id/checkBox2" />
</RelativeLayout>

下面是我的第一个活动的Java文件

package com.example.mypc.practical1;

    import android.content.Intent;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.*;

public class MainActivity extends AppCompatActivity {
Button btn;
EditText name,add,cont,email;
CheckBox cb1,cb2,cb3,cb4;
Intent intent=new Intent(this,Activity2.class);

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    btn=(Button)findViewById(R.id.button);
    name=(EditText)findViewById(R.id.editText);
    add=(EditText)findViewById(R.id.editText3);
    cont=(EditText)findViewById(R.id.editText4);
    email=(EditText)findViewById(R.id.editText5);
    cb1=(CheckBox)findViewById(R.id.checkBox);
    cb2=(CheckBox)findViewById(R.id.checkBox2);
    cb3=(CheckBox)findViewById(R.id.checkBox3);
    cb4=(CheckBox)findViewById(R.id.checkBox4);
    btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String nm=name.getText().toString();
            String a=add.getText().toString();
            String c=cont.getText().toString();
            String em=email.getText().toString();
            String cvalue="";
            if (cb1.isChecked()==true)
            {
                cvalue+="Playing";
            }
            else if(cb2.isChecked()==true)
            {
                cvalue+="Dancing";
            }
            else if(cb3.isChecked()==true)
            {
                cvalue+="Reading";
            }
            else if(cb4.isChecked()==true)
            {
                cvalue+="Singing";
            }

            intent.putExtra("this_name",   nm);
            intent.putExtra("this_add",  a);
            intent.putExtra("this_cont",  c);
            intent.putExtra("this_email",  em);
            intent.putExtra("this_chechbox", cvalue);
            startActivity(intent);
        }
    });
}
}

下面是我的第二个活动.java文件

package com.example.mypc.practical1;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.*;

public class Activity2 extends AppCompatActivity {
EditText ed;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_2);
    Intent i=getIntent();

    Bundle b=getIntent().getExtras();
    String name=i.getExtras().getString("this_name");
    String add=i.getExtras().getString("this_add");
    String cont=i.getExtras().getString("this_cont");
    String email=i.getExtras().getString("this_email");
    String check=i.getExtras().getString("this_chechbox");
    ed.setText(name+add+cont+email+check);
}
}

下面是我的第二个activity.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=".Activity2">

<EditText
    android:id="@+id/editText2"
    android:layout_width="301dp"
    android:layout_height="396dp"
    android:ems="10"
    android:inputType="textMultiLine"
    tools:layout_editor_absoluteX="33dp"
    tools:layout_editor_absoluteY="30dp" />
</RelativeLayout>

下面是我的堆栈跟踪:

E/AndroidRuntime: FATAL EXCEPTION: main
              java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.mypc.practical1/com.example.mypc.practical1.MainActivity}: java.lang.NullPointerException
                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1983)
                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
                  at android.app.ActivityThread.access$600(ActivityThread.java:130)
                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
                  at android.os.Handler.dispatchMessage(Handler.java:99)
                  at android.os.Looper.loop(Looper.java:137)
                  at android.app.ActivityThread.main(ActivityThread.java:4745)
                  at java.lang.reflect.Method.invokeNative(Native Method)
                  at java.lang.reflect.Method.invoke(Method.java:511)
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
                  at dalvik.system.NativeStart.main(Native Method)
               Caused by: java.lang.NullPointerException
                  at android.content.ContextWrapper.getPackageName(ContextWrapper.java:127)
                  at android.content.ComponentName.<init>(ComponentName.java:75)
                  at android.content.Intent.<init>(Intent.java:3301)
                  at com.example.mypc.practical1.MainActivity.<init>(MainActivity.java:13)
                  at java.lang.Class.newInstanceImpl(Native Method)
                  at java.lang.Class.newInstance(Class.java:1319)
                  at android.app.Instrumentation.newActivity(Instrumentation.java:1053)
                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1974)
                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084) 
                  at android.app.ActivityThread.access$600(ActivityThread.java:130) 
                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195) 
                  at android.os.Handler.dispatchMessage(Handler.java:99) 
                  at android.os.Looper.loop(Looper.java:137) 
                  at android.app.ActivityThread.main(ActivityThread.java:4745) 
                  at java.lang.reflect.Method.invokeNative(Native Method) 
                  at java.lang.reflect.Method.invoke(Method.java:511) 
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786) 
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 
                  at dalvik.system.NativeStart.main(Native Method) 
Application terminated.

请帮助我解决错误 预先感谢。

3 个答案:

答案 0 :(得分:1)

您无法声明意图,因为创建后上下文仍然为空。

您需要将您的意图Intent intent=new Intent(this,Activity2.class);移至onCreate或更好的onClick中,它将正常工作

答案 1 :(得分:0)

您有隐藏意图。更改此行;

// final   Intent intent=new Intent(this,Activity2.class);

收件人

final   Intent intent=new Intent(this,Activity2.class);

并将其从活动中移除延伸至下方。

答案 2 :(得分:-1)

首先,您需要更改按钮onclick中的Intent:

 btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            **Intent intent = new Intent(this, Activity2.class);**
            String nm = name.getText().toString();
            String a = add.getText().toString();
            String c = cont.getText().toString();
            String em = email.getText().toString();
            String cvalue = "";
            if (cb1.isChecked() == true) {
                cvalue += "Playing";
            } else if (cb2.isChecked() == true) {
                cvalue += "Dancing";
            } else if (cb3.isChecked() == true) {
                cvalue += "Reading";
            } else if (cb4.isChecked() == true) {
                cvalue += "Singing";
            }

            intent.putExtra("this_name", nm);
            intent.putExtra("this_add", a);
            intent.putExtra("this_cont", c);
            intent.putExtra("this_email", em);
            intent.putExtra("this_chechbox", cvalue);
            startActivity(intent);}});

第二,您的Edittext没有在第二个活动中初始化:

EditText ed;
ed=(EditText)findViewById(R.id.ed);

然后在Edittext中设置意图数据。