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.
Related
I'm wanting to let the user create an avatar (profile picture if you like) when they set up their user info. I've created a method for a single click/touch which would ask the user to take a picture and one for a long click which would ask the user to choose a picture from their gallery.
Below are my methods from the class file:
public void onLaunchCamera(View v) {
avatarButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
String strAvatarPrompt = "Take your picture to store as your avatar!";
Intent pictureIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(Intent.createChooser(pictureIntent, strAvatarPrompt), TAKE_AVATAR_CAMERA_REQUEST);
}
});
avatarButton.setOnLongClickListener(new View.OnLongClickListener() {
public boolean onLongClick(View v) {
String strAvatarPrompt = "Choose a picture to use as your avatar!";
Intent pickPhoto = new Intent(Intent.ACTION_PICK);
pickPhoto.setType("image/*");
startActivityForResult(Intent.createChooser(pickPhoto, strAvatarPrompt), TAKE_AVATAR_GALLERY_REQUEST);
return true;
}
});
}
And below is the XML associated with the ImageButton:
<ImageButton
android:id="#+id/ImageButton_Avatar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:maxHeight="#dimen/avatar_size"
android:minHeight="#dimen/avatar_size"
android:onClick="onLaunchCamera"
android:scaleType="fitXY"
android:src="#drawable/avatar"></ImageButton>
All it does is crash when I click on the ImageButton and I have no idea why. Any ideas?
Thanks
EDIT: Adding the logcat below (Sorry about the formatting. Couldn't work out how to get it all sorted properly:
[ 04-12 18:32:50.989 5901: 5901 D/ ]
HostConnection::get() New Host Connection established 0xb8a44530, tid 5901
04-12 18:32:51.039 5901-5901/cct.mad.lab D/OpenGLRenderer: Enabling debug mode 0
04-12 18:32:55.739 5901-5901/cct.mad.lab V/RenderScript: 0xb8c53300 Launching thread(s), CPUs 2
04-12 18:32:57.389 5901-5901/cct.mad.lab D/AndroidRuntime: Shutting down VM
04-12 18:32:57.389 5901-5901/cct.mad.lab W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0xb0d2db20)
04-12 18:32:57.399 5901-5901/cct.mad.lab E/AndroidRuntime: FATAL EXCEPTION: main
Process: cct.mad.lab, PID: 5901 java.lang.IllegalStateException: Could not execute method of the activity
at android.view.View$1.onClick(View.java:3823)
at android.view.View.performClick(View.java:4438)
at android.view.View$PerformClick.run(View.java:18422)
at a android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
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:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
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:515)
at android.view.View$1.onClick(View.java:3818)
at android.view.View.performClick(View.java:4438)
at android.view.View$PerformClick.run(View.java:18422)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
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:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at cct.mad.lab.SettingsActivity.onLaunchCamera(SettingsActivity.java:201)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at android.view.View$1.onClick(View.java:3818)
at android.view.View.performClick(View.java:4438)
at android.view.View$PerformClick.run(View.java:18422)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
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:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)
It looks like you might not have defined avatarButton, if you follow the Caused By path on the LogCat you see the bottom one is a NullPointerException.
Since I can't see the line numbers, the issue is happening on line 201--the only obvious null pointer I see in your code is avatarButton.
Based on what you want to do, you'll want to go about this a bit differently.
Remove the android:onClick="onLaunchCamera" from the XML.
in your onCreate() after you set the content view add the following:
View avatarButton = findViewById(R.id.ImageButton_Avatar);
avatarButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
String strAvatarPrompt = "Take your picture to store as your avatar!";
Intent pictureIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(Intent.createChooser(pictureIntent, strAvatarPrompt), TAKE_AVATAR_CAMERA_REQUEST);
}
});
avatarButton.setOnLongClickListener(new View.OnLongClickListener() {
public boolean onLongClick(View v) {
String strAvatarPrompt = "Choose a picture to use as your avatar!";
Intent pickPhoto = new Intent(Intent.ACTION_PICK);
pickPhoto.setType("image/*");
startActivityForResult(Intent.createChooser(pickPhoto, strAvatarPrompt), TAKE_AVATAR_GALLERY_REQUEST);
return true;
}
});
This allows you to set both a click and a longClick listener with more control. The way you had it, you were never really defining the onClick or onLongClick until you clicked on them the first time.
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());
I have popupMenu and CheckBox. I need make write status CheckBox to boolean.
This code not working:
MenuItem fast_result;
boolean fast=false;
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
switch (id) {
case R.id.FastResult:
fast_result = item.getSubMenu().getItem(R.id.FastResult);//This is 182 line
fast_result.setChecked(!fast_result.isChecked());
fast=fast_result.isChecked();
return true;
}
}
It is errors:
FATAL EXCEPTION: main
java.lang.NullPointerException
at com.alexvsalex.HelpforMath.RootsActivity.onOptionsItemSelected(RootsActivity.java:182)
at android.app.Activity.onMenuItemSelected(Activity.java:2502)
at com.android.internal.policy.impl.PhoneWindow.onMenuItemSelected(PhoneWindow.java:950)
at com.android.internal.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:735)
at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:149)
at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:874)
at com.android.internal.view.menu.ListMenuPresenter.onItemClick(ListMenuPresenter.java:163)
at android.widget.AdapterView.performItemClick(AdapterView.java:292)
at android.widget.AbsListView.performItemClick(AbsListView.java:1058)
at android.widget.AbsListView$PerformClick.run(AbsListView.java:2514)
at android.widget.AbsListView$1.run(AbsListView.java:3168)
at android.os.Handler.handleCallback(Handler.java:605)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4424)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
at dalvik.system.NativeStart.main(Native Method)
What to do?
The problem is solved:
case R.id.FastResult:
fast_result = item; //There was an error
fast_result.setChecked(!fast_result.isChecked());
fast=fast_result.isChecked();
return true;
When I test my app, after close app and clean memory(in my case, CleanMaster app) and then reopen it again, it force close. I try search for cause but can't find solvable solution.
It work all fine if not clean memory. I wonder if anything need to be saved and restore after these incident. I'm quite newby for Android.
here is log
06-29 11:59:19.479 22283-22283/com.thaifasttel D/WebView﹕ onTrimMemory: 20
06-29 11:59:33.985 22524-22524/com.thaifasttel D/jdwp﹕ sendBufferedRequest : len=0x39
06-29 11:59:33.993 22524-22524/com.thaifasttel W/asset﹕ AssetManager-->addDefaultAssets CIP path not exsit!
06-29 11:59:34.050 22524-22524/com.thaifasttel D/dalvikvm﹕ newInstance failed: no <init>()
06-29 11:59:34.052 22524-22524/com.thaifasttel D/AndroidRuntime﹕ Shutting down VM
06-29 11:59:34.052 22524-22524/com.thaifasttel W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x40d6f9a8)
06-29 11:59:34.060 22524-22524/com.thaifasttel E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.thaifasttel/com.thaifasttel.MainActivity}: android.support.v4.app.Fragment$InstantiationException: Unable to instantiate fragment com.thaifasttel.MainActivity$PlaceholderFragment: make sure class name exists, is public, and has an empty constructor that is public
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2343)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2395)
at android.app.ActivityThread.access$600(ActivityThread.java:162)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1364)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5371)
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 dalvik.system.NativeStart.main(Native Method)
Caused by: android.support.v4.app.Fragment$InstantiationException: Unable to instantiate fragment com.thaifasttel.MainActivity$PlaceholderFragment: make sure class name exists, is public, and has an empty constructor that is public
at android.support.v4.app.Fragment.instantiate(Fragment.java:413)
at android.support.v4.app.FragmentState.instantiate(Fragment.java:97)
at android.support.v4.app.FragmentManagerImpl.restoreAllState(FragmentManager.java:1801)
at android.support.v4.app.FragmentActivity.onCreate(FragmentActivity.java:213)
at android.support.v7.app.ActionBarActivity.onCreate(ActionBarActivity.java:97)
at com.thaifasttel.MainActivity.onCreate(MainActivity.java:73)
at android.app.Activity.performCreate(Activity.java:5122)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1081)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2307)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2395)
at android.app.ActivityThread.access$600(ActivityThread.java:162)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1364)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5371)
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 dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.InstantiationException: can't instantiate class com.thaifasttel.MainActivity$PlaceholderFragment; no empty constructor
at java.lang.Class.newInstanceImpl(Native Method)
at java.lang.Class.newInstance(Class.java:1319)
at android.support.v4.app.Fragment.instantiate(Fragment.java:402)
at android.support.v4.app.FragmentState.instantiate(Fragment.java:97)
at android.support.v4.app.FragmentManagerImpl.restoreAllState(FragmentManager.java:1801)
at android.support.v4.app.FragmentActivity.onCreate(FragmentActivity.java:213)
at android.support.v7.app.ActionBarActivity.onCreate(ActionBarActivity.java:97)
at com.thaifasttel.MainActivity.onCreate(MainActivity.java:73)
at android.app.Activity.performCreate(Activity.java:5122)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1081)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2307)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2395)
at android.app.ActivityThread.access$600(ActivityThread.java:162)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1364)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5371)
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 dalvik.system.NativeStart.main(Native Method)
MainActivity.java
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment())
.commit();
}
}
#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();
return id == R.id.action_settings || super.onOptionsItemSelected(item);
}
public class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Log.e("My App", "onCreateView");
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
return rootView;
}
WebView myWebView;
RelativeLayout splash_view;
TextView splash_text;
SharedPreferences Usettings;
String lang="th";
int isloadwa=0;
AssetManager assetPath = getAssets();
String waAssetPath ="web";
String wabase=getApplicationContext().getFilesDir().getPath();
String waIntPath = "web";
String waIntPath2 = wabase+"/"+waIntPath;
String baseupdatefolder="http://www.test.com/test/webapp";
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
Log.e("My App", "onActivityCreated");
myWebView = (WebView) getView().findViewById(R.id.webview);
splash_view = (RelativeLayout) getView().findViewById(R.id.splashview);
splash_text = (TextView) getView().findViewById(R.id.splash_status);
inituserPref();
copydir();
}
#Override
public void onStart(){
super.onStart();
Log.e("My App", "onstart");
}
#Override
public void onResume(){
super.onResume();
Log.e("My App", "onResume");
if(isloadwa==1) {
if (isConnected()) {
Log.e("My App", "check update in app");
splash_text.setText(a_check_update);
checkupdate();
}
}
}
// do a lot of method after this
In my current android project I create two bitmaps from xml resources. Every time I build my project using the xml bitmaps as the resource for Bitmap.decodeResources() I get a null pointer exception when I try to access the bitmaps. But when I switch to using the straight .png the error goes away.
Why is decodeResources() returning null?
code where the bitmaps are created:
public OvertButton(float x, float y, float width, float height, int pressedId, int unPressedId, Resources res, boolean visible) {
\\these are null if xml bitmap is passed
pressed = BitmapFactory.decodeResource(res, pressedId);
unPressed = BitmapFactory.decodeResource(res, unPressedId);
this.visible = visible;
relocate(x, y);
resize(width, height); //throws NPE when I createScaledBitmap()
ClickableManager.registerClickable(this);
}
resource file
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
android:src="#drawable/play_button_pressed"
android:filter="false"
android:antialias="false"
android:dither="false"/>
and the resize method
#Override
public void resize(float width, float height) {
box.set(box.left, box.top, box.left + width, box.top + height);
Log.d("resize",String.valueOf(height));
pressed = Bitmap.createScaledBitmap(pressed, (int)width, (int)height, true);
unPressed = Bitmap.createScaledBitmap(unPressed, (int)width, (int)height, true);
}
And the logcat
04-16 09:45:06.341 11824-11824/com.handmade.eed D/dalvikvm﹕ Late-enabling CheckJNI
04-16 09:45:06.450 11824-11824/com.handmade.eed D/MenuActivity﹕ 2130837594$$$2130837596
04-16 09:45:06.451 11824-11824/com.handmade.eed D/skia﹕ --- SkImageDecoder::Factory returned null
04-16 09:45:06.451 11824-11824/com.handmade.eed D/OvertButton﹕ true2130837591
04-16 09:45:06.457 11824-11824/com.handmade.eed D/OvertButton﹕ false2130837595
04-16 09:45:06.457 11824-11824/com.handmade.eed D/resize﹕ 252.0
04-16 09:45:06.458 11824-11824/com.handmade.eed D/AndroidRuntime﹕ Shutting down VM
04-16 09:45:06.459 11824-11824/com.handmade.eed W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x41610d40)
04-16 09:45:06.461 11824-11824/com.handmade.eed E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.handmade.eed, PID: 11824
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.handmade.eed/com.handmade.eed.MenuActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2209)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2269)
at android.app.ActivityThread.access$800(ActivityThread.java:139)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1210)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5102)
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:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at android.graphics.Bitmap.createScaledBitmap(Bitmap.java:615)
at com.handmade.overt.visible.OvertButton.resize(OvertButton.java:61)
at com.handmade.overt.visible.OvertButton.<init>(OvertButton.java:29)
at com.handmade.overt.visible.OvertButton.<init>(OvertButton.java:34)
at com.handmade.eed.MenuActivity.onCreate(MenuActivity.java:72)
at android.app.Activity.performCreate(Activity.java:5248)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1110)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2173)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2269)
at android.app.ActivityThread.access$800(ActivityThread.java:139)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1210)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5102)
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:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
04-16 09:45:09.872 11824-11824/com.handmade.eed I/Process﹕ Sending signal. PID: 11824 SIG: 9
This is a case of me being dumb but here is the answer for posterity.
Bitmap is not the same as BitmapDrawable
From the Drawable Resources link above:
COMPILED RESOURCE DATATYPE:
Resource pointer to a BitmapDrawable.
Thus I had to change this:
Bitmap pressed = BitmapFactory.decodeResource(res, R.drawable.play_button_pressed_res); //this is the id for the xml referring to the png
to either this:
Drawable pressed = res.getDrawable(R.drawable.play_button_pressed_res);
or this:
Bitmap pressed = BitmapFactory.decodeResource(res, R.drawable.play_button_pressed); //this is the id for the png
I decided to use bitmap because, as I understand, it is faster.