我有一个 Java 语言的程序,它无法启动一个活动。

huangapple go评论67阅读模式
英文:

I am having a java lang which it can't start an activity

问题

private void init(){
    Log.d(TAG, "init: inflating " + getString(R.string.profile_fragment));

    Intent intent = getIntent();
    if(intent.hasExtra(getString(R.string.calling_activity))){
        Log.d(TAG, "init: searching for user object attached as intent extra");
        if(intent.hasExtra(getString(R.string.intent_user))){
            User user = intent.getParcelableExtra(getString(R.string.intent_user));
            if(!user.getUser_id().equals(FirebaseAuth.getInstance().getCurrentUser().getUid())){
                Log.d(TAG, "init: inflating view profile");
                ViewProfileFragment fragment = new ViewProfileFragment();
                Bundle args = new Bundle();
                args.putParcelable(getString(R.string.intent_user),
                        intent.getParcelableExtra(getString(R.string.intent_user)));
                fragment.setArguments(args);

                FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
                transaction.replace(R.id.container, fragment);
                transaction.addToBackStack(getString(R.string.view_profile_fragment));
                transaction.commit();
            }else{
                Log.d(TAG, "init: inflating Profile");
                ProfileFragment fragment = new ProfileFragment();
                FragmentTransaction transaction = ProfileActivity.this.getSupportFragmentManager().beginTransaction();
                transaction.replace(R.id.container, fragment);
                transaction.addToBackStack(getString(R.string.profile_fragment));
                transaction.commit();
            }
        }else{
            Toast.makeText(mContext, "something went wrong", Toast.LENGTH_SHORT).show();
        }

    }else{
        Log.d(TAG, "init: inflating Profile");
        ProfileFragment fragment = new ProfileFragment();
        FragmentTransaction transaction = ProfileActivity.this.getSupportFragmentManager().beginTransaction();
        transaction.replace(R.id.container, fragment);
        transaction.addToBackStack(getString(R.string.profile_fragment));
        transaction.commit();
    }

}

public class User implements Parcelable{

    private String user_id;
    private long phone_number;
    private String email;
    private String username;
    private String status;

    public User(String user_id, long phone_number, String email, String username) {
        this.user_id = user_id;
        this.phone_number = phone_number;
        this.email = email;
        this.username = username;
        this.status = status;

    }

    public User() {

    }

    protected User(Parcel in) {
        user_id = in.readString();
        phone_number = in.readLong();
        email = in.readString();
        username = in.readString();
    }

    public static final Creator<User> CREATOR = new Creator<User>() {
        @Override
        public User createFromParcel(Parcel in) {
            return new User(in);
        }

        @Override
        public User[] newArray(int size) {
            return new User[size];
        }
    };

    public String getUser_id() {
        return user_id;
    }

    public void setUser_id(String user_id) {
        this.user_id = user_id;
    }

    public long getPhone_number() {
        return phone_number;
    }

    public void setPhone_number(long phone_number) {
        this.phone_number = phone_number;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    @Override
    public String toString() {
        return "User{" +
                "user_id='" + user_id + '\'' +
                ", phone_number=" + phone_number +
                ", email='" + email + '\'' +
                ", username='" + username + '\'' +
                ", status='" + status + '\'' +
                '}';
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(user_id);
        dest.writeLong(phone_number);
        dest.writeString(email);
        dest.writeString(username);
        dest.writeString(status);
    }
}

E/AndroidRuntime: FATAL EXCEPTION: main
Process: umo.com.players, PID: 3876
java.lang.RuntimeException: Unable to start activity ComponentInfo{umo.com.players/umo.com.players.Profile.ProfileActivity}: android.os.BadParcelableException: ClassNotFoundException when unmarshalling: 
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2868)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2958)
at android.app.ActivityThread.-wrap12(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1653)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6739)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:449)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
Caused by: android.os.BadParcelableException: ClassNotFoundException when unmarshalling: 
at android.os.Parcel.readParcelableCreator(Parcel.java:2925)
at android.os.Parcel.readParcelable(Parcel.java:2851)
at android.os.Parcel.readParcelableArray(Parcel.java:2959)
at android.os.Parcel.readValue(Parcel.java:2808)
at android.os.Parcel.readArrayMapInternal(Parcel.java:3123)
at android.os.BaseBundle.initializeFromParcelLocked(BaseBundle.java:273)
at android.os.BaseBundle.unparcel(BaseBundle.java:226)
at android.os.BaseBundle.containsKey(BaseBundle.java:467)
at android.content.Intent.hasExtra(Intent.java:6971)
at umo.com.players.Profile.ProfileActivity.init(ProfileActivity.java:92)
at umo.com.players.Profile.ProfileActivity.onCreate(ProfileActivity.java:83)
at android.app.Activity.performCreate(Activity.java:7045)
at android.app.Activity.performCreate(Activity.java:7036)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1217)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2815)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2958) 
at android.app.ActivityThread.-wrap12(Unknown Source:0) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1653) 
at android.os.Handler.dispatchMessage(Handler.java:106) 
at android.os.Looper.loop(Looper.java:164) 
at android.app.ActivityThread.main(ActivityThread.java:6739) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:449) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
英文:

