I need help getting an intent to open another activity - java

I need to be able to touch a TextView and have it open up another page, however, when I run the app and touch the TextView the app crashes.
Here's the code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding= "10dp"
android:background="#drawable/background"
android:orientation="vertical"
tools:context="com.example.android.practiceapp.MainActivity">
<TextView
android:id="#+id/numbers"
style="#style/CategoryStyle"
android:layout_marginBottom="16dp"
android:background="#drawable/numbers"
android:text="#string/category_numbers"
android:onClick="openNumbersList"/>
Here's the java code:
package com.example.android.practiceapp;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void openNumbersList(View view) {
Intent i = new Intent(this, Numbers.class);
startActivity(i);
}
}

Make sure you have the Numbers activity exists and is in your Android.manifest.
All activities must be represented by elements in the manifest file. Any that are not declared there will not be seen by the system and will never be run.

First, you are not initialized your TextView
textView = (TextView)findViewById(R.id.numbers)
Second, as others suggested, add Numbers activity in your Android.manifest

You did not add the clickable attribute, without it, the click handler isn't called. So, add it...
...
<TextView
android:id="#+id/numbers"
style="#style/CategoryStyle"
android:layout_marginBottom="16dp"
android:background="#drawable/numbers"
android:text="#string/category_numbers"
android:onClick="openNumbersList"
android:clickable="true"/>
...

thanks for the responses, i actually ran the debugger and it turns out i had to change my java code in the numbers file from "extends AppCompatActivity" to "extends Activity" like the MainActivity java file

Try inserting
MainActivity.this
instead of
this
like so
Intent i = new Intent(MainActivity.this, Numbers.class);

Related

Getting white screen when trying to open the app. App is getting launched on my phone without errors

When I am trying to run my app on my phone it is getting successfully installed in my phone but I am getting white screen instead of my splash page . Attaching my xml code and java code for your reference . Your help is highly appreciated !!
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/background" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="100dp"
android:fontFamily="sans-serif-black"
android:text="MY APP"
android:textColor="#color/Black"
android:textSize="50sp" />
</Relativelayout>
activity_main.java
package com.example.kriova;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent intent = new Intent(getApplicationContext(),
MainActivity.class);
startActivity(intent);
finish();
}
}
The finish() method is called and the activity destroys and returns to the home screen.
Your code should be like this
package com.example.kriova;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}}
remove the last line which is :-
finish();

Android Studio: cannot resolve symbol R.id.textView

I'm currently following the Android Studio "Build Your First App" tutorial (https://developer.android.com/training/basics/firstapp/starting-activity) and I can't seem to get the DisplayMessageActivity working. The variable "R.id.textView" doesn't seem to exist and I can't see any differences between the tutorial and my own code. I know this is just me being stupid somewhere but I can't pinpoint it.
Here's my code for DisplayMessageActivity.java:
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
public class DisplayMessageActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_message);
// Get the Intent that started this activity and extract the string
Intent intent = getIntent();
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
// Capture the layout's TextView and set the string as its text
TextView textView = findViewById(R.id.textView); <-- Cannot resolve symbol 'textView'
textView.setText(message);
}
}
And MainActivity.java, which is trying to run it:
package com.example.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity {
public static final String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
/** Called when the user taps the Send button */
public void sendMessage(View view) {
Intent intent = new Intent(this, DisplayMessageActivity.class);
EditText editText = (EditText) findViewById(R.id.editTextTextPersonName);
String message = editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
}
}
When I try to run the app on the virtual device it doesn't crash, but the button which calls the DisplayMessage activity does nothing.
EDIT: As requested, the xml code for both activities:
activity_display_message.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".DisplayMessageActivity">
</androidx.constraintlayout.widget.ConstraintLayout>
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<EditText
android:id="#+id/editTextTextPersonName"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:ems="10"
android:hint="#string/edit_message"
android:inputType="textPersonName"
app:layout_constraintEnd_toStartOf="#+id/button2"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginEnd="16dp"
android:onClick="sendMessage"
android:text="#string/button_send"
app:layout_constraintBaseline_toBaselineOf="#+id/editTextTextPersonName"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="#+id/editTextTextPersonName" />
</androidx.constraintlayout.widget.ConstraintLayout>
I read the tutorial and I think they missed this part of code:
android:onClick="sendMessage"
so, the code should be like this:
<Button
android:id="#+id/button"
android:onClick="sendMessage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:layout_marginStart="16dp"
android:text="#string/button_send"
app:layout_constraintBaseline_toBaselineOf="#+id/editText"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="#+id/editText" />
and I think that's it, let me know if it helps
After looking more closely at #Vishal Naikawadi's comment, I realised what the problem was. R.id.textView is not some sort of variable, placeholder, or constant as I had assumed, it is literally getting the object with id "textView" (or so I think). I had accidentally changed the id of the object I was trying to reference in the visual editor, so there was no object with that id. I changed the id of the object back to the same as is referenced in the code and everything seems to be fine now.
Apologies to anyone whose time I've wasted with this, I'm really new to this concept in Java.

