YXTS122
2016-11-19 21:47
采纳率: 100%
浏览 1.4k

为什么按另一个按钮,程序就崩溃了?

IntentObjectActivity.java

 package com.example.ch02_intentobjectdemo;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;


public class IntentObjectActivity extends Activity {

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

    public void sendData(View view)
    {
        switch (view.getId())
        {
        case R.id.button1:
            SerializableUser sUser=new SerializableUser("Admin","123456");
            Intent intent=new Intent(this,ReceiveObjectActivity.class);
            Bundle bundle=new Bundle();
            bundle.putInt("type",1);
            bundle.putSerializable("serial",sUser);
            intent.putExtras(bundle);
            startActivity(intent);
            break;
        case R.id.button2:
            ParcelableUser pUser=new ParcelableUser("User","123456");
            Intent intent1=new Intent(this,ReceiveObjectActivity.class);
            Bundle bundle1=new Bundle();
            bundle1.putInt("type",2);
            bundle1.putParcelable("parcel",pUser);
            intent1.putExtras(bundle1);
            startActivity(intent1);
            break;
        }
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.intent_object, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

ParcelableUser.java

 package com.example.ch02_intentobjectdemo;

import android.os.Parcel;
import android.os.Parcelable;

public class ParcelableUser implements Parcelable{
    private String userName;
    private String passWord;
    public String getUserName()
    {
        return userName;
    }
    public void setUserName(String userName)
    {
        this.userName=userName;
    }
    public String getPassWord()
    {
        return passWord;
    }
    public void setPassWord(String passWord)
    {
        this.passWord=passWord;
    }
    public ParcelableUser(String userName,String passWord)
    {
        this.userName=userName;
        this.passWord=passWord;
    }
    public ParcelableUser()
    {

    }
    public int describeContents()
    {
        return 0;
    }
    public void writeToParcel(Parcel p,int arg1)
    {
        p.writeString(userName);
        p.writeString(passWord);
    }
    public static final Parcelable.Creator<ParcelableUser> CREATOR=new Creator<ParcelableUser>()
            {
        public ParcelableUser createFromParcel(Parcel source)
        {
            ParcelableUser parcelableUser=new ParcelableUser();
            parcelableUser.userName=source.readString();
            parcelableUser.passWord=source.readString();
            return parcelableUser;
        }
        public ParcelableUser[] newArray(int size)
        {
            return new ParcelableUser[size];
        }
            };

}

ReceiveObjectActivity.java

 package com.example.ch02_intentobjectdemo;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class ReceiveObjectActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.objectreceiver);
        TextView tv=(TextView)findViewById(R.id.showresult);
        Bundle bundle=getIntent().getExtras();
        int type=bundle.getInt("type");
        if(type==1)
        {
            SerializableUser serializableUser=(SerializableUser)getIntent().getSerializableExtra("serial");
            tv.setText(serializableUser.getUserName()+"\n"+serializableUser.getPassWord());

        }
        else
        {
            ParcelableUser parcelableUser=(ParcelableUser)getIntent().getParcelableExtra("serial");
            tv.setText(parcelableUser.getUserName()+"\n"+parcelableUser.getPassWord());

        }
    }

}

SerializableUser.java

 package com.example.ch02_intentobjectdemo;

import java.io.Serializable;

public class SerializableUser implements Serializable{
    private String userName;
    private String passWord;
    public String getUserName()
    {
        return userName;
    }
    public void setUserName(String userName)
    {
        this.userName=userName;
    }
    public String getPassWord()
    {
        return passWord;
    }
    public void setPassWord(String passWord)
    {
        this.passWord=passWord;
    }
    public SerializableUser(String userName,String passWord)
    {
        this.userName=userName;
        this.passWord=passWord;
    }

}

activity_intent_object.xml

 <LinearLayout 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"
    tools:context="com.example.ch02_intentobjectdemo.IntentObjectActivity" 
    android:orientation="vertical">

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/di"
        android:onClick="sendData"/>

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/dui"
        android:onClick="sendData"/>

</LinearLayout>

objectreceiver.xml

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

    <TextView 
        android:id="@+id/showresult"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

</LinearLayout>

strings.xml

 <?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="app_name">IntentObjectDemo</string>
    <string name="hello_world">Hello world!</string>
    <string name="action_settings">Settings</string>
    <string name="di">传递Serializable对象</string>
    <string name="dui">传递Parcelable对象</string>

</resources>

AndroidManifest.xml

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

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="21" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".IntentObjectActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity 
            android:name=".ReceiveObjectActivity"
            android:label="@string/app_name">

        </activity>
    </application>

</manifest>

图片说明

图片说明

程序运行如下:
图片说明

当我按"传递Serializable对象"这个按钮时运行如下:
图片说明

当我按"传递Parcelable对象"这个按钮时程序却崩溃了:
图片说明

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

7条回答 默认 最新

  • 徐福记456 2016-11-20 06:13
    已采纳

    帮你运行一遍,知道问题在哪了。ReceiveObjectActivity:
    ParcelableUser parcelableUser=(ParcelableUser)getIntent().getParcelableExtra("serial");
    你传递的key是parcel,所以是key不对应,改成:
    ParcelableUser parcelableUser=(ParcelableUser)getIntent().getParcelableExtra("parcel");

    已采纳该答案
    打赏 评论
  • 小黄人的banana 2016-11-20 01:33

    打断点喽,看是那一行有错误,然后在进行解决

    打赏 评论
  • T_world 2016-11-20 03:29

    把一些关键信息输出一下看看,比如你可以在ReceiveObjectActivity里把type的值打印一下

    打赏 评论
  • qq_18179213 2016-11-20 05:08

    检查包名,看看监听器导入的包名是否匹配。

    打赏 评论
  • 徐福记456 2016-11-20 06:18

    你5点多在写代码,精神值得学习

    打赏 评论
  • viewgroup 2016-11-21 03:06

    你在取值的时候的key写错了,更改之后就可以了

    打赏 评论
  • YXTS122 2016-11-26 01:54

    布局文件里控件名字的大小写写错,程序也会崩溃。。。

    打赏 评论

相关推荐 更多相似问题