I have a custom class I want to pass to another Activity and I've read that a fine way to do this is by using Parcelable interface.
public class MatchData implements Parcelable{
private long mId;
private int kills, assists, deaths, creeps, ssp1, ssp2, playerRole;
private String type;
private String champion;
private ArrayList<Integer> items;
private boolean won;
MatchData(long m, int k, int d, int a, String id, int cr, int s1, int s2, int pr, String t, ArrayList<Integer> it, boolean w) {
mId = m;
kills = k;
assists = a;
deaths = d;
champion = id;
creeps = cr;
ssp1 = s1;
ssp2 = s2;
playerRole = pr;
type = t;
items = it;
won = w;
}
MatchData(Parcel in) {
mId = in.readInt();
kills = in.readInt();
deaths = in.readInt();
assists = in.readInt();
champion = in.readString();
creeps = in.readInt();
ssp1 = in.readInt();
ssp2 = in.readInt();
playerRole = in.readInt();
type = in.readString();
items = new ArrayList<>();
in.readList(items, null); // right here
won = Boolean.valueOf(in.readString());
}
#Override
public int describeContents() {
return 0;
}
#Override
public void writeToParcel(Parcel out, int flags) {
out.writeLong(this.getmId());
out.writeInt(this.getKills());
out.writeInt(this.getDeaths());
out.writeInt(this.getAssists());
out.writeString(this.getChampion());
out.writeInt(this.getCreeps());
out.writeInt(this.getSsp1());
out.writeInt(this.getSsp2());
out.writeInt(this.getPlayerRole());
out.writeString(this.getType());
out.writeList(this.getItems());
out.writeString(String.valueOf(this.isWon()));
}
public static final Parcelable.Creator<MatchData> CREATOR = new Parcelable.Creator<MatchData>() {
public MatchData createFromParcel(Parcel in) {
return new MatchData(in);
}
public MatchData[] newArray(int size) {
return new MatchData[size];
}
};
}
I didnt copy the getters and setters but they are there. The problem I face comes when I try to read the arraylist of integers from the parcel (I commented the line).
Edit: I replaced in that line null with Integer.class.getClassLoader() and this is the new stacktrace
06-23 13:26:35.479 2429-2429/com.nicu.bogdan.lolstats E/AndroidRuntime: FATAL EXCEPTION: main
06-23 13:26:35.479 2429-2429/com.nicu.bogdan.lolstats E/AndroidRuntime: Process: com.nicu.bogdan.lolstats, PID: 2429
06-23 13:26:35.479 2429-2429/com.nicu.bogdan.lolstats E/AndroidRuntime: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.nicu.bogdan.lolstats/com.nicu.bogdan.playerInfo.MatchActivity}: java.lang.RuntimeException: Parcel android.os.Parcel#24a39bfa: Unmarshalling unknown type code 4915278 at offset 168
06-23 13:26:35.479 2429-2429/com.nicu.bogdan.lolstats E/AndroidRuntime: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)
06-23 13:26:35.479 2429-2429/com.nicu.bogdan.lolstats E/AndroidRuntime: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
06-23 13:26:35.479 2429-2429/com.nicu.bogdan.lolstats E/AndroidRuntime: at android.app.ActivityThread.access$800(ActivityThread.java:151)
06-23 13:26:35.479 2429-2429/com.nicu.bogdan.lolstats E/AndroidRuntime: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
06-23 13:26:35.479 2429-2429/com.nicu.bogdan.lolstats E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:102)
06-23 13:26:35.479 2429-2429/com.nicu.bogdan.lolstats E/AndroidRuntime: at android.os.Looper.loop(Looper.java:135)
06-23 13:26:35.479 2429-2429/com.nicu.bogdan.lolstats E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5254)
06-23 13:26:35.479 2429-2429/com.nicu.bogdan.lolstats E/AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method)
06-23 13:26:35.479 2429-2429/com.nicu.bogdan.lolstats E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:372)
06-23 13:26:35.479 2429-2429/com.nicu.bogdan.lolstats E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
06-23 13:26:35.479 2429-2429/com.nicu.bogdan.lolstats E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
06-23 13:26:35.479 2429-2429/com.nicu.bogdan.lolstats E/AndroidRuntime: Caused by: java.lang.RuntimeException: Parcel android.os.Parcel#24a39bfa: Unmarshalling unknown type code 4915278 at offset 168
06-23 13:26:35.479 2429-2429/com.nicu.bogdan.lolstats E/AndroidRuntime: at android.os.Parcel.readValue(Parcel.java:2228)
06-23 13:26:35.479 2429-2429/com.nicu.bogdan.lolstats E/AndroidRuntime: at android.os.Parcel.readListInternal(Parcel.java:2526)
06-23 13:26:35.479 2429-2429/com.nicu.bogdan.lolstats E/AndroidRuntime: at android.os.Parcel.readList(Parcel.java:1661)
06-23 13:26:35.479 2429-2429/com.nicu.bogdan.lolstats E/AndroidRuntime: at com.nicu.bogdan.jsonParser.MatchData.<init>(MatchData.java:140)
06-23 13:26:35.479 2429-2429/com.nicu.bogdan.lolstats E/AndroidRuntime: at com.nicu.bogdan.jsonParser.MatchData$1.createFromParcel(MatchData.java:167)
06-23 13:26:35.479 2429-2429/com.nicu.bogdan.lolstats E/AndroidRuntime: at com.nicu.bogdan.jsonParser.MatchData$1.createFromParcel(MatchData.java:165)
06-23 13:26:35.479 2429-2429/com.nicu.bogdan.lolstats E/AndroidRuntime: at android.os.Parcel.readParcelable(Parcel.java:2252)
06-23 13:26:35.479 2429-2429/com.nicu.bogdan.lolstats E/AndroidRuntime: at android.os.Parcel.readValue(Parcel.java:2152)
06-23 13:26:35.479 2429-2429/com.nicu.bogdan.lolstats E/AndroidRuntime: at android.os.Parcel.readArrayMapInternal(Parcel.java:2485)
06-23 13:26:35.479 2429-2429/com.nicu.bogdan.lolstats E/AndroidRuntime: at android.os.BaseBundle.unparcel(BaseBundle.java:221)
06-23 13:26:35.479 2429-2429/com.nicu.bogdan.lolstats E/AndroidRuntime: at android.os.Bundle.getParcelable(Bundle.java:755)
06-23 13:26:35.479 2429-2429/com.nicu.bogdan.lolstats E/AndroidRuntime: at android.content.Intent.getParcelableExtra(Intent.java:5088)
06-23 13:26:35.479 2429-2429/com.nicu.bogdan.lolstats E/AndroidRuntime: at com.nicu.bogdan.playerInfo.MatchActivity.onCreate(MatchActivity.java:43)
06-23 13:26:35.479 2429-2429/com.nicu.bogdan.lolstats E/AndroidRuntime: at android.app.Activity.performCreate(Activity.java:5990)
06-23 13:26:35.479 2429-2429/com.nicu.bogdan.lolstats E/AndroidRuntime: at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
06-23 13:26:35.479 2429-2429/com.nicu.bogdan.lolstats E/AndroidRuntime: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
06-23 13:26:35.479 2429-2429/com.nicu.bogdan.lolstats E/AndroidRuntime: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
06-23 13:26:35.479 2429-2429/com.nicu.bogdan.lolstats E/AndroidRuntime: at android.app.ActivityThread.access$800(ActivityThread.java:151)
06-23 13:26:35.479 2429-2429/com.nicu.bogdan.lolstats E/AndroidRuntime: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
06-23 13:26:35.479 2429-2429/com.nicu.bogdan.lolstats E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:102)
06-23 13:26:35.479 2429-2429/com.nicu.bogdan.lolstats E/AndroidRuntime: at android.os.Looper.loop(Looper.java:135)
06-23 13:26:35.479 2429-2429/com.nicu.bogdan.lolstats E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5254)
06-23 13:26:35.479 2429-2429/com.nicu.bogdan.lolstats E/AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method)
06-23 13:26:35.479 2429-2429/com.nicu.bogdan.lolstats E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:372)
06-23 13:26:35.479 2429-2429/com.nicu.bogdan.lolstats E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
06-23 13:26:35.479 2429-2429/com.nicu.bogdan.lolstats E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Integer isn't parcelable by default. Try this:
in.readList(ids, Integer.class.getClassLoader());
Related
//main activity.java
package com.going.books;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
public class MainActivity extends AppCompatActivity {
//start button
private Button startBtn;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate( savedInstanceState );
setContentView( R.layout.activity_main );
//button start
startBtn=findViewById( R.id.startBtn1 );
startBtn.setOnClickListener( new View.OnClickListener() {
#Override
public void onClick(View v) {
// Start Activity
startActivity( new Intent( MainActivity.this,Main2Activity.class ) );
}
} );
}
}
//complete
//activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:background="#drawable/custom_uid"
android:orientation="horizontal"
android:gravity="bottom"
tools:context=".MainActivity">
<Button
android:id="#+id/startBtn1"
android:layout_width="wrap_content"
android:layout_height="80dp"
android:background="#color/white"
android:layerType="software"
android:layout_weight="1"
android:text="Start"
android:textColor="#color/Black"
android:textSize="20dp" />
<!--complete-->
</LinearLayout>'''
//logcat error
'''04-05 17:34:45.750 32139-32139/com.going.books E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.going.books, PID: 32139
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.going.books/com.going.books.MainActivity}: android.view.InflateException: Binary XML file line #2: Error inflating class
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:902)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:697)
Caused by: android.view.InflateException: Binary XML file line #2: Error inflating class
at android.view.LayoutInflater.createView(LayoutInflater.java:633)
at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:55)
at android.view.LayoutInflater.onCreateView(LayoutInflater.java:682)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:741)
at android.view.LayoutInflater.inflate(LayoutInflater.java:482)
at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:555)
at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:161)
at com.going.books.MainActivity.onCreate(MainActivity.java:19)
at android.app.Activity.performCreate(Activity.java:6033)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:902)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:697)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Constructor.newInstance(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:288)
at android.view.LayoutInflater.createView(LayoutInflater.java:607)
at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:55)
at android.view.LayoutInflater.onCreateView(LayoutInflater.java:682)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:741)
at android.view.LayoutInflater.inflate(LayoutInflater.java:482)
at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:555)
at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:161)
at com.going.books.MainActivity.onCreate(MainActivity.java:19)
at android.app.Activity.performCreate(Activity.java:6033)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:902)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:697)
Caused by: android.content.res.Resources$NotFoundException: Resource "com.going.books:drawable/custom_uid" (7f06005f) is not a Drawable (color or path): TypedValue{t=0x1/d=0x7f06005f a=-1 r=0x7f06005f}
at android.content.res.Resources.loadDrawableForCookie(Resources.java:2441)
at android.content.res.Resources.loadDrawable(Resources.java:2381)
at android.content.res.TypedArray.getDrawable(TypedArray.java:749)
at android.view.View.(View.java:3738)
at android.view.ViewGroup.(ViewGroup.java:498)
at android.widget.LinearLayout.(LinearLayout.java:200)
at android.widget.LinearLayout.(LinearLayout.java:196)
at android.widget.LinearLayout.(LinearLayout.java:192)
at java.lang.reflect.Constructor.newInstance(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:288)
at android.view.LayoutInflater.createView(LayoutInflater.java:607)
at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:55)
at android.view.LayoutInflater.onCreateView(LayoutInflater.java:682)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:741)
at android.view.LayoutInflater.inflate(LayoutInflater.java:482)
at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:555)
at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:161)
at com.going.books.MainActivity.onCreate(MainActivity.java:19)
at android.app.Activity.performCreate(Activity.java:6033)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:902)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:697)
'''
Firstly I am not used background image so no error will appear.
then I am using a background image then the onClick() method error will be produced.
Next than not using background image so no error will be found...
but I am not sure why did hear...
so I am having trouble locating where in my code I have gone wrong. I am building an audio player. The function cannot find all audio files in my device except 2 of them. I am thinking the problem may lie somewhere in my song locating algorithm, findSongs.
Here is my log cat:
03-17 15:29:24.492 20066-20066/? I/SELinux﹕ Function: selinux_android_load_priority , priority [3] , priority version is VE=SEPF_SGH-M919_4.4.4_0048
03-17 15:29:24.492 20066-20066/? E/dalvikvm﹕ >>>>> Normal User
03-17 15:29:24.492 20066-20066/? E/dalvikvm﹕ >>>>> com.example.joe.audiodomain [ userId:0 | appId:10237 ]
03-17 15:29:24.492 20066-20066/? D/dalvikvm﹕ Late-enabling CheckJNI
03-17 15:29:24.632 20066-20066/? W/dalvikvm﹕ VFY: unable to find class referenced in signature (Landroid/view/SearchEvent;)
03-17 15:29:24.632 20066-20066/? I/dalvikvm﹕ Could not find method android.view.Window$Callback.onSearchRequested, referenced from method android.support.v7.view.WindowCallbackWrapper.onSearchRequested
03-17 15:29:24.632 20066-20066/? W/dalvikvm﹕ VFY: unable to resolve interface method 15027: Landroid/view/Window$Callback;.onSearchRequested (Landroid/view/SearchEvent;)Z
03-17 15:29:24.632 20066-20066/? D/dalvikvm﹕ VFY: replacing opcode 0x72 at 0x0002
03-17 15:29:24.632 20066-20066/? I/dalvikvm﹕ Could not find method android.view.Window$Callback.onWindowStartingActionMode, referenced from method android.support.v7.view.WindowCallbackWrapper.onWindowStartingActionMode
03-17 15:29:24.632 20066-20066/? W/dalvikvm﹕ VFY: unable to resolve interface method 15031: Landroid/view/Window$Callback;.onWindowStartingActionMode (Landroid/view/ActionMode$Callback;I)Landroid/view/ActionMode;
03-17 15:29:24.632 20066-20066/? D/dalvikvm﹕ VFY: replacing opcode 0x72 at 0x0002
03-17 15:29:24.702 20066-20066/? I/dalvikvm﹕ Could not find method android.content.res.TypedArray.getChangingConfigurations, referenced from method android.support.v7.widget.TintTypedArray.getChangingConfigurations
03-17 15:29:24.702 20066-20066/? W/dalvikvm﹕ VFY: unable to resolve virtual method 396: Landroid/content/res/TypedArray;.getChangingConfigurations ()I
03-17 15:29:24.702 20066-20066/? D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x0002
03-17 15:29:24.702 20066-20066/? I/dalvikvm﹕ Could not find method android.content.res.TypedArray.getType, referenced from method android.support.v7.widget.TintTypedArray.getType
03-17 15:29:24.702 20066-20066/? W/dalvikvm﹕ VFY: unable to resolve virtual method 418: Landroid/content/res/TypedArray;.getType (I)I
03-17 15:29:24.702 20066-20066/? D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x0002
03-17 15:29:24.712 20066-20066/? E/VdcInflateDelegate﹕ Exception while inflating <vector>
org.xmlpull.v1.XmlPullParserException: Binary XML file line #17<vector> tag requires viewportWidth > 0
at android.support.graphics.drawable.VectorDrawableCompat.updateStateFromTypedArray(VectorDrawableCompat.java:535)
at android.support.graphics.drawable.VectorDrawableCompat.inflate(VectorDrawableCompat.java:472)
at android.support.graphics.drawable.VectorDrawableCompat.createFromXmlInner(VectorDrawableCompat.java:436)
at android.support.v7.widget.AppCompatDrawableManager$VdcInflateDelegate.createFromXmlInner(AppCompatDrawableManager.java:829)
at android.support.v7.widget.AppCompatDrawableManager.loadDrawableFromDelegates(AppCompatDrawableManager.java:303)
at android.support.v7.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:178)
at android.support.v7.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:173)
at android.support.v7.widget.TintTypedArray.getDrawable(TintTypedArray.java:60)
at android.support.v7.widget.Toolbar.<init>(Toolbar.java:254)
at android.support.v7.widget.Toolbar.<init>(Toolbar.java:196)
at java.lang.reflect.Constructor.constructNative(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at android.view.LayoutInflater.createView(LayoutInflater.java:600)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:702)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:761)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:769)
at android.view.LayoutInflater.inflate(LayoutInflater.java:498)
at android.view.LayoutInflater.inflate(LayoutInflater.java:398)
at android.view.LayoutInflater.inflate(LayoutInflater.java:354)
at android.support.v7.app.AppCompatDelegateImplV7.createSubDecor(AppCompatDelegateImplV7.java:379)
at android.support.v7.app.AppCompatDelegateImplV7.ensureSubDecor(AppCompatDelegateImplV7.java:300)
at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:264)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:129)
at com.example.joe.audiodomain.MainActivity.onCreate(MainActivity.java:25)
at android.app.Activity.performCreate(Activity.java:5585)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1093)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2400)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2495)
at android.app.ActivityThread.access$900(ActivityThread.java:170)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1304)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:146)
at android.app.ActivityThread.main(ActivityThread.java:5635)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1291)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1107)
at dalvik.system.NativeStart.main(Native Method)
03-17 15:29:24.712 20066-20066/? D/AndroidRuntime﹕ Shutting down VM
03-17 15:29:24.712 20066-20066/? W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x41845da0)
03-17 15:29:24.712 20066-20066/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.joe.audiodomain, PID: 20066
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.joe.audiodomain/com.example.joe.audiodomain.MainActivity}: android.view.InflateException: Binary XML file line #36: Error inflating class android.support.v7.widget.Toolbar
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2436)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2495)
at android.app.ActivityThread.access$900(ActivityThread.java:170)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1304)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:146)
at android.app.ActivityThread.main(ActivityThread.java:5635)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1291)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1107)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.view.InflateException: Binary XML file line #36: Error inflating class android.support.v7.widget.Toolbar
at android.view.LayoutInflater.createView(LayoutInflater.java:626)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:702)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:761)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:769)
at android.view.LayoutInflater.inflate(LayoutInflater.java:498)
at android.view.LayoutInflater.inflate(LayoutInflater.java:398)
at android.view.LayoutInflater.inflate(LayoutInflater.java:354)
at android.support.v7.app.AppCompatDelegateImplV7.createSubDecor(AppCompatDelegateImplV7.java:379)
at android.support.v7.app.AppCompatDelegateImplV7.ensureSubDecor(AppCompatDelegateImplV7.java:300)
at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:264)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:129)
at com.example.joe.audiodomain.MainActivity.onCreate(MainActivity.java:25)
at android.app.Activity.performCreate(Activity.java:5585)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1093)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2400)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2495)
at android.app.ActivityThread.access$900(ActivityThread.java:170)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1304)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:146)
at android.app.ActivityThread.main(ActivityThread.java:5635)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1291)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1107)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Constructor.constructNative(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at android.view.LayoutInflater.createView(LayoutInflater.java:600)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:702)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:761)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:769)
at android.view.LayoutInflater.inflate(LayoutInflater.java:498)
at android.view.LayoutInflater.inflate(LayoutInflater.java:398)
at android.view.LayoutInflater.inflate(LayoutInflater.java:354)
at android.support.v7.app.AppCompatDelegateImplV7.createSubDecor(AppCompatDelegateImplV7.java:379)
at android.support.v7.app.AppCompatDelegateImplV7.ensureSubDecor(AppCompatDelegateImplV7.java:300)
at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:264)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:129)
at com.example.joe.audiodomain.MainActivity.onCreate(MainActivity.java:25)
at android.app.Activity.performCreate(Activity.java:5585)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1093)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2400)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2495)
at android.app.ActivityThread.access$900(ActivityThread.java:170)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1304)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:146)
at android.app.ActivityThread.main(ActivityThread.java:5635)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1291)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1107)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.content.res.Resources$NotFoundException: File res/drawable-v19/abc_ic_ab_back_material.xml from drawable resource ID #0x7f020016
at android.content.res.Resources.loadDrawable(Resources.java:3439)
at android.content.res.Resources.getDrawable(Resources.java:1893)
at android.support.v7.widget.TintResources.superGetDrawable(TintResources.java:48)
at android.support.v7.widget.AppCompatDrawableManager.onDrawableLoadedFromResources(AppCompatDrawableManager.java:374)
at android.support.v7.widget.TintResources.getDrawable(TintResources.java:44)
at android.support.v4.content.ContextCompat.getDrawable(ContextCompat.java:323)
at android.support.v7.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:180)
at android.support.v7.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:173)
at android.support.v7.widget.TintTypedArray.getDrawable(TintTypedArray.java:60)
at android.support.v7.widget.Toolbar.<init>(Toolbar.java:254)
at android.support.v7.widget.Toolbar.<init>(Toolbar.java:196)
at java.lang.reflect.Constructor.constructNative(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at android.view.LayoutInflater.createView(LayoutInflater.java:600)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:702)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:761)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:769)
at android.view.LayoutInflater.inflate(LayoutInflater.java:498)
at android.view.LayoutInflater.inflate(LayoutInflater.java:398)
at android.view.LayoutInflater.inflate(LayoutInflater.java:354)
at android.support.v7.app.AppCompatDelegateImplV7.createSubDecor(AppCompatDelegateImplV7.java:379)
at android.support.v7.app.AppCompatDelegateImplV7.ensureSubDecor(AppCompatDelegateImplV7.java:300)
at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:264)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:129)
at com.example.joe.audiodomain.MainActivity.onCreate(MainActivity.java:25)
at android.app.Activity.performCreate(Activity.java:5585)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1093)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2400)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2495)
at android.app.ActivityThread.access$900(ActivityThread.java:170)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1304)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:146)
at android.app.ActivityThread.main(ActivityThread.java:5635)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1291)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1107)
at dalvik.system.NativeStart.main(Native Method)
Caused by: org.xmlpull.v1.XmlPullParserException: Binary XML file line #17: invalid drawable tag vector
at android.graphics.drawable.Drawable.createFromXmlInner(Drawable.java:986)
at android.graphics.drawable.Drawable.createFromXml(Drawable.java:930)
at android.content.res.Resources.loadDrawable(Resources.java:3435)
at android.content.res.Resources.getDrawable(Resources.java:1893)
at android.support.v7.widget.TintResources.superGetDrawable(TintResources.java:48)
at android.support.v7.widget.AppCompatDrawableManager.onDrawableLoadedFromResources(AppCompatDrawableManager.java:374)
at android.support.v7.widget.TintResources.getDrawable(TintResources.java:44)
at android.support.v4.content.ContextCompat.getDrawable(ContextCompat.java:323)
at android.support.v7.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:180)
at android.support.v7.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:173)
at android.support.v7.widget.TintTypedArray.getDrawable(TintTypedArray.java:60)
at android.support.v7.widget.Toolbar.<init>(Toolbar.java:254)
at android.support.v7.widget.Toolbar.<init>(Toolbar.java:196)
at java.lang.reflect.Constructor.constructNative(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at android.view.LayoutInflater.createView(LayoutInflater.java:600)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:702)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:761)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:769)
at android.view.LayoutInflater.inflate(LayoutInflater.java:498)
at android.view.LayoutInflater.inflate(LayoutInflater.java:398)
at android.view.LayoutInflater.inflate(LayoutInflater.java:354)
at android.support.v7.app.AppCompatDelegateImplV7.createSubDecor(AppCompatDelegateImplV7.java:379)
at android.support.v7.app.AppCompatDelegateImplV7.ensureSubDecor(AppCompatDelegateImplV7.java:300)
at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:264)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:129)
at com.example.joe.audiodomain.MainActivity.onCreate(MainActivity.java:25)
at android.app.Activity.performCreate(Activity.java:5585)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1093)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2400)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2495)
at android.app.ActivityThread.access$900(ActivityThread.java:170)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1304)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:146)
at android.app.ActivityThread.main(ActivityThread.java:5635)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1291)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1107)
at dalvik.system.NativeStart.main(Native Method)
03-17 15:29:31.839 20066-20066/? I/Process﹕ Sending signal. PID: 20066 SIG: 9
Here is my code:
`
package com.example.joe.mp3demo;
import android.app.Activity;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Adapter;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
public class ListActivity extends Activity {
ListView lv;
ArrayAdapter<String> adapter;
String songs[];
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list);
//Creating a List view instance
lv= (ListView) findViewById(R.id.listView);
//Array list to hold all songs, using findSongs method passing the SD card directory
ArrayList<File> allSongs= findSongs(Environment.getExternalStorageDirectory());
songs= new String[allSongs.size()];
for(int c=0; c< allSongs.size(); c++){
songs[c]=allSongs.get(c).getName().toString();
}
adapter= new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, songs);
lv.setAdapter(adapter);
}
//function to find all songs in SD card. Storing them in a temp ArrayList and returning
public ArrayList<File> findSongs(File root){
ArrayList<File> al= new ArrayList<File>();
File[] files=root.listFiles();
for(File singleFile: files){
if(singleFile.isDirectory() && !singleFile.isHidden()) {
al.addAll(findSongs(singleFile));
}
else{
if(singleFile.getName().endsWith(".mp3") || singleFile.getName().endsWith(".wav") || singleFile.getName().endsWith(".wma") ){
al.add(singleFile);
}
}
}
return al;
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_list, 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();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
Try this code:-
public class FindMp3Files extends AppCompatActivity {
ListView mp3List;
String title, artist, path, displayName, songDuration;
ArrayList<Mp3Model> songs;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_find_mp3_files);
mp3List = (ListView) findViewById(R.id.mp3List);
songs = new ArrayList<>();
scanSdcard();
SongAdapter adapter = new SongAdapter(this, songs);
mp3List.setAdapter(adapter);
}
private void scanSdcard() {
String selection = MediaStore.Audio.Media.IS_MUSIC + " != 0";
String[] projection = {
MediaStore.Audio.Media.TITLE,
MediaStore.Audio.Media.ARTIST,
MediaStore.Audio.Media.DATA,
MediaStore.Audio.Media.DISPLAY_NAME,
MediaStore.Audio.Media.DURATION
};
final String sortOrder = MediaStore.Audio.AudioColumns.TITLE + " COLLATE LOCALIZED ASC";
Cursor cursor = null;
try {
Uri uri = android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
cursor = this.getContentResolver().query(uri, projection, selection, null, sortOrder);
if (cursor != null) {
cursor.moveToFirst();
while (!cursor.isAfterLast()) {
Mp3Model mp3Model = new Mp3Model();
title = cursor.getString(0);
artist = cursor.getString(1);
path = cursor.getString(2);
displayName = cursor.getString(3);
songDuration = cursor.getString(4);
mp3Model.setTitle(title);
mp3Model.setArtist(artist);
mp3Model.setPath(path);
mp3Model.setDisplayName(displayName);
mp3Model.setSongDuration(songDuration);
songs.add(mp3Model);
cursor.moveToNext();
}
}
} catch (Exception e) {
Log.e("Tag", e.toString());
} finally {
if (cursor != null) {
cursor.close();
}
}
}
}
Here is the Mp3Model class :-
public class Mp3Model {
String title, artist, path, displayName, songDuration;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getArtist() {
return artist;
}
public void setArtist(String artist) {
this.artist = artist;
}
public String getPath() {
return path;
}
public void setPath(String path) {
this.path = path;
}
public String getDisplayName() {
return displayName;
}
public void setDisplayName(String displayName) {
this.displayName = displayName;
}
public String getSongDuration() {
return songDuration;
}
public void setSongDuration(String songDuration) {
this.songDuration = songDuration;
}
}
SongAdapter.java
public class SongAdapter extends BaseAdapter {
Context context;
ArrayList<Mp3Model> mp3Models;
LayoutInflater inflater;
Typeface captureFont;
public SongAdapter(Context context, ArrayList<Mp3Model> mp3Models) {
this.context = context;
this.mp3Models = mp3Models;
inflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
captureFont = Typeface.createFromAsset(this.context.getAssets(), "fonts/Montserrat.ttf");
}
#Override
public int getCount() {
return mp3Models.size();
}
#Override
public Object getItem(int position) {
return position;
}
#Override
public long getItemId(int position) {
return position;
}
public class ViewHolder {
TextView songName;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder vh;
if (convertView == null) {
vh = new ViewHolder();
convertView = inflater.inflate(R.layout.song_row, parent, false);
vh.songName = (TextView) convertView.findViewById(R.id.songName);
convertView.setTag(vh);
} else {
vh = (ViewHolder) convertView.getTag();
}
vh.songName.setText(mp3Models.get(position).getTitle());
vh.songName.setTypeface(captureFont);
vh.songName.setPadding(10, 10, 10, 10);
vh.songName.setTextColor(Color.WHITE);
vh.songName.setBackgroundColor(Color.parseColor("#333333"));
vh.songName.setTextSize(TypedValue.COMPLEX_UNIT_SP, 18);
return convertView;
}
}
I'm trying to make my class Parcelable, but I keep getting these errors:
08-28 07:42:55.598 7810-7810/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.IllegalStateException: Could not execute method of the activity
at android.view.View$1.onClick(View.java:3617)
at android.view.View.performClick(View.java:4222)
at android.view.View$PerformClick.run(View.java:17620)
at android.os.Handler.handleCallback(Handler.java:800)
at android.os.Handler.dispatchMessage(Handler.java:100)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5391)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:126)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at android.view.View$1.onClick(View.java:3612)
at android.view.View.performClick(View.java:4222)
at android.view.View$PerformClick.run(View.java:17620)
at android.os.Handler.handleCallback(Handler.java:800)
at android.os.Handler.dispatchMessage(Handler.java:100)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5391)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:126)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.RuntimeException: Parcelable encountered IOException writing serializable object (name = thesleeplesselite.drgreenthumb.CropRotationPlan$SubPlan)
at android.os.Parcel.writeSerializable(Parcel.java:1279)
at android.os.Parcel.writeValue(Parcel.java:1233)
at android.os.Parcel.writeList(Parcel.java:622)
at thesleeplesselite.drgreenthumb.CropRotationPlan.writeToParcel(CropRotationPlan.java:248)
at android.os.Parcel.writeParcelable(Parcel.java:1254)
at android.os.Parcel.writeValue(Parcel.java:1173)
at android.os.Parcel.writeMapInternal(Parcel.java:591)
at android.os.Bundle.writeToParcel(Bundle.java:1627)
at android.os.Parcel.writeBundle(Parcel.java:605)
at android.content.Intent.writeToParcel(Intent.java:6850)
at android.app.ActivityManagerProxy.startActivity(ActivityManagerNative.java:1908)
at android.app.Instrumentation.execStartActivity(Instrumentation.java:1483)
at android.app.Activity.startActivityForResult(Activity.java:3388)
at android.app.Activity.startActivityForResult(Activity.java:3349)
at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:820)
at thesleeplesselite.drgreenthumb.CropRotationPlannerActivity.btnViewPlan(CropRotationPlannerActivity.java:197)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at android.view.View$1.onClick(View.java:3612)
at android.view.View.performClick(View.java:4222)
at android.view.View$PerformClick.run(View.java:17620)
at android.os.Handler.handleCallback(Handler.java:800)
at android.os.Handler.dispatchMessage(Handler.java:100)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5391)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:126)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.io.NotSerializableException: thesleeplesselite.drgreenthumb.CropRotationPlan
at java.io.ObjectOutputStream.writeNewObject(ObjectOutputStream.java:1364)
at java.io.ObjectOutputStream.writeObjectInternal(ObjectOutputStream.java:1671)
at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1517)
at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1481)
at java.io.ObjectOutputStream.writeFieldValues(ObjectOutputStream.java:979)
at java.io.ObjectOutputStream.defaultWriteObject(ObjectOutputStream.java:368)
at java.io.ObjectOutputStream.writeHierarchy(ObjectOutputStream.java:1074)
at java.io.ObjectOutputStream.writeNewObject(ObjectOutputStream.java:1404)
at java.io.ObjectOutputStream.writeObjectInternal(ObjectOutputStream.java:1671)
at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1517)
at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1481)
at android.os.Parcel.writeSerializable(Parcel.java:1274)
at android.os.Parcel.writeValue(Parcel.java:1233)
at android.os.Parcel.writeList(Parcel.java:622)
at thesleeplesselite.drgreenthumb.CropRotationPlan.writeToParcel(CropRotationPlan.java:248)
at android.os.Parcel.writeParcelable(Parcel.java:1254)
at android.os.Parcel.writeValue(Parcel.java:1173)
at android.os.Parcel.writeMapInternal(Parcel.java:591)
at android.os.Bundle.writeToParcel(Bundle.java:1627)
at android.os.Parcel.writeBundle(Parcel.java:605)
at android.content.Intent.writeToParcel(Intent.java:6850)
at android.app.ActivityManagerProxy.startActivity(ActivityManagerNative.java:1908)
at android.app.Instrumentation.execStartActivity(Instrumentation.java:1483)
at android.app.Activity.startActivityForResult(Activity.java:3388)
at android.app.Activity.startActivityForResult(Activity.java:3349)
at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:820)
at thesleeplesselite.drgreenthumb.CropRotationPlannerActivity.btnViewPlan(CropRotationPlannerActivity.java:197)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at android.view.View$1.onClick(View.java:3612)
at android.view.View.performClick(View.java:4222)
at android.view.View$PerformClick.run(View.java:17620)
at android.os.Handler.handleCallback(Handler.java:800)
at android.os.Handler.dispatchMessage(Handler.java:100)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5391)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:126)
at dalvik.system.NativeStart.main(Native Method)
Here's the structure of my class; it has an inner class. I don't think it's ideal to put my entire code. It's just too long.
public class CropRotationPlan implements Parcelable {
String name; // Name of plan
HashMap<String, List<HashMap<String, String>>> listOfFamilies; // HashMap containing Plant Family Names as keys and a List of plants
List<SubPlan> listOfSubPlans;
public CropRotationPlan() {
name = null;
listOfFamilies = new HashMap<>();
listOfSubPlans = new ArrayList<>();
}
...
#Override
public int describeContents() {
return 0;
}
#Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(this.name);
dest.writeSerializable(this.listOfFamilies);
dest.writeList(this.listOfSubPlans);
}
protected CropRotationPlan(Parcel in) {
this.name = in.readString();
this.listOfFamilies = (HashMap<String, List<HashMap<String, String>>>) in.readSerializable();
this.listOfSubPlans = new ArrayList<SubPlan>();
in.readList(this.listOfSubPlans, List.class.getClassLoader());
}
public static final Parcelable.Creator<CropRotationPlan> CREATOR = new Parcelable.Creator<CropRotationPlan>() {
public CropRotationPlan createFromParcel(Parcel source) {
return new CropRotationPlan(source);
}
public CropRotationPlan[] newArray(int size) {
return new CropRotationPlan[size];
}
};
public class SubPlan implements Serializable {
int year;
HashMap<String, Integer> orderOfFamilies;
...
}
}
At this point, I seriously have no idea what to do.
CropRotationPlan.java:248 is dest.writeList(this.listOfSubPlans); in writeToParcel()
Also, in another class, I have this method:
public void btnViewPlan(View view) {
// We'll be sending our cropRotationPlan object to next activity (PlanActivity)
// And we'll be expecting a result (since we'll allow the user to manipulate the object there)
Intent i = new Intent(CropRotationPlannerActivity.this, PlanActivity.class);
i.putExtra("cropRotationPlan", cropRotationPlan);
startActivityForResult(i, SET_PLAN_REQUEST);
}
CropRotationPlannerActivity.java:197 is: startActivityForResult(i, SET_PLAN_REQUEST);
I've tried placing my inner class into a separate class, and then had it to implement Serializable OR Parcelable (just one of 'em) but I just keep getting errors. All relating to my SubPlan class; like problems with marshing it and stuff. Along with using TypedList instead somewhere in the code.
Help would be appreciated. Thanks!
I just don't know what to do anymore.
PS: I'm sorry if I sound so incoherent right now. I might edit this post once I recover my sanity.
CODE FOR SubPlan CLASS
public class SubPlan implements Serializable {
int year;
HashMap<String, Integer> orderOfFamilies;
public SubPlan(int year) {
orderOfFamilies = new HashMap<>();
this.year = year;
int temp = -1;
for (String key : listOfFamilies.keySet()) {
orderOfFamilies.put(key, ++temp);
}
}
public SubPlan(int year, HashMap<String, Integer> orderOfFamilies) {
this.year = year;
this.orderOfFamilies = orderOfFamilies;
}
public int getYear() {
return year;
}
public String getName() {
return "Year " + year;
}
public String getKey(int index) {
for (String key : orderOfFamilies.keySet()) {
if (orderOfFamilies.get(key).equals(index))
return key;
}
return null;
}
public int getLocationOf(String family) {
return orderOfFamilies.get(family);
}
public void setOrderOfFamily(String family, int index) {
if (orderOfFamilies.containsKey(family))
orderOfFamilies.put(family, index);
}
public void removeFamily(String family) {
if (orderOfFamilies.containsKey(family)) {
orderOfFamilies.remove(family);
}
}
public void rotateFamilies() {
for (int i = 0; i < getNumberOfFamilies(); i++) {
String family = getKey(i);
// if not last family on list
if (orderOfFamilies.get(family) != getNumberOfFamilies() - 1)
setOrderOfFamily(family, i + 1);
else
setOrderOfFamily(family, 0);
}
}
}
Additionally, getNumberOfFamilies() is a method belonging to the parent class, with the following definition:
public int getNumberOfFamilies() {
return listOfFamilies.size();
}
I have initially not let SubPlan implement Serializable and these are the exceptions I get:
08-28 08:31:07.809 8741-8741/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.IllegalStateException: Could not execute method of the activity
at android.view.View$1.onClick(View.java:3617)
at android.view.View.performClick(View.java:4222)
at android.view.View$PerformClick.run(View.java:17620)
at android.os.Handler.handleCallback(Handler.java:800)
at android.os.Handler.dispatchMessage(Handler.java:100)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5391)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:126)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at android.view.View$1.onClick(View.java:3612)
at android.view.View.performClick(View.java:4222)
at android.view.View$PerformClick.run(View.java:17620)
at android.os.Handler.handleCallback(Handler.java:800)
at android.os.Handler.dispatchMessage(Handler.java:100)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5391)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:126)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.RuntimeException: Parcel: unable to marshal value thesleeplesselite.drgreenthumb.CropRotationPlan$SubPlan#4191f010
at android.os.Parcel.writeValue(Parcel.java:1235)
at android.os.Parcel.writeList(Parcel.java:622)
at thesleeplesselite.drgreenthumb.CropRotationPlan.writeToParcel(CropRotationPlan.java:249)
at android.os.Parcel.writeParcelable(Parcel.java:1254)
at android.os.Parcel.writeValue(Parcel.java:1173)
at android.os.Parcel.writeMapInternal(Parcel.java:591)
at android.os.Bundle.writeToParcel(Bundle.java:1627)
at android.os.Parcel.writeBundle(Parcel.java:605)
at android.content.Intent.writeToParcel(Intent.java:6850)
at android.app.ActivityManagerProxy.startActivity(ActivityManagerNative.java:1908)
at android.app.Instrumentation.execStartActivity(Instrumentation.java:1483)
at android.app.Activity.startActivityForResult(Activity.java:3388)
at android.app.Activity.startActivityForResult(Activity.java:3349)
at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:820)
at thesleeplesselite.drgreenthumb.CropRotationPlannerActivity.btnViewPlan(CropRotationPlannerActivity.java:197)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at android.view.View$1.onClick(View.java:3612)
at android.view.View.performClick(View.java:4222)
at android.view.View$PerformClick.run(View.java:17620)
at android.os.Handler.handleCallback(Handler.java:800)
at android.os.Handler.dispatchMessage(Handler.java:100)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5391)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:126)
at dalvik.system.NativeStart.main(Native Method)
08-28 08:31:09.543 531-1643/? E/InputDispatcher﹕ channel '41d34990 thesleeplesselite.drgreenthumb/thesleeplesselite.drgreenthumb.MainActivity (server)' ~ Channel is unrecoverably broken and will be disposed!
08-28 08:31:09.543 531-1643/? E/InputDispatcher﹕ channel '4281f970 thesleeplesselite.drgreenthumb/thesleeplesselite.drgreenthumb.CropRotationPlannerActivity (server)' ~ Channel is unrecoverably broken and will be disposed!
Additionally, I have tried implementing Parcelable on SubPlan and made it have its own class:
08-28 08:54:59.291 9245-9245/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.IllegalStateException: Could not execute method of the activity
at android.view.View$1.onClick(View.java:3617)
at android.view.View.performClick(View.java:4222)
at android.view.View$PerformClick.run(View.java:17620)
at android.os.Handler.handleCallback(Handler.java:800)
at android.os.Handler.dispatchMessage(Handler.java:100)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5391)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:126)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at android.view.View$1.onClick(View.java:3612)
at android.view.View.performClick(View.java:4222)
at android.view.View$PerformClick.run(View.java:17620)
at android.os.Handler.handleCallback(Handler.java:800)
at android.os.Handler.dispatchMessage(Handler.java:100)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5391)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:126)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.StackOverflowError
at java.io.SerializationHandleMap.get(SerializationHandleMap.java:63)
at java.io.ObjectOutputStream.dumpCycle(ObjectOutputStream.java:425)
at java.io.ObjectOutputStream.writeObjectInternal(ObjectOutputStream.java:1566)
at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1517)
at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1481)
at java.util.HashMap.writeObject(HashMap.java:1015)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at java.io.ObjectOutputStream.writeHierarchy(ObjectOutputStream.java:1053)
at java.io.ObjectOutputStream.writeNewObject(ObjectOutputStream.java:1404)
at java.io.ObjectOutputStream.writeObjectInternal(ObjectOutputStream.java:1671)
at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1517)
at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1481)
at java.util.ArrayList.writeObject(ArrayList.java:644)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at java.io.ObjectOutputStream.writeHierarchy(ObjectOutputStream.java:1053)
at java.io.ObjectOutputStream.writeNewObject(ObjectOutputStream.java:1404)
at java.io.ObjectOutputStream.writeObjectInternal(ObjectOutputStream.java:1671)
at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1517)
at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1481)
at java.util.HashMap.writeObject(HashMap.java:1016)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at java.io.ObjectOutputStream.writeHierarchy(ObjectOutputStream.java:1053)
at java.io.ObjectOutputStream.writeNewObject(ObjectOutputStream.java:1404)
at java.io.ObjectOutputStream.writeObjectInternal(ObjectOutputStream.java:1671)
at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1517)
at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1481)
at android.os.Parcel.writeSerializable(Parcel.java:1274)
at thesleeplesselite.drgreenthumb.CropRotationPlan.writeToParcel(CropRotationPlan.java:185)
at android.os.Parcel.writeParcelable(Parcel.java:1254)
at thesleeplesselite.drgreenthumb.SubPlan.writeToParcel(SubPlan.java:83)
at android.os.Parcel.writeTypedList(Parcel.java:1017)
at thesleeplesselite.drgreenthumb.CropRotationPlan.writeToParcel(CropRotationPlan.java:186)
at android.os.Parcel.writeParcelable(Parcel.java:1254)
at thesleeplesselite.drgreenthumb.SubPlan.writeToParcel(SubPlan.java:83)
at android.os.Parcel.writeTypedList(Parcel.java:1017)
at thesleeplesselite.drgreenthumb.CropRotationPlan.writeToParcel(CropRotationPlan.java:186)
at android.os.Parcel.writeParcelable(Parcel.java:1254)
at thesleeplesselite.drgreenthumb.SubPlan.writeToParcel(SubPlan.java:83)
at android.os.Parcel.writeTypedList(Parcel.java:1017)
at thesleeplesselite.drgreenthumb.CropRotationPlan.write
The root cause is listed at the bottom, and the error says it all:
java.io.NotSerializableException: thesleeplesselite.drgreenthumb.CropRotationPlan
The class CropRotationPlan must be Serializable.
Update
Because SubPlan is not static, serializing SubPlan also means serializing the outer class CropRotationPlan. Since only the SubPlan constructor seems to use the outer class, change the class to static and send listOfFamilies in as a parameter on the constructor.
I have some trouble with my code and i must have missed something.
here is my code.
I'm trying to build a simple fragment app with one listview window for users. and i can't seem to get it right.
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, 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);
}
activity_main.xml
<RelativeLayout 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.shoppinglist.MainActivity" >
<fragment
android:name="com.example.shoppinglist.CartList"
android:id="#+id/fragment1"
android:layout_width="0px"
android:layout_height="match_parent" />
</RelativeLayout>
CartList code
public class CartList extends ListFragment{
MyArray ma;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/*
// Create the adapter to convert the array to views
User newUser = new User("Nathan", "San Diego");
adapter.add(newUser);*/
ArrayList<User> arrayOfUsers = new ArrayList<User>();
UsersAdapter adapter = new UsersAdapter(getActivity(), arrayOfUsers);
ListView listView = (ListView) getView().findViewById(R.id.CartListView);
listView.setAdapter(adapter);
}
cartlist.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" >
<ListView
android:id="#+id/CartListView"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
UsersAdapter code
public class UsersAdapter extends ArrayAdapter<User> {
// View lookup cache
private static class ViewHolder {
TextView name;
TextView home;
}
public UsersAdapter(Context context, ArrayList<User> users) {
super(context, R.layout.item_user, users);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// Get the data item for this position
User user = getItem(position);
// Check if an existing view is being reused, otherwise inflate the view
ViewHolder viewHolder; // view lookup cache stored in tag
if (convertView == null) {
viewHolder = new ViewHolder();
LayoutInflater inflater = LayoutInflater.from(getContext());
convertView = inflater.inflate(R.layout.item_user, parent, false);
viewHolder.name = (TextView) convertView.findViewById(R.id.tvName);
viewHolder.home = (TextView) convertView.findViewById(R.id.tvHome);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
// Populate the data into the template view using the data object
viewHolder.name.setText(user.name);
viewHolder.home.setText(user.hometown);
// Return the completed view to render on screen
return convertView;
public class User {
public String name;
public String hometown;
public User(String name, String hometown) {
this.name = name;
this.hometown = hometown;
}
I get this fatal exception
FATAL EXCEPTION: main
02-25 00:39:00.237: E/AndroidRuntime(1912): Process: com.example.shoppinglist, PID: 1912
02-25 00:39:00.237: E/AndroidRuntime(1912): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.shoppinglist/com.example.shoppinglist.MainActivity}: android.view.InflateException: Binary XML file line #10: Error inflating class fragment
02-25 00:39:00.237: E/AndroidRuntime(1912): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
02-25 00:39:00.237: E/AndroidRuntime(1912): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
02-25 00:39:00.237: E/AndroidRuntime(1912): at android.app.ActivityThread.access$800(ActivityThread.java:144)
02-25 00:39:00.237: E/AndroidRuntime(1912): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
02-25 00:39:00.237: E/AndroidRuntime(1912): at android.os.Handler.dispatchMessage(Handler.java:102)
02-25 00:39:00.237: E/AndroidRuntime(1912): at android.os.Looper.loop(Looper.java:135)
02-25 00:39:00.237: E/AndroidRuntime(1912): at android.app.ActivityThread.main(ActivityThread.java:5221)
02-25 00:39:00.237: E/AndroidRuntime(1912): at java.lang.reflect.Method.invoke(Native Method)
02-25 00:39:00.237: E/AndroidRuntime(1912): at java.lang.reflect.Method.invoke(Method.java:372)
02-25 00:39:00.237: E/AndroidRuntime(1912): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
02-25 00:39:00.237: E/AndroidRuntime(1912): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
02-25 00:39:00.237: E/AndroidRuntime(1912): Caused by: android.view.InflateException: Binary XML file line #10: Error inflating class fragment
02-25 00:39:00.237: E/AndroidRuntime(1912): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:763)
02-25 00:39:00.237: E/AndroidRuntime(1912): at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
02-25 00:39:00.237: E/AndroidRuntime(1912): at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
02-25 00:39:00.237: E/AndroidRuntime(1912): at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
02-25 00:39:00.237: E/AndroidRuntime(1912): at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
02-25 00:39:00.237: E/AndroidRuntime(1912): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:377)
02-25 00:39:00.237: E/AndroidRuntime(1912): at android.app.Activity.setContentView(Activity.java:2144)
02-25 00:39:00.237: E/AndroidRuntime(1912): at com.example.shoppinglist.MainActivity.onCreate(MainActivity.java:13)
02-25 00:39:00.237: E/AndroidRuntime(1912): at android.app.Activity.performCreate(Activity.java:5933)
02-25 00:39:00.237: E/AndroidRuntime(1912): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
02-25 00:39:00.237: E/AndroidRuntime(1912): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
02-25 00:39:00.237: E/AndroidRuntime(1912): ... 10 more
02-25 00:39:00.237: E/AndroidRuntime(1912): Caused by: java.lang.NullPointerException: Attempt to invoke virtual method ' android.view.View android.view.View.findViewById(int)' on a null object reference
02-25 00:39:00.237: E/AndroidRuntime(1912): at com.example.shoppinglist.CartList.onCreate(CartList.java:25)
02-25 00:39:00.237: E/AndroidRuntime(1912): at android.app.Fragment.performCreate(Fragment.java:2031)
02-25 00:39:00.237: E/AndroidRuntime(1912): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:863)
02-25 00:39:00.237: E/AndroidRuntime(1912): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1045)
02-25 00:39:00.237: E/AndroidRuntime(1912): at android.app.FragmentManagerImpl.addFragment(FragmentManager.java:1147)
02-25 00:39:00.237: E/AndroidRuntime(1912): at android.app.FragmentManagerImpl.onCreateView(FragmentManager.java:2116)
02-25 00:39:00.237: E/AndroidRuntime(1912): at android.app.Activity.onCreateView(Activity.java:5282)
02-25 00:39:00.237: E/AndroidRuntime(1912): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:733)
02-25 00:39:00.237: E/AndroidRuntime(1912): ... 20 more`
Change
android:name="com.example.shoppinglist.CartList"
To
class="com.example.shoppinglist.CartList"
And make sure your activity is extending FragmentActivity and not Activity
Also make sure your arraylist is not empty. Add item in it in this manner
ArrayList<User> arrayOfUsers = new ArrayList<User>();
User newUser = new User("Nathan", "San Diego");
arrayOfUsers.add(newUser);
So I have a spinner, a button and edittext, and according to which item is selected on spinner and which value is in edittext something happens on button click. It used to work perfectly and now it suddenly crashes every time I click the button. I have no idea what I have done to ruin it so I am kindly asking you for some help. I've been stuck like this for hours and I just can't point a finger to what ruined the code.
Button click method (As you can see in case 1, I added shared preferences to a comment just to make sure this isn't what's runing the code):
public void submitQuantityButton (View v){
Button submitButton = (Button)findViewById(R.id.submitButton);
final Spinner sItems = (Spinner)findViewById(R.id.spinner1);
final Context context = this;
final CheckBox cb4 = (CheckBox) findViewById(R.id.checkBox4);
submitButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
final int position = sItems.getSelectedItemPosition();
EditText quantityEditText = (EditText)findViewById(R.id.editText1);
switch (position){
case 0:
AlertDialog.Builder spinnerErrorBuilder = new AlertDialog.Builder(context);
spinnerErrorBuilder.setTitle("Warning");
spinnerErrorBuilder.setMessage("Please choose an item from the list");
spinnerErrorBuilder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog spinnerError = spinnerErrorBuilder.create();
spinnerError.show();
break;
case 1:
String items = quantityEditText.getText().toString();
cb4.setText("Elaborate Totem (" + items + "/250)");
//saveItemQuantity("cb4", cb4.getText().toString());
break;
}
}
});
}
Button XML:
<Button
android:id="#+id/submitButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/editText1"
android:layout_alignBottom="#+id/editText1"
android:layout_alignLeft="#+id/checkBox25"
android:text="#string/addMaterial"
android:onClick="submitQuantityButton" />
Logcat file:
06-23 15:05:29.276: E/AndroidRuntime(22681): FATAL EXCEPTION: main
06-23 15:05:29.276: E/AndroidRuntime(22681): java.lang.IllegalStateException: Could not execute method of the activity
06-23 15:05:29.276: E/AndroidRuntime(22681): at android.view.View$1.onClick(View.java:3599)
06-23 15:05:29.276: E/AndroidRuntime(22681): at android.view.View.performClick(View.java:4204)
06-23 15:05:29.276: E/AndroidRuntime(22681): at android.view.View$PerformClick.run(View.java:17355)
06-23 15:05:29.276: E/AndroidRuntime(22681): at android.os.Handler.handleCallback(Handler.java:725)
06-23 15:05:29.276: E/AndroidRuntime(22681): at android.os.Handler.dispatchMessage(Handler.java:92)
06-23 15:05:29.276: E/AndroidRuntime(22681): at android.os.Looper.loop(Looper.java:137)
06-23 15:05:29.276: E/AndroidRuntime(22681): at android.app.ActivityThread.main(ActivityThread.java:5041)
06-23 15:05:29.276: E/AndroidRuntime(22681): at java.lang.reflect.Method.invokeNative(Native Method)
06-23 15:05:29.276: E/AndroidRuntime(22681): at java.lang.reflect.Method.invoke(Method.java:511)
06-23 15:05:29.276: E/AndroidRuntime(22681): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
06-23 15:05:29.276: E/AndroidRuntime(22681): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
06-23 15:05:29.276: E/AndroidRuntime(22681): at dalvik.system.NativeStart.main(Native Method)
06-23 15:05:29.276: E/AndroidRuntime(22681): Caused by: java.lang.reflect.InvocationTargetException
06-23 15:05:29.276: E/AndroidRuntime(22681): at java.lang.reflect.Method.invokeNative(Native Method)
06-23 15:05:29.276: E/AndroidRuntime(22681): at java.lang.reflect.Method.invoke(Method.java:511)
06-23 15:05:29.276: E/AndroidRuntime(22681): at android.view.View$1.onClick(View.java:3594)
06-23 15:05:29.276: E/AndroidRuntime(22681): ... 11 more
06-23 15:05:29.276: E/AndroidRuntime(22681): Caused by: java.lang.ClassCastException: android.widget.Spinner cannot be cast to android.widget.Button
06-23 15:05:29.276: E/AndroidRuntime(22681): at com.example.gw2legendary.Bifrost.submitQuantityButton(Bifrost.java:967)
06-23 15:05:29.276: E/AndroidRuntime(22681): ... 14 more
These kinds of errors normally happen when you have moved two elements inside your layout (switched their places). A Project --> Clean in Eclipse normally resolves this, which makes Android rebuild the R-class.