Android Studio MyFirstApp (sendMessage) issue [duplicate]

This question already has answers here:
How to start new activity on button click
(28 answers)
How do I pass data between Activities in Android application?
(53 answers)
Closed 1 year ago.
I am new to this Java programming and android development and still just working with Hello World App while following a youtube video Series.
Youtube tutorial link
Need to change the interface from send Messeage Button press to Welcome Message screen.
My project have two Java claases
MainActivity.java
package com.example.helloworldapp;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
// Specify the onclick method for the button
// Access modifier must be public
// Method name is send Message
// Specify parameter and object of view class
//WHen user click the button system will invoke this method
//With in the method have to create newly created activity
//TO create a new activity have to create a object of intend
// intent is intention of doing something by the android application
// E.g. Start a new activity start a new service broadcast a message
public void sendMessage (View view){
//TO create a new activity have to create a object of intent
//Have to pass 2 parameter 1. context and 2. class name of the target activity
// Context name is this, target activity is MessageActivity
Intent intent = new Intent(this,MessageActivity.class);
//To start the activity have to call the method call start activity
//Then pass intent parameter
startActivity(intent);
}
}
MessageAcitivity.java
package com.example.helloworldapp;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
public class MessageActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_message);
}
}
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:onClick="sendMessage"
tools:context=".MainActivity">
<EditText
android:id="#+id/editTextTextPersonName"
android:layout_width="156dp"
android:layout_height="54dp"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:autofillHints=""
android:ems="10"
android:hint="#string/edit_message"
android:inputType="textPersonName"
app:layout_constraintEnd_toStartOf="#+id/button"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="#+id/button"
android:layout_width="154dp"
android:layout_height="48dp"
android:layout_marginStart="16dp"
android:layout_marginEnd="16dp"
android:text="#string/button_label"
app:layout_constraintBaseline_toBaselineOf="#+id/editTextTextPersonName"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="#+id/editTextTextPersonName" />
</androidx.constraintlayout.widget.ConstraintLayout>
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MessageActivity">
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
android:text="#string/welcome_message"
android:textSize="20sp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
You need to handle your button-click listener to navigate to the next screen.
public class MainActivity extends AppCompatActivity {
private Button button;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = findViewById(R.id.button);
button.setOnClickListener(v -> openMessageAcitivity());
}
public void openMessageAcitivity (View view){
String message = "Your message";
Intent intent = new Intent(this,MessageActivity.class);
intent.putExtra("STRING_YOU_NEED", message);
startActivity(intent);
}
}
Then, to retrieve the value try something like:
String newString;
if (savedInstanceState == null) {
Bundle extras = getIntent().getExtras();
if(extras == null) {
newString= null;
} else {
newString= extras.getString("STRING_YOU_NEED");
}
}
The button click not working could be solved via declaring sendMessage in the layout activity_main.xml in the Button code inserted android:onClick = "sendMessage"
If someone can elaborate on the above it will be very helpful since it is not mentioning in the tutorial itself. Onlick attribute in the layout has alrady as sendMessage as instructed in the tutorial.

Wanting to throw an intent to open a new activity but continually crashes

