I am developing an android app but my app is being stopped
Here is the steps I did: I ...
1. am using tabbed activity and there is a list view
2. Each element in listView is registered for context menu
3. First option is context menu is for editing info for the view
4. Once it is clicked, it opens another activity(intent is used)
And the error is when I click close button while the new activity is open, I am having error:"Unfortunately app has stopped"
Edit Class:
public class Editor extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_editor);
Intent intent = getIntent();
int position = intent.getIntExtra("POSITION", -1);
TextView t = (TextView)findViewById(R.id.intentResult);
t.setText(position+"");
}
}
Intent Call :
public boolean onContextItemSelected(MenuItem item) {
if(item.getItemId()==EDIT_ID){
}
else if(item.getItemId()==DELETE_ID){
AdapterView.AdapterContextMenuInfo acmi = (AdapterView.AdapterContextMenuInfo)item.getMenuInfo();
Intent intent = new Intent(getContext(), Editor.class);
intent.putExtra("POSITION", acmi.position);
startActivity(intent);
}
return super.onContextItemSelected(item);
}
//acmi is just an integer
Error: 01-21 16:54:59.714 2820-2820/com.example.abu_bakr.darsjadvali
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.abu_bakr.darsjadvali, PID: 2820
java.lang.RuntimeException: Unable to resume activity
{com.example.abu_bakr.darsjadvali/com.example.abu_bakr.darsjadvali.MainActivity}:
java.lang.IllegalStateException: trying to requery an already closed
cursor android.database.sqlite.SQLiteCursor#2e3a8e12
at
android.app.ActivityThread.performResumeActivity(ActivityThread.java:3033)
at
android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3064)
at
android.app.ActivityThread$H.handleMessage(ActivityThread.java:1353)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5356)
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:908)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:703)
Caused by: java.lang.IllegalStateException: trying to requery an
already closed cursor android.database.sqlite.SQLiteCursor#2e3a8e12
at android.app.Activity.performRestart(Activity.java:6076)
at android.app.Activity.performResume(Activity.java:6099)
at
android.app.ActivityThread.performResumeActivity(ActivityThread.java:3022)
at
android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3064)
at
android.app.ActivityThread$H.handleMessage(ActivityThread.java:1353)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5356)
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:908)
at com
.android.internal.os.ZygoteInit.main(ZygoteInit.java:703)
As your error log says, it's a database exception.
Post the complete code of your MainActivity so we can understand what's going on
Related
i have this code in MainActivity.java:
String passName = new String(getString(R.string.name));
where name is a string in strings.xml:
<string name="name">My name</string>
it does not give me an error, but the app keeps crashing, how can i do this properly?
basically i want to save my name as a string variable in MainActivity.java, but store the actual text in strings.xml
i originally had:
String passName = new String("my name");
and i was able to successfully pass it to the second activity, but i want the text to be stored in strings.xml, not the .java file
edit: i have provided more of my code for context:
public class MainActivity extends AppCompatActivity {
Button button;
//code that will eventually point to my name in strings.xml
String passName = new String("my name");
//String passName = getResources().getString(R.string.name);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//code that senses when the button is clicked
button = findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//code that executes when the button is clicked
Toast.makeText(getApplicationContext(),"Moving to second activity...",Toast.LENGTH_SHORT).show();
//code that passes my name to the second activity
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
intent.putExtra("key", passName);
startActivity(intent);
}
});
}
}
as for a crash log, there doesn't seem to be one. when i launch the app it opens and then immediately closes again.
the message it gives me is this:
01/19 15:21:11: Launching 'app' on Pixel 4 XL API 30 (test).
Install successfully finished in 388 ms.
$ adb shell am start -n "my.name.n0000.lab_n0000/my.name.n0000.lab_n0000.MainActivity" -a android.intent.action.MAIN -c android.intent.category.LAUNCHER
edit: i have added the proper logcat dialogue:
2023-01-19 15:32:41.587 7562-7562/my.name.n00000.lab_n00000 E/AndroidRuntime: FATAL EXCEPTION: main
Process: my.name.n00000.lab_n00000, PID: 7562
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{my.name.n00000.lab_n00000/my.name.n00000.lab_n00000.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3365)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066)
at android.os.Handler.dispatchMessage(Handler.java:106)
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)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference
at android.content.ContextWrapper.getResources(ContextWrapper.java:97)
at android.view.ContextThemeWrapper.getResourcesInternal(ContextThemeWrapper.java:134)
at android.view.ContextThemeWrapper.getResources(ContextThemeWrapper.java:128)
at androidx.appcompat.app.AppCompatActivity.getResources(AppCompatActivity.java:612)
at my.name.n00000.lab_n00000.MainActivity.<init>(MainActivity.java:17)
at java.lang.Class.newInstance(Native Method)
at android.app.AppComponentFactory.instantiateActivity(AppComponentFactory.java:95)
at androidx.core.app.CoreComponentFactory.instantiateActivity(CoreComponentFactory.java:45)
at android.app.Instrumentation.newActivity(Instrumentation.java:1253)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3353)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066)
at android.os.Handler.dispatchMessage(Handler.java:106)
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)
2023-01-19 15:32:41.615 7562-7562/my.name.n00000.lab_n00000 I/Process: Sending signal. PID: 7562 SIG: 9
There is a lifecycle involved when creating activities. Some things should not be accessed before the onCreate method is called. So you should be able to declare passName prior and then inside onCreate, you can initialise it.
public class MainActivity extends AppCompatActivity {
Button button;
String passName;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//code that senses when the button is clicked
button = findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//code that executes when the button is clicked
Toast.makeText(getApplicationContext(),"Moving to second activity...",Toast.LENGTH_SHORT).show();
//code that passes my name to the second activity
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
intent.putExtra("key", passName);
startActivity(intent);
}
});
passName = getResources().getString(R.string.name);
}
}
You can read more about the activity lifecycle here
And in particular in this paragraph, it talks about instantiating class scope variables.
For example, your implementation of onCreate() might bind data to lists, associate the activity with a ViewModel, and instantiate some class-scope variables.
This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 5 years ago.
I am trying to pass an arraylist between two activities, but my app crashes at the second activity. Can someone help me solve in this problem...
Here,
I have two parts of my MainActivity from where sending arraylist to another Activity which is BankList Activity
Part 1:
Intent intent = new Intent(MainActivity.this, BankList.class);
intent.putStringArrayListExtra("BANKLIST",bankListArrayList);
startActivity(intent);
Part 2:
Result<ArrayList<Bank>> banklist = (Result<ArrayList<Bank>>)data;
if(banklist.getCode().equals("00")){
Toast.makeText(this,"list Banks success",Toast.LENGTH_SHORT).show();
bankListArrayList = new ArrayList<>();
for(Bank bank :banklist.getData()){
bank.getIin();
bank.getLogo();
bank.getName();
b
ankListArrayList.add(bank.toString());
}
Log.d("BANK_ARRAYLIST","BANK_ARRAYLIST"+bankListArrayList);
Toast.makeText(this,"BANK_ARRAYLIST"+bankListArrayList,Toast.LENGTH_SHORT).show();
}
My Second Activity (BankList)
public class BankList extends AppCompatActivity {
ListView bankList;
ArrayList<String> bankdataList;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_bank_list);
bankList = (ListView)findViewById(R.id.bankList);
Intent intent = getIntent();
intent.getStringArrayListExtra("BANKLIST");
bankdataList.add(intent.toString());
Toast.makeText(BankList.this,"bank list"+bankdataList,Toast.LENGTH_SHORT).show();
ArrayAdapter<String> itemsAdapter = new ArrayAdapter<String>(getApplicationContext(),R.layout.support_simple_spinner_dropdown_item, bankdataList);
bankList.setAdapter(itemsAdapter);
}
}
And my Log is :-
11-09 12:58:01.548 1941-1941/com.example.rajdeeps.upi_integration E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.rajdeeps.upi_integration, PID: 1941
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.rajdeeps.upi_integration/com.example.rajdeeps.upi_integration.BankList}: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.util.ArrayList.add(java.lang.Object)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2659)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2724)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1473)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6123)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:757)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.util.ArrayList.add(java.lang.Object)' on a null object reference
at com.example.rajdeeps.upi_integration.BankList.onCreate(BankList.java:30)
at android.app.Activity.performCreate(Activity.java:6672)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1140)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2612)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2724)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1473)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6123)
You forgot to initialize your bankdataList Arraylist just initialize it
ArrayList<String> bankdataList= new ArrayList();;
Sample code
public class BankList extends AppCompatActivity {
ListView bankList;
ArrayList<String> bankdataList;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_bank_list);
bankdataList= new ArrayList();
bankList = (ListView)findViewById(R.id.bankList);
Intent intent = getIntent();
intent.getStringArrayListExtra("BANKLIST");
bankdataList.add(intent.toString());
Toast.makeText(BankList.this,"bank list"+bankdataList,Toast.LENGTH_SHORT).show();
ArrayAdapter<String> itemsAdapter = new ArrayAdapter<String>(getApplicationContext(),R.layout.support_simple_spinner_dropdown_item, bankdataList);
bankList.setAdapter(itemsAdapter);
}
}
You have not initialize bankdataList before add items.
bankdataList = new ArrayList<String>();
bankdataList.add(intent.getStringArrayListExtra("BANKLIST"));
I am begineer in android and having problems
Above error showing while i'm trying to open main activity from login button on login activity.
when pressing the login button suddenly app stops and following error is showing
following error is showing in android monitor
10-20 18:47:23.314 13432-13432/com.antherx.prasenjit.ihdfbachatgat E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.antherx.prasenjit.ihdfbachatgat, PID: 13432
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.antherx.prasenjit.ihdfbachatgat/com.antherx.prasenjit.ihdfbachatgat.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Context.getPackageName()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2460)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2629)
at android.app.ActivityThread.access$800(ActivityThread.java:182)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1474)
at android.os.Handler.dispatchMessage(Handler.java:111)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5706)
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:1033)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:828)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Context.getPackageName()' on a null object reference
at android.content.ContextWrapper.getPackageName(ContextWrapper.java:132)
at android.content.ComponentName.<init>(ComponentName.java:77)
at android.content.Intent.<init>(Intent.java:4358)
at com.antherx.prasenjit.ihdfbachatgat.MainActivity.<init>(MainActivity.java:26)
at java.lang.reflect.Constructor.newInstance(Native Method)
at java.lang.Class.newInstance(Class.java:1606)
at android.app.Instrumentation.newActivity(Instrumentation.java:1071)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2437)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2629)
at android.app.ActivityThread.access$800(ActivityThread.java:182)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1474)
at android.os.Handler.dispatchMessage(Handler.java:111)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5706)
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:1033)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:828)
this is login activity code is their any problem in this code
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
//Please do not
Email = (EditText)findViewById(R.id.email);
Password = (EditText)findViewById(R.id.password);
String wel_on_login = "Welcome On IHDF Bachat Gat app";
Toast welcome = Toast.makeText(getApplicationContext(),wel_on_login,Toast.LENGTH_LONG);
welcome.show();
final Button signIn = (Button)findViewById(R.id.email_sign_in_button);
final Button register = (Button)findViewById(R.id.registration) ;
final Intent main_trans = new Intent(this, MainActivity.class);
final Intent reg_trans = new Intent(this, register.class);
signIn.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View view) {
startActivity(main_trans);
}
});
register.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
startActivity(reg_trans);
}
});
this is main activity codes is their any problem in here
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = (Toolbar)findViewById(R.id.home_toolbar);
setSupportActionBar(toolbar);
newMember = (MenuItem)findViewById(R.id.new_member);
newProfile = (MenuItem)findViewById(R.id.profile) ;
Drawer =(DrawerLayout)findViewById(R.id.drawerLayout);
sToggle = new ActionBarDrawerToggle(this,Drawer,R.string.open,R.string.close);
Grp_ico = (ImageView)findViewById(R.id.grp_ico);
Drawer.addDrawerListener(sToggle);
sToggle.syncState();
getSupportActionBar();
String wel_on_main = "You are successfully on Main Home";
Toast welcome = Toast.makeText(getApplicationContext(),wel_on_main,Toast.LENGTH_LONG);
welcome.show();
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()){
case R.id.new_member :
startActivity(new_new_member);
case R.id.profile :
startActivity(new_new_profile);
case R.id.setting :
startActivity(new_new_setting);
case R.id.meeting :
startActivity(new_new_meeting);
default:
return super.onOptionsItemSelected(item);
}
}
for me i was trying to use the application with no base context accidently.
so i had in my application override:
#Override
protected void attachBaseContext(Context newBase) {
}
//instead of calling its super:
#Override
protected void attachBaseContext(Context newBase) {
super.attachBaseContext(newBase);}
Change your getApplicationContext() from
Toast welcome = Toast.makeText(getApplicationContext(),wel_on_main,Toast.LENGTH_LONG);
to either getBaseContext() or preferably this
I'm using AppCompat library (com.android.support:appcompat-v7:22.1.0) in my app. I created an ActionBar in a fragment. When I click in a menu item it shows an Alert Dialog. Here is my code:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.action_new:
showFilterDialog();
return true;
case R.id.action_send:
new sendInventoryTask().execute();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
And my showInventoryDialog method:
private void showFilterInventoryDialog() {
AlertDialog.Builder alert = new AlertDialog.Builder(getActivity());
LayoutInflater inflater= getActivity().getLayoutInflater();
View v = inflater.inflate(R.layout.dialog_filter_inventory,null);
alert.setView(v);
alert.setTitle(getResources().getString(R.string.filters));
alert.setPositiveButton(getResources().getString(R.string.filter), new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// TODO
}
});
alert.setNegativeButton(getResources().getString(R.string.cancel), null);
alert.show();
}
Everything works fine, but when I click on menu item, the logcat shows me an error:
I/AppCompatDelegate﹕ The Activity's LayoutInflater already has a Factory installed so we can not install AppCompat's
How to solve this?
From what I can tell, appcompat 23.1.1 calls installViewFactory() in AppCompatDelegateImplV7 every time you show a dialog created by AlertDialog.Builder.
Call Stack:
at
android.support.v7.app.AppCompatDelegateImplV7.installViewFactory(AppCompatDelegateImplV7.java:970)
at
android.support.v7.app.AppCompatDialog.onCreate(AppCompatDialog.java:58)
at android.support.v7.app.AlertDialog.onCreate(AlertDialog.java:239)
at android.app.Dialog.dispatchOnCreate(Dialog.java:361) at
android.app.Dialog.show(Dialog.java:262) at
android.support.v7.app.AlertDialog$Builder.show(AlertDialog.java:902)
#Override
public void installViewFactory() {
LayoutInflater layoutInflater = LayoutInflater.from(mContext);
if (layoutInflater.getFactory() == null) {
LayoutInflaterCompat.setFactory(layoutInflater, this);
} else {
Log.i(TAG, "The Activity's LayoutInflater already has a Factory installed"
+ " so we can not install AppCompat's");
}
}
As you can see, once the factory is already set it just logs the informational message. It seems pretty safe to ignore, but it can get annoying when it fills up your logs.
You are required to use the themed context in this case, i.e. instead of
new AlertDialog.Builder(getActivity());
you have to do
new AlertDialog.Builder(getSupportActionBar().getThemedContext());
Besides, you also need to follow the parent theme and windowActionBar tip given here - support.v7.app.AlertDialog throws NullPointerException on dismiss
Solution - The error "The Activity's LayoutInflater already has a Factory installed so we can not install AppCompat's" could also be due to incorrect code. In my case the code was calling "super.onCreate(savedInstanceState)" twice (code error) and once the duplicate call was removed the error was resolved.
This is my error log:
02-29 04:54:40.706 4417-4417/com.projects.ajay.example2 I/AppCompatDelegate: The Activity's LayoutInflater already has a Factory installed so we can not install AppCompat's
02-29 04:54:40.709 4417-4417/? D/AndroidRuntime: Shutting down VM
02-29 04:54:40.715 4417-4417/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.projects.ajay.example2, PID: 4417
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.projects.ajay.example2/com.projects.ajay.example2.MainActivity}: java.lang.IllegalStateException: Already attached
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2331)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2393)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1309)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5351)
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:908)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:703)
Caused by: java.lang.IllegalStateException: Already attached
at android.support.v4.app.FragmentManagerImpl.attachController(FragmentManager.java:2025)
at android.support.v4.app.FragmentController.attachHost(FragmentController.java:95)
at android.support.v4.app.FragmentActivity.onCreate(FragmentActivity.java:276)
at android.support.v7.app.AppCompatActivity.onCreate(AppCompatActivity.java:61)
at com.projects.ajay.example2.MainActivity.onCreate(MainActivity.java:24)
at android.app.Activity.performCreate(Activity.java:6020)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2284)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2393)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1309)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5351)
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:908)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:703)
02-29 04:54:40.727 4417-4417/? I/Process: Sending signal. PID: 4417 SIG: 9
Change
inflater.inflate(R.layout.dialog_filter_inventory,null);
to
inflater.inflate(R.layout.dialog_filter_inventory,null,false);
In case of inflating a Dialog, we don't want it to attach to the specified ViewGroup (== null).
This could help.
Second try
The info-log is thrown by the following code in AppCompatDelegateImplV7:
#Override
public void installViewFactory() {
LayoutInflater layoutInflater = LayoutInflater.from(mContext);
if (layoutInflater.getFactory() == null) {
LayoutInflaterCompat.setFactory(layoutInflater, this);
} else {
Log.i(TAG, "The Activity's LayoutInflater already has a Factory installed"
+ " so we can not install AppCompat's");
}
}
Could it be you installed a Factory somewere earlier in your code?
The code below is an onClick() method for a Button.
It keeps giving me an error.
If I delete the startActivity(), then it works fine.
How can I use a POST method when I click the Button, and also move to another Activity?
public void onClick(View view) {
switch (view.getId()) {
case R.id.register:
if (!validate())
Toast.makeText(getBaseContext(), "Enter user information!", Toast.LENGTH_LONG).show();
// call AsynTask to perform network operation on separate thread
new HttpAsyncTask().execute(herokuServer);
startActivity(new Intent(this, FoodPreference.class));
break;
}
startActivity(new Intent(Register1Activity.this, FoodPreference.class));
}
THE ERROR:
04-27 19:41:26.066 1974-1974/com.opshun_test.opshun_test E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.opshun_test.opshun_test, PID: 1974
android.content.ActivityNotFoundException: Unable to find explicit activity class {com.opshun_test.opshun_test/com.opshun_test.opshun_test.FoodPreference}; have you declared this activity in your AndroidManifest.xml?
at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1761)
at android.app.Instrumentation.execStartActivity(Instrumentation.java:1485)
at android.app.Activity.startActivityForResult(Activity.java:3736)
at android.app.Activity.startActivityForResult(Activity.java:3697)
at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:817)
at android.app.Activity.startActivity(Activity.java:4007)
at android.app.Activity.startActivity(Activity.java:3975)
at com.opshun_test.opshun_test.Register1Activity.onClick(Register1Activity.java:173)
at android.view.View.performClick(View.java:4756)
at android.view.View$PerformClick.run(View.java:19749)
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:5221)
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:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
04-27 19:41:26.106 1974-2307/com.opshun_test.opshun_test I/output﹕ org.apache.http.client.methods.HttpPost#5b541ce
If you are using anonymous inner class for the click listener , use className.this instead of this
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this,Activity2.class));
}
});
if you are handling the click in the activity, make sure the Activity is defined in the manifest file. Provide the stack trace for a complete solution.
You didn't declare you activity in you manifest. Add the following lines to you manifest file.
<activity
android:name=".FoodPreference"
android:label="#string/title_activity_food_preference" >
</activity>
Moreover, the more safe way to start an intent is always checking the intent is resolvable or not, like the following code.
Intent intent = new Intent(this, FoodPreference.class);
if (intent.resolveActivity(getPackageManager()) != null) {
startActivity(intent);
} else {
// TODO: activity not found
}
Register your Activity FoodPreference in Manifest
and you are calling
new HttpAsyncTask().execute(herokuServer);
So better Intent to FoodPreference.class in onPostExecute()