profile fragment
I am Having a java lang exception which can't start activity how can I fix this please help
I am new at this and I need to finish my project today please help

The log cat is below the profile fragment

profile fragment
I am Having a java lang exception which can't start activity how can I fix this please help
I am new at this and I need to finish my project today please help

The log cat is below the profile fragment

        private void init(){
Log.d(TAG, &quot;init: inflating &quot; + getString(R.string.profile_fragment));
Intent intent = getIntent();
if(intent.hasExtra(getString(R.string.calling_activity))){
Log.d(TAG, &quot;init: searching for user object attached as intent extra&quot;);
if(intent.hasExtra(getString(R.string.intent_user))){
User user = intent.getParcelableExtra(getString(R.string.intent_user));
if(!user.getUser_id().equals(FirebaseAuth.getInstance().getCurrentUser().getUid())){
Log.d(TAG, &quot;init: inflating view profile&quot;);
ViewProfileFragment fragment = new ViewProfileFragment();
Bundle args = new Bundle();
args.putParcelable(getString(R.string.intent_user),
intent.getParcelableExtra(getString(R.string.intent_user)));
fragment.setArguments(args);
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.container, fragment);
transaction.addToBackStack(getString(R.string.view_profile_fragment));
transaction.commit();
}else{
Log.d(TAG, &quot;init: inflating Profile&quot;);
ProfileFragment fragment = new ProfileFragment();
FragmentTransaction transaction = ProfileActivity.this.getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.container, fragment);
transaction.addToBackStack(getString(R.string.profile_fragment));
transaction.commit();
}
}else{
Toast.makeText(mContext, &quot;something went wrong&quot;, Toast.LENGTH_SHORT).show();
}
}else{
Log.d(TAG, &quot;init: inflating Profile&quot;);
ProfileFragment fragment = new ProfileFragment();
FragmentTransaction transaction = ProfileActivity.this.getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.container, fragment);
transaction.addToBackStack(getString(R.string.profile_fragment));
transaction.commit();
}
}

here is the user class

      public class User implements Parcelable{
private String user_id;
private long phone_number;
private String email;
private String username;
private String status;
public User(String user_id, long phone_number, String email, String username) {
this.user_id = user_id;
this.phone_number = phone_number;
this.email = email;
this.username = username;
this.status = status;
}
public User() {
}
protected User(Parcel in) {
user_id = in.readString();
phone_number = in.readLong();
email = in.readString();
username = in.readString();
}
public static final Creator&lt;User&gt; CREATOR = new Creator&lt;User&gt;() {
@Override
public User createFromParcel(Parcel in) {
return new User(in);
}
@Override
public User[] newArray(int size) {
return new User[size];
}
};
public String getUser_id() {
return user_id;
}
public void setUser_id(String user_id) {
this.user_id = user_id;
}
public long getPhone_number() {
return phone_number;
}
public void setPhone_number(long phone_number) {
this.phone_number = phone_number;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
@Override
public String toString() {
return &quot;User{&quot; +
&quot;user_id=&#39;&quot; + user_id + &#39;\&#39;&#39; +
&quot;, phone_number=&quot; + phone_number +
&quot;, email=&#39;&quot; + email + &#39;\&#39;&#39; +
&quot;, username=&#39;&quot; + username + &#39;\&#39;&#39; +
&quot;, status=&#39;&quot; + status + &#39;\&#39;&#39; +
&#39;}&#39;;
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(user_id);
dest.writeLong(phone_number);
dest.writeString(email);
dest.writeString(username);
dest.writeString(status);
}

}

logcat

    E/AndroidRuntime: FATAL EXCEPTION: main
Process: umo.com.players, PID: 3876
java.lang.RuntimeException: Unable to start activity ComponentInfo{umo.com.players/umo.com.players.Profile.ProfileActivity}: android.os.BadParcelableException: ClassNotFoundException when unmarshalling: 
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2868)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2958)
at android.app.ActivityThread.-wrap12(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1653)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6739)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:449)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
Caused by: android.os.BadParcelableException: ClassNotFoundException when unmarshalling: 
at android.os.Parcel.readParcelableCreator(Parcel.java:2925)
at android.os.Parcel.readParcelable(Parcel.java:2851)
at android.os.Parcel.readParcelableArray(Parcel.java:2959)
at android.os.Parcel.readValue(Parcel.java:2808)
at android.os.Parcel.readArrayMapInternal(Parcel.java:3123)
at android.os.BaseBundle.initializeFromParcelLocked(BaseBundle.java:273)
at android.os.BaseBundle.unparcel(BaseBundle.java:226)
at android.os.BaseBundle.containsKey(BaseBundle.java:467)
at android.content.Intent.hasExtra(Intent.java:6971)
at umo.com.players.Profile.ProfileActivity.init(ProfileActivity.java:92)
at umo.com.players.Profile.ProfileActivity.onCreate(ProfileActivity.java:83)
at android.app.Activity.performCreate(Activity.java:7045)
at android.app.Activity.performCreate(Activity.java:7036)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1217)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2815)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2958) 
at android.app.ActivityThread.-wrap12(Unknown Source:0) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1653) 
at android.os.Handler.dispatchMessage(Handler.java:106) 
at android.os.Looper.loop(Looper.java:164) 
at android.app.ActivityThread.main(ActivityThread.java:6739) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:449) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807) 