There's nothing fancy or complicated here. I am just looking to open a new, blank, activity from when I press on the textview. From the projects completed files, I seem to have done it as it wants me to. However, every time I click on the textview to start the intent to open the other activity, it crashes. I cannot seem to find the cause either. In a separate app I've made that send a text string from an edit text field via a button, seems to work fine and that has some other more detailed code to perform that, so I am not entirely sure where the error is arising here.
MainActivity.java
package com.example.android.miwok;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Set the content of the activity to use the activity_main.xml layout file
setContentView(R.layout.activity_main);
}
public void openNumbersList(View view) {
Intent i = new Intent(this, NumbersActivity.class);
startActivity(i);
}
}
NumbersActivity.java
package com.example.android.miwok;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
public class NumbersActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_numbers);
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/tan_background"
android:orientation="vertical"
tools:context="com.example.android.miwok.MainActivity">
<TextView
android:id="#+id/numbers"
style="#style/CategoryStyle"
android:background="#color/category_numbers"
android:onClick="openNumbersList"
android:text="#string/category_numbers" />
<TextView
android:id="#+id/family"
style="#style/CategoryStyle"
android:background="#color/category_family"
android:text="#string/category_family" />
<TextView
android:id="#+id/colors"
style="#style/CategoryStyle"
android:background="#color/category_colors"
android:text="#string/category_colors" />
<TextView
android:id="#+id/phrases"
style="#style/CategoryStyle"
android:background="#color/category_phrases"
android:text="#string/category_phrases" />
</LinearLayout>
In logcat there's an error showing when the app crashes:
02-20 20:10:26.762 7582-7582/com.example.android.miwok E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.android.miwok, PID: 7582
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.android.miwok/com.example.android.miwok.NumbersActivity}: android.view.InflateException: Binary XML file line #0: Binary XML file line #0: Error inflating class TextView
In your xml layout for the Text view with id numbers add
android:clickable="true"
Only if this attribute is set to true the text view's onclick handler will be called
And also add height and width to all your textviews.. like this
android:height="wrap_content"
android:width="wrap_content"
This must have caused the app crash
There is a post on how to set click listeners to textviews here .
"Error inflating class TextView" means that your xml in the next activity is faulty. Try removing all the views from the 2nd activity's layout, double check the layout and then try it. If it is just the intent launch you wanna confirm, this should work.

setOnClickListener never fired - Android/Eclipse

I'm having a problem that seems to be quite common but none of the solutions on here so far have helped me.
I am trying to add a simple button to my app which will start a new activity when clicked.
The button displays ok in my app but when I touch it nothing happens.
My problem is that setOnClickListener() is never fired - when I debug it just skips right over it and no logging message is displayed in LogCat.
Here is my layout file:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="sarah.tourapplication.MainActivity"
tools:ignore="MergeRootFrame" >
<fragment
android:id="#+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.MapFragment" />
<RelativeLayout
android:id="#+id/belowlayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:orientation="horizontal" >
<Button
android:id="#+id/cam_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
android:text="Take Picture" />
</RelativeLayout>
And this is my activity:
public class MainActivity extends FragmentActivity implements OnClickListener {
Button camButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent locationUpdatesIntent = new Intent(this, GetLocationUpdates.class);
startActivity(locationUpdatesIntent);
camButton = (Button)findViewById(R.id.cam_button);
//add button functionality
camButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Log.d("TEST", "Button was clicked.");
Intent cameraIntent = new Intent(getApplicationContext(), CameraActivity.class);
startActivity(cameraIntent);
}
});
}
Can anyone tell me where I've gone wrong? I'd really appreciate any pointers.
I'm using Eclipse on Windows 8 and everything I'm using is up to date.
Thanks in advance!
Thanks for all the help. It turns out that I was calling the click handler in the wrong activity.
Try removing the lines
Intent locationUpdatesIntent = new Intent(this, GetLocationUpdates.class);
startActivity(locationUpdatesIntent);
It looks like you're starting a different activity before you actually get to the point where you're adding the listener...
Sorry I'm on my phone but try to use setOnClickListener(this); and override the onClick method. Inside the onClick method use if (view == yourview) to do w/e you want. :). If it still doesn't work try removing the startActivity method at the very beginning of your codes

Categories

Resources