程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程解疑 >> 編程-為什麼按另一個按鈕,程序就崩潰了?

編程-為什麼按另一個按鈕,程序就崩潰了?

編輯:編程解疑
為什麼按另一個按鈕,程序就崩潰了?

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對象"這個按鈕時程序卻崩潰了:
圖片說明

最佳回答:


幫你運行一遍,知道問題在哪了。ReceiveObjectActivity:
ParcelableUser parcelableUser=(ParcelableUser)getIntent().getParcelableExtra("serial");
你傳遞的key是parcel,所以是key不對應,改成:
ParcelableUser parcelableUser=(ParcelableUser)getIntent().getParcelableExtra("parcel");

YXTS122
YXTS122
YXTS122
YXTS122
YXTS122
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved