Calling startActivity function on menu crashes the app












-1















I'm setting up a project that will receive details from the user, insert it to the SQLite DB and will show the information that was given by the user on the Second Activity's ListView.



I tried many tutorials and suggesting on the internet, tried ActivityForResult, tried not using Switch but still, nothing seems to work.



public boolean onOptionsItemSelected(MenuItem item) {


switch (item.getItemId()) {
case R.id.SecondActivity:
Intent i = new Intent(this, SecondActivity.class);
startActivity(i);
break;
case R.id.credits:
Toast.makeText(MainActivity.this,
"some text ;)", Toast.LENGTH_LONG).show();
break;
}

return true;

}


I expect it to run the second activity. But it keeps on crashing when I try switching between activities










share|improve this question

























  • Please add the logcat of this crash.

    – Geno Chen
    Jan 1 at 9:58











  • Did you add SecondActivity in your AndroidManifest.xml?

    – Geno Chen
    Jan 1 at 9:58











  • Sorry but I don't know how to do it.

    – asslox
    Jan 1 at 9:59











  • Yes it is on the .xml file

    – asslox
    Jan 1 at 10:00













  • In src/main/AndroidManifest.xml, did you add something like, for example, starting with <activity android:name=".SecondActivity"?

    – Geno Chen
    Jan 1 at 10:02
















-1















I'm setting up a project that will receive details from the user, insert it to the SQLite DB and will show the information that was given by the user on the Second Activity's ListView.



I tried many tutorials and suggesting on the internet, tried ActivityForResult, tried not using Switch but still, nothing seems to work.



public boolean onOptionsItemSelected(MenuItem item) {


switch (item.getItemId()) {
case R.id.SecondActivity:
Intent i = new Intent(this, SecondActivity.class);
startActivity(i);
break;
case R.id.credits:
Toast.makeText(MainActivity.this,
"some text ;)", Toast.LENGTH_LONG).show();
break;
}

return true;

}


I expect it to run the second activity. But it keeps on crashing when I try switching between activities










share|improve this question

























  • Please add the logcat of this crash.

    – Geno Chen
    Jan 1 at 9:58











  • Did you add SecondActivity in your AndroidManifest.xml?

    – Geno Chen
    Jan 1 at 9:58











  • Sorry but I don't know how to do it.

    – asslox
    Jan 1 at 9:59











  • Yes it is on the .xml file

    – asslox
    Jan 1 at 10:00













  • In src/main/AndroidManifest.xml, did you add something like, for example, starting with <activity android:name=".SecondActivity"?

    – Geno Chen
    Jan 1 at 10:02














-1












-1








-1








I'm setting up a project that will receive details from the user, insert it to the SQLite DB and will show the information that was given by the user on the Second Activity's ListView.



I tried many tutorials and suggesting on the internet, tried ActivityForResult, tried not using Switch but still, nothing seems to work.



public boolean onOptionsItemSelected(MenuItem item) {


switch (item.getItemId()) {
case R.id.SecondActivity:
Intent i = new Intent(this, SecondActivity.class);
startActivity(i);
break;
case R.id.credits:
Toast.makeText(MainActivity.this,
"some text ;)", Toast.LENGTH_LONG).show();
break;
}

return true;

}


I expect it to run the second activity. But it keeps on crashing when I try switching between activities










share|improve this question
















I'm setting up a project that will receive details from the user, insert it to the SQLite DB and will show the information that was given by the user on the Second Activity's ListView.



I tried many tutorials and suggesting on the internet, tried ActivityForResult, tried not using Switch but still, nothing seems to work.



public boolean onOptionsItemSelected(MenuItem item) {


switch (item.getItemId()) {
case R.id.SecondActivity:
Intent i = new Intent(this, SecondActivity.class);
startActivity(i);
break;
case R.id.credits:
Toast.makeText(MainActivity.this,
"some text ;)", Toast.LENGTH_LONG).show();
break;
}

return true;

}


I expect it to run the second activity. But it keeps on crashing when I try switching between activities







sqlite android-studio android-activity






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 1 at 9:57







asslox

















asked Jan 1 at 9:49









assloxasslox

164