答案1

得分: 0

如何理解发生了什么...

查看堆栈跟踪...

Caused by: android.os.BadParcelableException: 在解组时出现 ClassNotFoundException
    at android.os.Parcel.readParcelableCreator(Parcel.java:2925)
    at android.os.Parcel.readParcelable(Parcel.java:2851)
    at android.os.Parcel.readParcelableArray(Parcel.java:2959)
    at android.os.Parcel.readValue(Parcel.java:2808)
    at android.os.Parcel.readArrayMapInternal(Parcel.java:3123)
    at android.os.BaseBundle.initializeFromParcelLocked(BaseBundle.java:273)
    at android.os.BaseBundle.unparcel(BaseBundle.java:226)
    at android.os.BaseBundle.containsKey(BaseBundle.java:467)
    at android.content.Intent.hasExtra(Intent.java:6971)
    at umo.com.players.Profile.ProfileActivity.init(ProfileActivity.java:92)
    at umo.com.players.Profile.ProfileActivity.onCreate(ProfileActivity.java:83)
    at android.app.Activity.performCreate(Activity.java:7045)
    at android.app.Activity.performCreate(Activity.java:7036)

从堆栈跟踪中最底部开始,找到与您的代码库相关的部分(提示:查找以您的项目包名称开头的行):

    at umo.com.players.Profile.ProfileActivity.onCreate(ProfileActivity.java:83)

您可以看到在 ProfileActivity 的第 83 行调用了一些代码。从底部往上分析堆栈。如果您理解了关于堆栈的推入/弹出操作,将有助于更轻松地理解其余部分。尝试在堆栈跟踪中导航,您会在某个地方找到抛出异常的原因。

英文:

How to understand whats going on...

Look at the stack trace...

Caused by: android.os.BadParcelableException: ClassNotFoundException when unmarshalling: 
    at android.os.Parcel.readParcelableCreator(Parcel.java:2925)
    at android.os.Parcel.readParcelable(Parcel.java:2851)
    at android.os.Parcel.readParcelableArray(Parcel.java:2959)
    at android.os.Parcel.readValue(Parcel.java:2808)
    at android.os.Parcel.readArrayMapInternal(Parcel.java:3123)
    at android.os.BaseBundle.initializeFromParcelLocked(BaseBundle.java:273)
    at android.os.BaseBundle.unparcel(BaseBundle.java:226)
    at android.os.BaseBundle.containsKey(BaseBundle.java:467)
    at android.content.Intent.hasExtra(Intent.java:6971)
    at umo.com.players.Profile.ProfileActivity.init(ProfileActivity.java:92)
    at umo.com.players.Profile.ProfileActivity.onCreate(ProfileActivity.java:83)
    at android.app.Activity.performCreate(Activity.java:7045)
    at android.app.Activity.performCreate(Activity.java:7036)

Starting at the line lowest down in the stack trace that looks like its relevant to your codebase (hint: look lines starting with your project's package name) :

    at umo.com.players.Profile.ProfileActivity.onCreate(ProfileActivity.java:83)

You can see some line of code was called in ProfileActivity on line 83. Work your way up the stack. If you understand you push/pop on stacks it makes understanding the rest easier. Try to navigate through the lines in the stack trace and somewhere you'll find the cause of the exception being thrown.

答案2

得分: 0

你忘记在构造函数中添加 status

public User(String user_id, long phone_number, String email, String username, String status) {
    this.user_id = user_id;
    this.phone_number = phone_number;
    this.email = email;
    this.username = username;
    this.status = status;
}

并且添加初始化 status

protected User(Parcel in) {
    user_id = in.readString();
    phone_number = in.readLong();
    email = in.readString();
    username = in.readString();
    status = in.readString();
}
英文:

You forgot to add the status into constructor:

public User(String user_id, long phone_number, String email, String username, String status) {
this.user_id = user_id;
this.phone_number = phone_number;
this.email = email;
this.username = username;
this.status = status;
}

And add initialization status:


protected User(Parcel in) {
user_id = in.readString();
phone_number = in.readLong();
email = in.readString();
username = in.readString();
status = in.readString();
}

huangapple
  • 本文由 发表于 2020年10月16日 02:59:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/64378100.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定