I am getting an exception at second startActivity(i) statement. My first startActivity(i) statement works fine.
public void onBClick(View v){
EditText a = (EditText) findViewById(R.id.TFUsername);
String str = a.getText().toString();
if(v.getId() == R.id.BLogin){
Intent i = new Intent(MainActivity.this, new_activ_java.class);
i.putExtra("username",str+',');
startActivity(i);
} else if(v.getId() == R.id.BforSignUp){
Intent i = new Intent(MainActivity.this, Signup.class);
startActivity(i); // I am getting an exception here
}
}
Please make sure Signup is an Activity declared in the Android Manifest. As a convention, append Activity at the end of the class name to indicate that it is indeed an Activity, for instance, SignUpActivity.
Make sure that the new activity you are trying to start is listed in Android Manifest.xml file.
Related
Immediate Disclaimer: I am not a programmer, I've been dumped with this as part of a group project, so apologies if the code is shabby.
I've got a main activity as the start-up page with several buttons that should open different activities, three of these buttons work perfectly with opening up their specific activities (Main2Activity, MOT and Garage), but the others, with the same structure being used, just close the app instead of opening the next screen.
public void defineButtons() {
findViewById(R.id.mot_button).setOnClickListener(buttonClickListener);
findViewById(R.id.enter_button).setOnClickListener(buttonClickListener);
findViewById(R.id.garage_button).setOnClickListener(buttonClickListener);
findViewById(R.id.profile_button).setOnClickListener(buttonClickListener);
findViewById(R.id.contact_button).setOnClickListener(buttonClickListener);
findViewById(R.id.settings_button).setOnClickListener(buttonClickListener);
}
private View.OnClickListener buttonClickListener = new View.OnClickListener() {
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.mot_button:
Intent intent = new Intent(MainActivity.this, MOT.class);
startActivity(intent);
break;
case R.id.garage_button:
Intent x = new Intent(MainActivity.this, garage.class);
startActivity(x);
break;
case R.id.profile_button:
Intent a = new Intent(MainActivity.this, Profile.class);
startActivity(a);
break;
case R.id.contact_button:
Intent b = new Intent(MainActivity.this, Contact.class);
startActivity(b);
break;
case R.id.settings_button:
Intent c = new Intent(MainActivity.this, Activity_Settings.class);
startActivity(c);
break;
case R.id.enter_button:
reg_input=findViewById(R.id.reg_input);
Intent i = new Intent(MainActivity.this, Main2Activity.class);
regNo = reg_input.getText().toString();
i.putExtra("Value", regNo);
startActivity(i);
finish();
break;
This is the relevant code for it, let me know if you want to see anything else.
I'm probably being really stupid, but I'd appreciate the help.
Your app is crashing, probably because the activities your want to start aren't in the manifest. Check your manifest and make sure that all your activities are declared there.
I am trying this intent so I can navigate from one activity to another activity but I'm getting this error `
final Button btnAdd = findViewById(R.id.addEmp);
btnAdd.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
if(v.getId() == R.id.addEmp){
Intent intent = new
Intent(getCallingActivity(),AddEmployee.class);
startActivity(intent);
}
}
});`
there is a red line under
(getCallingActivity(),AddEmployee.class);
there error says
cannot resolve constructor
Is anything wrong with this
(getCallingActivity(),AddEmployee.class);
statement?
You need to change your line from
Intent intent = new Intent(getCallingActivity(),AddEmployee.class);
to this:
Intent intent = new Intent(YourActivityName.this,AddEmployee.class);
OR
Intent intent = new Intent(getApplicationContext(),AddEmployee.class);
Edit
Whats wrong with getCallingActivty()?
getCallingActivity() returns ComponentName while intent constructor requires Context as a first argument.
Hope this will work
The problem I am having is that it prints out Null on the second activity and not the actual username that is entered. Is the data being passed to the second activity correctly? Does the second activity need more code? Sorry but not the best at programming.
I have this code in my main class
if (username.getText().toString().equals("batman") &&
password.getText().toString().equals("Joker")) {
Toast.makeText(MainActivity.this, "Username and
password is correct", Toast.LENGTH_SHORT).show();
Intent intent = new Intent("com.example.*******.loginpage.User");
intent.putExtra("username",String.valueOf(username));
startActivity(new Intent(MainActivity.this, User.class));
This is the code inside my second class.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_user);
Intent intent = getIntent();
String username = getIntent().getStringExtra("username");
TextView textView = (TextView) findViewById(R.id.textView4);
textView.setText("Welcome" + " " + username );
The problem is your intent in your first class
Intent intent = new Intent("com.example.*******.loginpage.User"); <-- have created an intent
intent.putExtra("username",String.valueOf(username));
startActivity(new Intent(MainActivity.this, User.class)); <-- but using new Intent
You have created an intent but you passing new intent. Use your created Intent instead of passing new Intent.
Intent intent = new Intent(MainActivity.this, User.class);
intent.putExtra("username",String.valueOf(username));
startActivity(intent);
EDIT
Instead using String.valueOf(username) you must use username.getText(), because String.valueOf(username) is method to translate your object to String.
Intent intent = new Intent(MainActivity.this, User.class);
intent.putExtra("username",username.getText());
startActivity(intent);
Two problems here.
First one is that you have to pass the intent where you put your extra instead of creating new one to startActivity, like
Intent intent = new Intent(MainActivity.this, User.class);
intent.putExtra("username",username.getText().toString());
startActivity(intent);
Second problem is that username looks like editText, String.valueOf won't pass actual value, use username.getText().toString() like i mentioned in code.
In my app, there are so many activities which are referred to as levels. And one activity is Reward activity. when i win level-1, reward activity opens. Now i want to replay the level-1. For this i have used getExtra(). My app crashes when i click the replay button.
Houselevel1.java
public void getReward(){
if(count == 3) {
Intent intent = new Intent("com.creatives.arfa.revealthesecretsgame.Reward");
intent.putExtra("activity", "level1");
startActivity(intent);
}
}
HouseLevel2.java
public void getReward(){
if(count == 3) {
Intent intent = new Intent("com.creatives.arfa.revealthesecretsgame.Reward");
intent.putExtra("activity", "level2");
startActivity(intent);
}
}
Reward.java
public void replayLevel() {
replay = (ImageButton) findViewById(R.id.replay);
Intent intent= getIntent();
activity = intent.getStringExtra("activity");
replay.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View paramView) {
if(activity.equals("level2")){
Intent intent = new Intent("com.creatives.arfa.revealthesecretsgame.HouseLevel2");
startActivity(intent);
}
if(activity.equals("level1")){
Intent intent = new Intent("com.creatives.arfa.revealthesecretsgame.Houselevel1");
startActivity(intent);
}
}
});
}
With the java code you've posted, in the Reward.java file, you're trying to create another Intent Object with the same name as the one declared in the scope right above it. Because of this, the build will never be successful.
Also, when you declare intents, you MUST pass on the activity_name.class file.
Something you can try:
1) HouseLevel1.java
public void getReward(){
if(count == 3) {
Intent intent = new Intent(getApplicationContext(), com.creatives.arfa.revealthesecretsgame.Reward.class);
intent.putExtra("activity", "level1");
startActivity(intent);
}
}
2) HouseLevel2.java
public void getReward(){
if(count == 3) {
Intent intent = new Intent(getApplicationContext(), com.creatives.arfa.revealthesecretsgame.Reward.class);
intent.putExtra("activity", "level2");
startActivity(intent);
}
}
3) Reward.java
public void replayLevel() {
replay = (ImageButton) findViewById(R.id.replay);
Intent intent= getIntent();
activity = intent.getStringExtra("activity");
replay.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View paramView) {
if(activity.equals("level2")){
Intent intent = new Intent(getApplicationContext(), com.creatives.arfa.revealthesecretsgame.HouseLevel2.class);
startActivity(intent);
}
else if(activity.equals("level1")){
Intent intent = new Intent(getApplicationContext(), com.creatives.arfa.revealthesecretsgame.Houselevel1.class);
startActivity(intent);
}
}
});
}
Also, if you're simply using the Reward.java file to get the previous intent's data, perform some calculation, and send some data back to the calling, or parent activity, then you can simply use the startActivityForResult() method, which takes care what what you're trying to do manually.
Here's a small article that might be able to help you with the problem
http://www.vogella.com/tutorials/AndroidIntent/article.html#retrieving-result-data-from-a-sub-activity
If all you want is go from Activity 1 or to 2 to a Reward activity grab something and send that something back to either activity.
What you do is startActivityForResult You pass an Id (constant number) do what you do on the Reward activty, pack what you need to return in a Bundle, and set ActivtyResult to OK and close your activity.
Your app will go back to the Activity1 or 2 whoever call it. On those activties you override the method onActivityResult There you check if the id on which the result is coming from is the Id you sent on the startActivityForResult and if the status is OK.
Then you have whatever was set on the Reward activity. The Reward activity don't need to know from where it came from if only will grab some data. So you can later have an Activity3 that calls the Reward activity and you do not need to modify the Reward activity.
It is explain here check the accepted answer.
How to manage `startActivityForResult` on Android?
In my Android app, I have a button that when clicked, launches the external application of my choice to play a video (I gather that this is called an "implicit intent"). Here is the relevant Java code from my onCreate method.
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener
(
new Button.OnClickListener()
{
public void onClick(View v)
{
Intent i = new Intent(Intent.ACTION_VIEW);
i.setDataAndType(Uri.parse("https://youtu.be/jxoG_Y6dvU8"), "video/*");
startActivity(i);
}
}
);
I expected this to work, since I've followed tutorials and the Android developers documentation pretty closely, but when I test my app in the AVD, instead of prompting a menu of external applications where I can view my video, the app crashes.
What is causing my app to crash?
Change your onClick method to below code. You should give the option to choose the external player.
#Override
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.parse("https://youtu.be/jxoG_Y6dvU8"), "video/*");
startActivity(Intent.createChooser(intent, "Complete action using"));
}
Change your code to add this check:
Intent i = new Intent(Intent.ACTION_VIEW);
i.setDataAndType(Uri.parse("https://youtu.be/jxoG_Y6dvU8"), "video/*");
// Check there is an activity that can handle this intent
if (i.resolveActivity(getPackageManager()) == null) {
// TODO No activity available. Do something else.
} else {
startActivity(i);
}