I have an activity with a viewpager containing 2 fragments, inside each fragment contains a list, from this list a button can be pressed that creates a new fragment in the viewpager, this works fine until I create a new activity and close it, after the new activity is closed and I return to the previous activity pressing the button to create a new fragment again now gives a classcastexception, almost as if the closed activity is still open, but it is not, I have tried searching this issue and cant find any examples of this being encountered by anyone else which makes me wonder how this could be happening, I will post some code examples below, any help will go a long way thanks.
Click listener in ListAdapter:
holder.rlStatusContainer.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//add new comment fragment and set page
try {
((HomeFragmentActivity) activity).addCommentFragment(data.get(finalPosition));
} catch (Exception e) {
e.printStackTrace();
}
}
});
Method in activity:
public void addCommentFragment(ArrayList<Object> fragmentExtras) {
list.add(NavigationFragment.newInstance(9, getApplicationContext(), this, fragmentExtras));
adapter.notifyDataSetChanged();
pager.setCurrentItem(list.size());
}
Error received:
12-13 13:37:50.177 17262-17262/test.test.com.test E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: test.test.com.test, PID: 17262
java.lang.ClassCastException: test.test.com.test.activities.NewPostActivity cannot be cast to test.test.com.test.activities.HomeFragmentActivity
at test.test.com.test.adapters.ListHomeAdapter$4.onClick(ListHomeAdapter.java:462)
at android.view.View.performClick(View.java:4780)
at android.view.View$PerformClick.run(View.java:19866)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
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:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Related
In order to reset a user password in my app I decided to create a dialog for the user to enter their email so I could send them a mail to reset the password. The Dialog works and it looks like - that
The code behind creating the dialog:
public void CreateDialogResetPassword ()
{
d = new Dialog(this);
d.setContentView(R.layout.resetpassword_layout);
d.setCancelable(true);
btnResetPassword = (Button)findViewById(R.id.btnResetPassword);
etEmailResetPassword = (EditText)findViewById(R.id.etResetPasswordEmail);
d.setTitle("Reset Password");
d.show();
}
The thing is whenever I add the setOnClickListener to the btnResetPassword and then I click the Forgot Password imageButton the app crashes.
This is the code after adding the setOnClickListener method.
public void CreateDialogResetPassword ()
{
d = new Dialog(this);
d.setContentView(R.layout.resetpassword_layout);
d.setCancelable(true);
btnResetPassword = (Button)findViewById(R.id.btnResetPassword);
btnResetPassword.setOnClickListener(this);
etEmailResetPassword = (EditText)findViewById(R.id.etResetPasswordEmail);
d.setTitle("Reset Password");
d.show();
}
What happens after adding the setOnClickListener and then clicking on the Forgot Pasword imagebutton which should open the dialog is this basically crashing the app and going to home page.
And this is how the onClick looks like -
#Override
public void onClick(View v) {
if (v==btnLogin)
{
login();
}
if (v == btnSignUpMain) {
CreateDialogSignUp();
}
if (v == imageBtnSingUp) {
register();
}
if (v==imageBtnResetPassword)
{
CreateDialogResetPassword();
}
You can ignore most of the code in the onClick I don't know how much that matters but decided to add it anyway.
Logcat -
2021-05-06 17:18:15.429 9175-9175/com.example.recyclub I/AssistStructure: Flattened final assist data: 1992 bytes, containing 1 windows, 12 views
2021-05-06 17:18:18.295 9175-9175/com.example.recyclub D/AndroidRuntime: Shutting down VM
2021-05-06 17:18:18.296 9175-9175/com.example.recyclub E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.recyclub, PID: 9175
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at com.example.recyclub.MainActivity.CreateDialogResetPassword(MainActivity.java:143)
at com.example.recyclub.MainActivity.onClick(MainActivity.java:207)
at android.view.View.performClick(View.java:7448)
at android.view.View.performClickInternal(View.java:7425)
at android.view.View.access$3600(View.java:810)
at android.view.View$PerformClick.run(View.java:28305)
at android.os.Handler.handleCallback(Handler.java:938)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:223)
at android.app.ActivityThread.main(ActivityThread.java:7656)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
2021-05-06 17:18:18.333 9175-9175/com.example.recyclub I/Process: Sending signal. PID: 9175 SIG: 9
Apparently the only problem was in this code where the Dialog is being created
Button btnSubmitResetPassword = (Button)findViewById(R.id.btnSubmitResetPassword);
Needed to get reference from the dialog itself and the correct code is this -
Button btnSubmitResetPassword = (Button)d.findViewById(R.id.btnSubmitResetPassword);
Now it is referenced in a proper way.
I have added splash screen in my app but the app crashes when is run on Android 6. The interesting thing is that the app runs fine on Android 9.
Don't know what to do.
Target SDK - 28.
min SDK= 19
it is showing error on oncreate method while run app on android 6
splash.java
import android.os.Bundle;
import android.os.Handler;
public class splash extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); // it showing error here.
setContentView(R.layout.activity_splash);
int SPLASH_TIME_OUT = 4000;
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
Intent splashintent=new Intent(splash.this,MainActivity.class);
startActivity(splashintent);
finish();
}
}, SPLASH_TIME_OUT);
}
}
logs
06-22 16:17:30.930 10294-10294/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.napps.wallpaper, PID: 10294
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.napps.wallpaper/com.napps.wallpaper.splash}: android.content.res.Resources$NotFoundException: Resource ID #0x7f07009e
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2655)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2725)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1572)
at android.os.Handler.dispatchMessage(Handler.java:111)
at android.os.Looper.loop(Looper.java:207)
at android.app.ActivityThread.main(ActivityThread.java:5896)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:679)
Caused by: android.content.res.Resources$NotFoundException: Resource ID #0x7f07009e
at android.content.res.Resources.getValue(Resources.java:1595)
at androidx.appcompat.widget.AppCompatDrawableManager.loadDrawableFromDelegates(AppCompatDrawableManager.java:331)
at androidx.appcompat.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:198)
at androidx.appcompat.widget.TintTypedArray.getDrawableIfKnown(TintTypedArray.java:86)
at androidx.appcompat.app.AppCompatDelegateImpl.<init>(AppCompatDelegateImpl.java:260)
at androidx.appcompat.app.AppCompatDelegate.create(AppCompatDelegate.java:182)
at androidx.appcompat.app.AppCompatActivity.getDelegate(AppCompatActivity.java:520)
at androidx.appcompat.app.AppCompatActivity.onCreate(AppCompatActivity.java:71)
at com.napps.wallpaper.splash.onCreate(splash.java:13)
at android.app.Activity.performCreate(Activity.java:6298)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1113)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2608)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2725)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1572)
at android.os.Handler.dispatchMessage(Handler.java:111)
at android.os.Looper.loop(Looper.java:207)
at android.app.ActivityThread.main(ActivityThread.java:5896)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:679)
There might be problem with your drawable Image. While copying image Please check the folder in which you are copying.
Try to add the image in drawable folder not drawable-21
I would suggest to remove old image and Add Image again to proper directory.
I've been trying to get a dialog message to work on android and the app always crash when it reaches "dialog.show();"
public class Logic extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(context);
dialogBuilder.setTitle("Alarm");
dialogBuilder.setMessage(messageActivity.getMes());
dialogBuilder.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
AlertDialog dialog = dialogBuilder.create();
dialog.show();
}
}
Here is my logcat:
FATAL EXCEPTION: main
Process: it226.myapplicationit226androidapp, PID: 19598
java.lang.RuntimeException: Unable to start receiver it226.myapplicationit226androidapp.Logic: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
at android.app.ActivityThread.handleReceiver(ActivityThread.java:2732)
at android.app.ActivityThread.-wrap14(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1421)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
at android.view.ViewRootImpl.setView(ViewRootImpl.java:571)
at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:310)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:85)
at android.app.Dialog.show(Dialog.java:319)
at it226.myapplicationit226androidapp.Logic.onReceive(Logic.java:65)
at android.app.ActivityThread.handleReceiver(ActivityThread.java:2725)
at android.app.ActivityThread.-wrap14(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1421)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
You can't create dialog from receiver.
Creating dialogs is allowed only from UI components (which have looper).
You could start transparent activity with dialog, which would be the same for user.
You can't create a dialog using the context of a BroadcastReceiver, you has two options to solve this problem:
first one is to create a regular Activity which has only the size of the dialog as a shown part and the full remaining is transparent:
Intent newIntent = new Intent(context, Displayer.class);
newIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(newIntent);
second one is to hold your Activity context and then use it in creating the dialog, but at this method you have to make sure what is the activity that is currently open:
// in your activity onCreate
ctx = YourActivity.this; // let's suppose ctx is static and general var
////////////////////////////////////////////////
// in the BroadcastReceiver
AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(YourActivity.ctx);
This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 7 years ago.
i try to add onClick this button then create contact in database
Here is code i try to add it to main_fragment
final Button addBtn = (Button) view.findViewById(R.id.btnadd);
addBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Uri imageUri = Uri.parse("android.resource://org.intracode.contactmanager/drawable/no_user_logo.png");
import_fragment.Contact contact = new import_fragment.Contact(dbHandler.getContactsCount(), String.valueOf(nametxt.getText()), String.valueOf(phoneTxt.getText()), String.valueOf(emailTxt.getText()), String.valueOf(addressTxt.getText()), imageUri);
if (!contactExists(contact)) {
dbHandler.createContact(contact);
Contacts.add(contact);
contactAdapter.notifyDataSetChanged(); // Error in this line
Toast.makeText(getActivity().getApplicationContext(), String.valueOf(nametxt.getText()) + " has been added to your Contacts!", Toast.LENGTH_SHORT).show();
return;
}
Toast.makeText(getActivity().getApplicationContext(), String.valueOf(nametxt.getText()) + " already exists. Please use a different name.", Toast.LENGTH_SHORT).show();
}
});
When i press this button in my app, 'app has stopped working'
Here is my logcat
01-22 08:31:04.014 29398-29398/com.al3almya.users.al3almya E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.al3almya.users.al3almya, PID: 29398
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ArrayAdapter.notifyDataSetChanged()' on a null object reference
at com.al3almya.users.al3almya.main_fragment$1.onClick(main_fragment.java:77)
at android.view.View.performClick(View.java:4848)
at android.view.View$PerformClick.run(View.java:20262)
at android.os.Handler.handleCallback(Handler.java:815)
at android.os.Handler.dispatchMessage(Handler.java:104)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5637)
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:960)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
Either the contactAdapter is not initialized(contactAdater is null) or the object on which the adapter is set is null.
Make sure you have initialized all the variables. Else try running dubugger.
It appears that the variable contactAdapter has not been set.
I have successfully sent an email in the background and displayed a progress dialog to the user as said email is sent. However when the user flips the screen while the dialog is up I get an IllegalArgumentException. I have tried using a WeakReference object and it doesn't seem to be fixing the problem. This is my AsyncTask class.
private class SendMailTask extends AsyncTask<Mail, String, EmailStatusResponce> {
private final WeakReference<ProgressDialog> weakReference;
public SendMailTask() {
ProgressDialog progressDialog = new ProgressDialog(SendReportActivity.this);
progressDialog.setMessage("Sending...");
progressDialog.setCancelable(false);
progressDialog.show();
weakReference = new WeakReference<>(progressDialog);
}
#Override
protected void onPreExecute() {
super.onPreExecute();
}
#Override
protected EmailStatusResponce doInBackground(Mail... mail) {
return mail[0].send();
}
#Override
protected void onPostExecute(EmailStatusResponce result) {
super.onPostExecute(result);
if (result != null && weakReference != null) {
weakReference.get().dismiss(); // This is where the exception is thrown.
if (result.isSuccess()) {
Intent intent = new Intent(SendReportActivity.this, MainActivity.class);
startActivity(intent);
}
else {}
}
}
This is the exception
java.lang.IllegalArgumentException: View=com.android.internal.policy.PhoneWindow$DecorView{688d3e2 V.E...... R......D 0,0-1026,348} not attached to window manager
Thanks in advance for any and all help.
EDIT: More logcat
--------- beginning of crash
12-11 16:22:40.154 1976-1976/com.blazapps.allenfamilymedicine E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.blazapps.allenfamilymedicine, PID: 1976
java.lang.IllegalArgumentException: View=com.android.internal.policy.PhoneWindow$DecorView{f89e667 V.E...... R......D 0,0-760,232} not attached to window manager
at android.view.WindowManagerGlobal.findViewLocked(WindowManagerGlobal.java:424)
at android.view.WindowManagerGlobal.removeView(WindowManagerGlobal.java:350)
at android.view.WindowManagerImpl.removeViewImmediate(WindowManagerImpl.java:116)
at android.app.Dialog.dismissDialog(Dialog.java:362)
at android.app.Dialog.dismiss(Dialog.java:345)
at com.blazapps.allenfamilymedicine.SendReportActivity$SendMailTask.onPostExecute(SendReportActivity.java:168)
at com.blazapps.allenfamilymedicine.SendReportActivity$SendMailTask.onPostExecute(SendReportActivity.java:138)
at android.os.AsyncTask.finish(AsyncTask.java:651)
at android.os.AsyncTask.-wrap1(AsyncTask.java)
at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:668)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
I still do no know why the exception is being thrown but because I save lots of information in EmailStatusResponse I can just surround the exception with a try catch and if the task was complete I get the result I expected.
try {
weakReference.get().dismiss();
} catch (Exception e) {}
If anyone can figure a better solution I would really appreciate it. I hate just catching exceptions. There is usually a better way.