164













  • Please add the logcat of this crash.

    – Geno Chen
    Jan 1 at 9:58











  • Did you add SecondActivity in your AndroidManifest.xml?

    – Geno Chen
    Jan 1 at 9:58











  • Sorry but I don't know how to do it.

    – asslox
    Jan 1 at 9:59











  • Yes it is on the .xml file

    – asslox
    Jan 1 at 10:00













  • In src/main/AndroidManifest.xml, did you add something like, for example, starting with <activity android:name=".SecondActivity"?

    – Geno Chen
    Jan 1 at 10:02



















  • Please add the logcat of this crash.

    – Geno Chen
    Jan 1 at 9:58











  • Did you add SecondActivity in your AndroidManifest.xml?

    – Geno Chen
    Jan 1 at 9:58











  • Sorry but I don't know how to do it.

    – asslox
    Jan 1 at 9:59











  • Yes it is on the .xml file

    – asslox
    Jan 1 at 10:00













  • In src/main/AndroidManifest.xml, did you add something like, for example, starting with <activity android:name=".SecondActivity"?

    – Geno Chen
    Jan 1 at 10:02

















Please add the logcat of this crash.

– Geno Chen
Jan 1 at 9:58





Please add the logcat of this crash.

– Geno Chen
Jan 1 at 9:58













Did you add SecondActivity in your AndroidManifest.xml?

– Geno Chen
Jan 1 at 9:58





Did you add SecondActivity in your AndroidManifest.xml?

– Geno Chen
Jan 1 at 9:58













Sorry but I don't know how to do it.

– asslox
Jan 1 at 9:59





Sorry but I don't know how to do it.

– asslox
Jan 1 at 9:59













Yes it is on the .xml file

– asslox
Jan 1 at 10:00







Yes it is on the .xml file

– asslox
Jan 1 at 10:00















In src/main/AndroidManifest.xml, did you add something like, for example, starting with <activity android:name=".SecondActivity"?

– Geno Chen
Jan 1 at 10:02





In src/main/AndroidManifest.xml, did you add something like, for example, starting with <activity android:name=".SecondActivity"?

– Geno Chen
Jan 1 at 10:02












2 Answers
2






active

oldest

votes


















0














 case R.id.SecondActivity:
Intent i = new Intent(this, SecondActivity.class);
startActivity(i);
break;


you might have problem in Second ativity check if it is exist or not in AndroidManifest.xml



or in intent calling if it is not fragment use Intent i=new Intent(getApplicationContext(),SecondActivity.class);






share|improve this answer
























  • The comment said that the author have already add it.

    – Geno Chen
    Jan 1 at 10:11











  • Tried it. Did not work.

    – asslox
    Jan 1 at 10:34



















-1














In your SecondActivity, line 24:



Cursor getUserData=db.query(HelperDB.TABLE_USER,null,null,null,null,null,null);


However, at this time (during initialization of SecondActivity), the variable db is declared (line 21) but not assigned (it is assigned too late, in onCreate() in line 33). Note that <init> of a class is far earlier than calling this onCreate().



So move these two Cursor after line 33 (and, maybe, between line 33 and 34) will solve this issue.






share|improve this answer
























  • Unfortunately, that still doesn't solve my issue.

    – asslox
    Jan 1 at 10:53











  • @aslox What do you mean by "doesn't solve issue"? Do the stacktrace (line 35 to 59 in run data) still the same?

    – Geno Chen
    Jan 1 at 10:56











  • I guess the new question is generated from the too-early db.close().

    – Geno Chen
    Jan 1 at 11:00











  • I tried moving lines 24,25 to 34,35 (inside the onCreate function) that didn't work as well. watch the new run data: pastebin.com/M1ft5uSR

    – asslox
    Jan 1 at 11:24













  • @asslox Yes, read line 110 and 115 in the new run data, you should remove the db.close() line, or, put it in the very bottom, for example at the end of onCreate() block.

    – Geno Chen
    Jan 1 at 11:27











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53994461%2fcalling-startactivity-function-on-menu-crashes-the-app%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














 case R.id.SecondActivity:
Intent i = new Intent(this, SecondActivity.class);
startActivity(i);
break;


you might have problem in Second ativity check if it is exist or not in AndroidManifest.xml



or in intent calling if it is not fragment use Intent i=new Intent(getApplicationContext(),SecondActivity.class);






share|improve this answer
























  • The comment said that the author have already add it.

    – Geno Chen
    Jan 1 at 10:11











  • Tried it. Did not work.

    – asslox
    Jan 1 at 10:34
















0














 case R.id.SecondActivity:
Intent i = new Intent(this, SecondActivity.class);
startActivity(i);
break;


you might have problem in Second ativity check if it is exist or not in AndroidManifest.xml



or in intent calling if it is not fragment use Intent i=new Intent(getApplicationContext(),SecondActivity.class);






share|improve this answer
























  • The comment said that the author have already add it.

    – Geno Chen
    Jan 1 at 10:11











  • Tried it. Did not work.

    – asslox
    Jan 1 at 10:34














0












0








0







 case R.id.SecondActivity:
Intent i = new Intent(this, SecondActivity.class);
startActivity(i);
break;


you might have problem in Second ativity check if it is exist or not in AndroidManifest.xml



or in intent calling if it is not fragment use Intent i=new Intent(getApplicationContext(),SecondActivity.class);






share|improve this answer













 case R.id.SecondActivity:
Intent i = new Intent(this, SecondActivity.class);
startActivity(i);
break;


you might have problem in Second ativity check if it is exist or not in AndroidManifest.xml



or in intent calling if it is not fragment use Intent i=new Intent(getApplicationContext(),SecondActivity.class);







share|improve this answer












share|improve this answer



share|improve this answer










answered Jan 1 at 10:10









sagarsagar

4517




4517













  • The comment said that the author have already add it.

    – Geno Chen
    Jan 1 at 10:11











  • Tried it. Did not work.

    – asslox
    Jan 1 at 10:34



















  • The comment said that the author have already add it.

    – Geno Chen
    Jan 1 at 10:11











  • Tried it. Did not work.

    – asslox
    Jan 1 at 10:34

















The comment said that the author have already add it.

– Geno Chen
Jan 1 at 10:11





The comment said that the author have already add it.

– Geno Chen
Jan 1 at 10:11













Tried it. Did not work.

– asslox
Jan 1 at 10:34





Tried it. Did not work.

– asslox
Jan 1 at 10:34













-1














In your SecondActivity, line 24:



Cursor getUserData=db.query(HelperDB.TABLE_USER,null,null,null,null,null,null);


However, at this time (during initialization of SecondActivity), the variable db is declared (line 21) but not assigned (it is assigned too late, in onCreate() in line 33). Note that <init> of a class is far earlier than calling this onCreate().



So move these two Cursor after line 33 (and, maybe, between line 33 and 34) will solve this issue.






share|improve this answer
























  • Unfortunately, that still doesn't solve my issue.

    – asslox
    Jan 1 at 10:53











  • @aslox What do you mean by "doesn't solve issue"? Do the stacktrace (line 35 to 59 in run data) still the same?

    – Geno Chen
    Jan 1 at 10:56











  • I guess the new question is generated from the too-early db.close().

    – Geno Chen
    Jan 1 at 11:00











  • I tried moving lines 24,25 to 34,35 (inside the onCreate function) that didn't work as well. watch the new run data: pastebin.com/M1ft5uSR

    – asslox
    Jan 1 at 11:24













  • @asslox Yes, read line 110 and 115 in the new run data, you should remove the db.close() line, or, put it in the very bottom, for example at the end of onCreate() block.

    – Geno Chen
    Jan 1 at 11:27
















-1














In your SecondActivity, line 24:



Cursor getUserData=db.query(HelperDB.TABLE_USER,null,null,null,null,null,null);


However, at this time (during initialization of SecondActivity), the variable db is declared (line 21) but not assigned (it is assigned too late, in onCreate() in line 33). Note that <init> of a class is far earlier than calling this onCreate().



So move these two Cursor after line 33 (and, maybe, between line 33 and 34) will solve this issue.






share|improve this answer
























  • Unfortunately, that still doesn't solve my issue.

    – asslox
    Jan 1 at 10:53











  • @aslox What do you mean by "doesn't solve issue"? Do the stacktrace (line 35 to 59 in run data) still the same?

    – Geno Chen
    Jan 1 at 10:56











  • I guess the new question is generated from the too-early db.close().

    – Geno Chen
    Jan 1 at 11:00











  • I tried moving lines 24,25 to 34,35 (inside the onCreate function) that didn't work as well. watch the new run data: pastebin.com/M1ft5uSR

    – asslox
    Jan 1 at 11:24













  • @asslox Yes, read line 110 and 115 in the new run data, you should remove the db.close() line, or, put it in the very bottom, for example at the end of onCreate() block.

    – Geno Chen
    Jan 1 at 11:27














-1












-1








-1







In your SecondActivity, line 24:



Cursor getUserData=db.query(HelperDB.TABLE_USER,null,null,null,null,null,null);


However, at this time (during initialization of SecondActivity), the variable db is declared (line 21) but not assigned (it is assigned too late, in onCreate() in line 33). Note that <init> of a class is far earlier than calling this onCreate().



So move these two Cursor after line 33 (and, maybe, between line 33 and 34) will solve this issue.






share|improve this answer













In your SecondActivity, line 24:



Cursor getUserData=db.query(HelperDB.TABLE_USER,null,null,null,null,null,null);


However, at this time (during initialization of SecondActivity), the variable db is declared (line 21) but not assigned (it is assigned too late, in onCreate() in line 33). Note that <init> of a class is far earlier than calling this onCreate().



So move these two Cursor after line 33 (and, maybe, between line 33 and 34) will solve this issue.







share|improve this answer












share|improve this answer



share|improve this answer










answered Jan 1 at 10:43









Geno ChenGeno Chen

2,3646925




2,3646925













  • Unfortunately, that still doesn't solve my issue.

    – asslox
    Jan 1 at 10:53











  • @aslox What do you mean by "doesn't solve issue"? Do the stacktrace (line 35 to 59 in run data) still the same?

    – Geno Chen
    Jan 1 at 10:56











  • I guess the new question is generated from the too-early db.close().

    – Geno Chen
    Jan 1 at 11:00











  • I tried moving lines 24,25 to 34,35 (inside the onCreate function) that didn't work as well. watch the new run data: pastebin.com/M1ft5uSR

    – asslox
    Jan 1 at 11:24













  • @asslox Yes, read line 110 and 115 in the new run data, you should remove the db.close() line, or, put it in the very bottom, for example at the end of onCreate() block.

    – Geno Chen
    Jan 1 at 11:27



















  • Unfortunately, that still doesn't solve my issue.

    – asslox
    Jan 1 at 10:53











  • @aslox What do you mean by "doesn't solve issue"? Do the stacktrace (line 35 to 59 in run data) still the same?

    – Geno Chen
    Jan 1 at 10:56











  • I guess the new question is generated from the too-early db.close().

    – Geno Chen
    Jan 1 at 11:00











  • I tried moving lines 24,25 to 34,35 (inside the onCreate function) that didn't work as well. watch the new run data: pastebin.com/M1ft5uSR

    – asslox
    Jan 1 at 11:24













  • @asslox Yes, read line 110 and 115 in the new run data, you should remove the db.close() line, or, put it in the very bottom, for example at the end of onCreate() block.

    – Geno Chen
    Jan 1 at 11:27

















Unfortunately, that still doesn't solve my issue.

– asslox
Jan 1 at 10:53





Unfortunately, that still doesn't solve my issue.

– asslox
Jan 1 at 10:53













@aslox What do you mean by "doesn't solve issue"? Do the stacktrace (line 35 to 59 in run data) still the same?

– Geno Chen
Jan 1 at 10:56





@aslox What do you mean by "doesn't solve issue"? Do the stacktrace (line 35 to 59 in run data) still the same?

– Geno Chen
Jan 1 at 10:56













I guess the new question is generated from the too-early db.close().

– Geno Chen
Jan 1 at 11:00





I guess the new question is generated from the too-early db.close().

– Geno Chen
Jan 1 at 11:00













I tried moving lines 24,25 to 34,35 (inside the onCreate function) that didn't work as well. watch the new run data: pastebin.com/M1ft5uSR

– asslox
Jan 1 at 11:24







I tried moving lines 24,25 to 34,35 (inside the onCreate function) that didn't work as well. watch the new run data: pastebin.com/M1ft5uSR

– asslox
Jan 1 at 11:24















@asslox Yes, read line 110 and 115 in the new run data, you should remove the db.close() line, or, put it in the very bottom, for example at the end of onCreate() block.

– Geno Chen
Jan 1 at 11:27





@asslox Yes, read line 110 and 115 in the new run data, you should remove the db.close() line, or, put it in the very bottom, for example at the end of onCreate() block.

– Geno Chen
Jan 1 at 11:27


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53994461%2fcalling-startactivity-function-on-menu-crashes-the-app%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

MongoDB - Not Authorized To Execute Command

How to fix TextFormField cause rebuild widget in Flutter

Npm cannot find a required file even through it is in the searched directory