I am new in android. I am trying to make simple login activity but I am getting run time error when I click on Login button. I think I am not getting data correctly.I have check and there is a data in SQLite in corresponding to that PK.
logcat.
FATAL EXCEPTION: main
Process: com.example.champ.remindme, PID: 4043
java.lang.IllegalStateException: Could not execute method for android:onClick
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:289)
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)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:284)
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)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.database.Cursor android.database.sqlite.SQLiteDatabase.rawQuery(java.lang.String, java.lang.String[])' on a null object reference
at com.example.champ.remindme.Login.LogIn(Login.java:45)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:284)
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)
java code.
package com.example.champ.remindme;
import android.app.AlertDialog;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class Login extends AppCompatActivity {
EditText edtUsername, edtPass;
String Username,Password;
int counter=3;
Button LoginButton;
SQLiteDatabase db;
// TextView txtAttempts;
//private final int interval = 3000;
/*private Runnable runnable = new Runnable(){
public void run() {
LoginButton.setEnabled(true);
}
}; */
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
LoginButton= (Button)findViewById(R.id.LoginButton);
edtUsername=(EditText)findViewById(R.id.edtitem);
// Username=edtUsername.getText().toString();
edtPass=(EditText)findViewById(R.id.password);
}
public void LogIn(View v){
if((edtUsername.getText().toString().trim().length()==0)||(edtPass.getText().toString().trim().length()==0))
{
showMessage("Error", "Please enter the empty Text Box");
return;
}
Cursor c=db.rawQuery("SELECT * FROM User WHERE Username='"+ edtUsername.getText()+"'", null);
if(c.moveToFirst())
{
Username=c.getString(1);
Password=c.getString(2);
if (edtUsername.getText().toString().equals(Username) && edtPass.getText().toString().equals(Password)){
Toast.makeText(Login.this, "Login Successful", Toast.LENGTH_LONG).show();
Intent intent = new Intent(this, Menu.class);
intent.putExtra("Username",Username);
startActivity(intent);
}
else {
Toast.makeText(Login.this, "Login Failed", Toast.LENGTH_LONG).show();
counter--;
}
// txtAttempts.setText("Attempts Left: " + counter);
if (counter == 0) {
LoginButton.setEnabled(false);
// new Timer().schedule((TimerTask) runnable,interval);
}
}
else
{
showMessage("Error", "Invalid Username");
}
/*-----------------------------------------------------------------*/
}
public void Signup(View v) {
Intent intent = new Intent(this, SignUp.class);
startActivity(intent);
}
public void showMessage(String title,String message)
{
AlertDialog.Builder builder=new AlertDialog.Builder(this);
builder.setCancelable(true);
builder.setTitle(title);
builder.setMessage(message);
builder.show();
}
}
XML code.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.example.champ.remindme.Login"
android:background="#drawable/back">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageView2"
android:src="#drawable/remind_me_logo"
android:contentDescription="Logo" />
<EditText
android:id="#+id/edtitem"
android:layout_width="265dp"
android:layout_height="wrap_content"
android:padding="10dip"
android:background="#drawable/username_rounded_edited_text"
android:inputType="text"
android:hint="Username"
android:textAlignment="center"
android:layout_marginTop="37dp"
android:layout_below="#+id/imageView2"
android:layout_centerHorizontal="true" />
<EditText
android:id="#+id/password"
android:layout_width="265dp"
android:layout_height="wrap_content"
android:padding="10dip"
android:background="#drawable/pass_rounded_edited_text"
android:inputType="text"
android:hint="Password"
android:textAlignment="center"
android:layout_below="#+id/edtitem"
android:layout_centerHorizontal="true" />
<Button
android:id="#+id/LoginButton"
android:background="#drawable/blue_botton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/login"
style="#style/ButtonText"
android:onClick="LogIn"
android:layout_below="#+id/password"
android:layout_alignLeft="#+id/password"
android:layout_alignStart="#+id/password"
android:layout_alignRight="#+id/password"
android:layout_alignEnd="#+id/password" />
<Button
android:id="#+id/txtSignup"
android:background="#drawable/blue_botton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Signup"
style="#style/ButtonText"
android:onClick="Signup"
android:layout_below="#+id/LoginButton"
android:layout_alignLeft="#+id/LoginButton"
android:layout_alignStart="#+id/LoginButton" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageView3"
android:src="#drawable/fb_loging"
android:layout_alignBottom="#+id/txtSignup"
android:layout_toRightOf="#+id/txtSignup"
android:layout_toEndOf="#+id/txtSignup" />
</RelativeLayout>
You didn't initialize db before using it. Add this to your onCreate()..
SQLiteDatabase db = this.getWritableDatabase();
Nothing to do. Just in your XML file's Button attribute remove
android:onClick
And in your activity type:-
LoginButton.setOnClickListener(new OnClickListener(){
#Override
onClick(){
LogIn();
}
});
I also faced this problem. Yeah, I know that your process is also not wrong. But the truth is I just don't know why that process doesn't work (for me). But when I implement onClick method programmatically then it works like a charm. And I hope you will also get your code working
And also add onCreate method
db = getWritableDatabase();
Related
I'm very new to Android, and I've been stuck on trying to get the value string from a material inputField component for hours.
To summarize, I have a button that clicks and opens up a dialog, and on the dialog there is an inputField and button. The button will read out what the inputField says using TextToSpeech.
I am confident that this is how you get the string from the material component (source: http://charmndroid.blogspot.com/2015/06/how-to-use-textinputlayout-in-android.html)
Everything works except for the string value extraction from inputField. If I only comment that out, everything works. Including the speech method (I tried with a static string and it works).
Could you please help me figure this out?
The error I'm getting:
2020-10-23 01:00:56.610 12012-12012/com.example.mint_anroidapp E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.mint_anroidapp, PID: 31408
java.lang.IllegalStateException: Could not execute method for android:onClick
at android.view.View$DeclaredOnClickListener.onClick(View.java:5979)
at android.view.View.performClick(View.java:7125)
at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:992)
at android.view.View.performClickInternal(View.java:7102)
at android.view.View.access$3500(View.java:801)
at android.view.View$PerformClick.run(View.java:27336)
at android.os.Handler.handleCallback(Handler.java:883)
at android.os.Handler.dispatchMessage(Handler.java:100)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7356)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at android.view.View$DeclaredOnClickListener.onClick(View.java:5974)
at android.view.View.performClick(View.java:7125)
at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:992)
at android.view.View.performClickInternal(View.java:7102)
at android.view.View.access$3500(View.java:801)
at android.view.View$PerformClick.run(View.java:27336)
at android.os.Handler.handleCallback(Handler.java:883)
at android.os.Handler.dispatchMessage(Handler.java:100)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7356)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.widget.EditText com.google.android.material.textfield.TextInputLayout.getEditText()' on a null object reference
at com.example.mint_anroidapp.MainActivity.speak(MainActivity.java:49)
at java.lang.reflect.Method.invoke(Native Method)
at android.view.View$DeclaredOnClickListener.onClick(View.java:5974)
at android.view.View.performClick(View.java:7125)
at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:992)
at android.view.View.performClickInternal(View.java:7102)
at android.view.View.access$3500(View.java:801)
at android.view.View$PerformClick.run(View.java:27336)
at android.os.Handler.handleCallback(Handler.java:883)
at android.os.Handler.dispatchMessage(Handler.java:100)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7356)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
MainActivity.java
public class MainActivity extends AppCompatActivity {
private TextToSpeech text_to_speech;
private TextInputLayout one_voice;
Dialog myDialog;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
one_voice = (TextInputLayout) findViewById(R.id.command_one_voice);
myDialog = new Dialog(this);
text_to_speech = new TextToSpeech(this, new TextToSpeech.OnInitListener() {
#Override
public void onInit(int status) {
if(status != TextToSpeech.ERROR) {
text_to_speech.setLanguage(Locale.CANADA);
}
}
});
}
public void speak(View v) {
String to_speak = one_voice.getEditText().getText().toString();
text_to_speech.speak(to_speak, TextToSpeech.QUEUE_FLUSH, null);
}
public void OneShowPopup(View v) {
TextView text_close;
Button btnFollow = (Button) myDialog.findViewById(R.id.one_save_button);
myDialog.setContentView(R.layout.command_one);
text_close =(TextView) myDialog.findViewById(R.id.text_close);
text_close.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
myDialog.dismiss();
}
});
myDialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
myDialog.show();
}
command_one.xml
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/command_one_voice"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:hint="#string/text">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.button.MaterialButton
android:id="#+id/one_play_button"
android:onClick="speak"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:text="#string/play"
style="?attr/borderlessButtonStyle" />
MainActivity.xml
<com.google.android.material.button.MaterialButton
android:onClick="OneShowPopup"
android:id="#+id/command_one"
android:layout_width="140dp"
android:layout_height="100dp"
android:padding="5dp"
app:icon="#drawable/checked"
android:textColor="#ffffff"
android:text="Title 1"
android:layout_marginRight="30dp"
app:backgroundTint="#2BC9AF"
app:iconGravity="textStart" />
one_voice = (TextInputLayout) findViewById(R.id.command_one_voice);
You try to find a view in your activity layout (MainActivity.xml), but it is in the dialog layout (command_one.xml). You should do something like this:
one_voice = (TextInputLayout) myDialog.findViewById(R.id.command_one_voice);
But only after you init this dialog.
You need to add id to <com.google.android.material.textfield.TextInputEditText then you can get text from it directly.
Add id to your xml code as below
<com.google.android.material.textfield.TextInputEditText
android:id="#+id/edt_text"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
Then add below lines to activity
private TextInputEditText edtText;
In oncreate add below line
edtText= (TextInputEditText) findViewById(R.id.edt_text);
Edit your method
public void speak(View v) {
String to_speak = edtText.getText().toString();
text_to_speech.speak(to_speak, TextToSpeech.QUEUE_FLUSH, null);
}
I know that this question looks similar to others on SO but i have tried all the suggestions in the other similar questions and haven't had any success.
I'm getting this error:
2020-08-03 20:41:21.372 8916-8916/com.example.spacing E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.spacing, PID: 8916
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.spacing/com.example.spacing.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.pm.ApplicationInfo android.content.Context.getApplicationInfo()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2843)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.pm.ApplicationInfo android.content.Context.getApplicationInfo()' on a null object reference
at android.content.ContextWrapper.getApplicationInfo(ContextWrapper.java:159)
at android.view.ContextThemeWrapper.getTheme(ContextThemeWrapper.java:157)
at android.content.Context.obtainStyledAttributes(Context.java:675)
at androidx.appcompat.app.AppCompatDelegateImpl.createSubDecor(AppCompatDelegateImpl.java:692)
at androidx.appcompat.app.AppCompatDelegateImpl.ensureSubDecor(AppCompatDelegateImpl.java:659)
at androidx.appcompat.app.AppCompatDelegateImpl.findViewById(AppCompatDelegateImpl.java:479)
at androidx.appcompat.app.AppCompatActivity.findViewById(AppCompatActivity.java:214)
at com.example.spacing.MainActivity.<init>(MainActivity.java:15)
at java.lang.Class.newInstance(Native Method)
at android.app.AppComponentFactory.instantiateActivity(AppComponentFactory.java:69)
at androidx.core.app.CoreComponentFactory.instantiateActivity(CoreComponentFactory.java:41)
at android.app.Instrumentation.newActivity(Instrumentation.java:1215)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2831)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
2020-08-03 20:41:21.411 8916-8916/com.example.spacing I/Process: Sending signal. PID: 8916 SIG: 9
on this code:
package com.example.spacing;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity {
public int firstValue;
EditText first;
String firstString;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
first = (EditText)findViewById(R.id.firstNum);
firstString = first.getText().toString();
Button Go = (Button) findViewById(R.id.goButton);
Go.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
firstValue = Integer.parseInt(firstString);
System.out.print(firstValue);
}
});
}
}
Here is the 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">
<Button
android:id="#+id/goButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/buttonName"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.898" />
<TextView
android:id="#+id/FirstAttribute"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/spacedMaterial"
app:layout_constraintBottom_toTopOf="#+id/goButton"
app:layout_constraintEnd_toStartOf="#+id/firstNum"
app:layout_constraintHorizontal_bias="0.457"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.043" />
<EditText
android:id="#+id/firstNum"
android:layout_width="165dp"
android:layout_height="40dp"
android:ems="10"
android:hint="#string/inMM"
android:importantForAutofill="no"
android:inputType="number"
app:layout_constraintBottom_toBottomOf="#+id/FirstAttribute"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.934"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="#+id/FirstAttribute"
app:layout_constraintVertical_bias="0.476" />
<TextView
android:id="#+id/secondAttribute"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/amount"
app:layout_constraintBottom_toTopOf="#+id/goButton"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.073"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.131" />
<EditText
android:id="#+id/secondNumber"
android:layout_width="165dp"
android:layout_height="40dp"
android:ems="10"
android:hint="#string/number"
android:importantForAutofill="no"
android:inputType="number"
app:layout_constraintBottom_toBottomOf="#+id/secondAttribute"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.934"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="#+id/secondAttribute" />
<TextView
android:id="#+id/thirdAttribute"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/areaWidth"
app:layout_constraintBottom_toTopOf="#+id/goButton"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.068"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.218" />
<EditText
android:id="#+id/thirdNumber"
android:layout_height="40dp"
android:layout_width="165dp"
android:ems="10"
android:hint="#string/inMM"
android:inputType="number"
app:layout_constraintBottom_toBottomOf="#+id/thirdAttribute"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.934"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="#+id/thirdAttribute"
app:layout_constraintVertical_bias="0.476"
android:importantForAutofill="no" />
</androidx.constraintlayout.widget.ConstraintLayout>
Suggestions i have tried:
I have tried changing the init for the edit text to before and after on-create.
I have made sure that all ids are correct and are referencing the right thing.
And about 5 other things that i cant recall but none of them worked when i tried to build.
Any help is appreciated.
Problem is:
you are trying to initialize the "firstString" variable OUTSIDE of the click listener and as soon as the Activity is created, which means the variable will always be a null object because you did not actually write something in the EditText.
Simply initialize the String inside the ClickListener!
` package com.example.spacing;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity {
public int firstValue;
EditText first;
String firstString;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
first = (EditText)findViewById(R.id.firstNum);
Button Go = (Button) findViewById(R.id.goButton);
Go.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
firstString = first.getText().toString();
firstValue = Integer.parseInt(firstString);
System.out.print(firstValue);
}
});
}
}`
I'm unable to get data(text) from an EditText to a TextView via a Button.
Logic should be fine and I can't really find the mistake.
I get the error as soon as I click the button which triggeres "sendMessage(View v)".
I can't append text to "chat" but I don't know why.
MainActivity.java:
package com.chattr.neonardo.chattr;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
TextView chat;
EditText message;
Button send;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
chat = (TextView) findViewById(R.id.chat);
message = (EditText) findViewById(R.id.message);
send = (Button) findViewById(R.id.send);
Log.d("test", "test2");
/* send.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
chat.append("\n" + message.getText().toString());
}
});*/
}
public void onMatchTap(View v) {
Toast myToast = Toast.makeText(getApplicationContext(), R.string.searching, Toast.LENGTH_LONG);
myToast.show();
setContentView(R.layout.chat_main);
Log.d("test", "test4");
}
public void sendMessage(View v) {
Log.d("test", "test6");
chat.append("\n" + message.getText().toString());
Log.d("test", "test7");
}
}
I've tried something with the onClickListener but it didn't work so I commented it out.
main_activity.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:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.chattr.neonardo.chattr.MainActivity">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="#drawable/chattr"
android:id="#+id/imageView"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="91dp"
android:onClick="onMatchTap"
android:text="#string/start_chat" />
I'm working with two different xml's because I'm changing scenes.
chat_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:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="1">
<TextView
android:id="#+id/chat"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1.00"
android:autoLink="web"
android:text="#string/chat_connection"
android:visibility="visible" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<EditText
android:id="#+id/message"
android:layout_width="301dp"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="text"
android:text="#string/defaultMessage" />
<Button
android:id="#+id/send"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:elevation="0dp"
android:onClick="sendMessage"
android:text="Send" />
</LinearLayout>
That's the error:
Process: com.chattr.neonardo.chattr, PID: 24340
java.lang.IllegalStateException: Could not execute method for android:onClick
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:293)
at android.view.View.performClick(View.java:5637)
at android.view.View$PerformClick.run(View.java:22429)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
at android.view.View.performClick(View.java:5637)
at android.view.View$PerformClick.run(View.java:22429)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.append(java.lang.CharSequence)' on a null object reference
at com.chattr.neonardo.chattr.MainActivity.sendMessage(MainActivity.java:47)
at java.lang.reflect.Method.invoke(Native Method)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
at android.view.View.performClick(View.java:5637)
at android.view.View$PerformClick.run(View.java:22429)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
Remove setContentView(R.layout.chat_main);
You should use Intent to open a new activity inside onClick method.
Intent intent = new Intent(CurrentActivity.this,NextActivity.class);
startActivity(intent);
Should be
public void onMatchTap(View v) {
Toast myToast = Toast.makeText(getApplicationContext(), R.string.searching, Toast.LENGTH_LONG).show();
Intent intent = new Intent(MainActivity.this,ChatMain.class);
startActivity(intent);
Log.d("test", "test4");
}
chat = (TextView) findViewById(R.id.chat);
This TextView is Define in your chat_main.xml file and you try to bind it in your Main Activity.that's why it will gives you NullPointerException.
I am attempting to get Strings from 2 Edittexts within a Dialog Fragment, but my app keeps crashing when the activity starts. It is some sort of issue with being able to bind the edittexts from the dialog. I am not sure of the correct way to bind them.
Here is the crash:
09-24 11:34:30.366 16147-16147/com.epicodus.concertaid E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.epicodus.concertaid, PID: 16147
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.epicodus.concertaid/com.epicodus.concertaid.ui.UserProfileActivity}: java.lang.RuntimeException: Unable to bind views for com.epicodus.concertaid.ui.UserProfileActivity
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
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: java.lang.RuntimeException: Unable to bind views for com.epicodus.concertaid.ui.UserProfileActivity
at butterknife.ButterKnife.bind(ButterKnife.java:322)
at butterknife.ButterKnife.bind(ButterKnife.java:237)
at com.epicodus.concertaid.ui.UserProfileActivity.onCreate(UserProfileActivity.java:37)
at android.app.Activity.performCreate(Activity.java:6237)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
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: java.lang.IllegalStateException: Required view 'userEmailEditText' with ID 2131558627 for field 'mUserEmailEditText' was not found. If this view is optional add '#Nullable' annotation.
at butterknife.ButterKnife$Finder.findRequiredView(ButterKnife.java:140)
at com.epicodus.concertaid.ui.UserProfileActivity$$ViewBinder.bind(UserProfileActivity$$ViewBinder.java:15)
at com.epicodus.concertaid.ui.UserProfileActivity$$ViewBinder.bind(UserProfileActivity$$ViewBinder.java:8)
at butterknife.ButterKnife.bind(ButterKnife.java:319)
at butterknife.ButterKnife.bind(ButterKnife.java:237)
at com.epicodus.concertaid.ui.UserProfileActivity.onCreate(UserProfileActivity.java:37)
at android.app.Activity.performCreate(Activity.java:6237)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
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)
And here is the Activity:
import android.content.DialogInterface;
import android.graphics.Typeface;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import com.epicodus.concertaid.Constants;
import com.epicodus.concertaid.R;
import com.firebase.client.Firebase;
import com.firebase.client.FirebaseError;
import butterknife.Bind;
import butterknife.ButterKnife;
public class UserProfileActivity extends AppCompatActivity implements View.OnClickListener {
#Bind(R.id.summaryTextView) TextView mSummaryTextView;
#Bind(R.id.deleteAccountButton) Button mDeleteAccountButton;
#Bind(R.id.userEmailEditText) EditText mUserEmailEditText;
#Bind(R.id.userPasswordEditText) EditText mUserPasswordEditText;
private Firebase mFirebaseRef;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_user_profile);
ButterKnife.bind(this);
mDeleteAccountButton.setOnClickListener(this);
//GET REFERENCE TO FIREBASE
mFirebaseRef = new Firebase(Constants.FIREBASE_URL);
//SETS FONT FOR TITLE
Typeface tf = Typeface.createFromAsset(getAssets(), "fonts/MUSICNET.ttf");
mSummaryTextView.setTypeface(tf);
}
#Override
public void onClick(View view) {
if(view == mDeleteAccountButton) {
createAlertDialog();
}
}
public void deleteUser(String userEmail, String userPassword) {
Firebase.ResultHandler handler = new Firebase.ResultHandler() {
#Override
public void onSuccess() {
Toast.makeText(UserProfileActivity.this, "User deleted Successflly", Toast.LENGTH_LONG).show();
}
#Override
public void onError(FirebaseError firebaseError) {
Toast.makeText(UserProfileActivity.this, "There was an error, please try again", Toast.LENGTH_LONG).show();
}
};
mFirebaseRef.removeUser(userEmail, userPassword, handler );
}
public void createAlertDialog() {
AlertDialog.Builder builder = new AlertDialog.Builder(UserProfileActivity.this);
LayoutInflater inflater = UserProfileActivity.this.getLayoutInflater();
View rootView = inflater.inflate(R.layout.delete_user_dialog, null);
builder.setView(inflater.inflate(R.layout.delete_user_dialog, null));
builder.setMessage(R.string.dialog_message)
.setTitle(R.string.dialog_title);
builder.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// User cancelled the dialog
}
});
builder.setPositiveButton(R.string.delete, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// User clicked OK button
String userEmail = mUserEmailEditText.getText().toString();
String userPassword = mUserPasswordEditText.getText().toString();
deleteUser(userEmail, userPassword);
}
});
AlertDialog dialog = builder.create();
dialog.show();
}
}
This is the layout for the activity
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
tools:context="com.epicodus.concertaid.ui.UserProfileActivity"
android:id="#+id/relativeLayout">
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/imageView"
android:layout_centerVertical="true"
android:scaleType="fitXY"
android:layout_centerHorizontal="true"
android:src="#drawable/background1" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Account Details"
android:textColor="#0288D1"
android:id="#+id/summaryTextView"
android:layout_alignParentTop="true"
android:textSize="30sp"
android:layout_centerInParent="true"
android:layout_marginTop="10dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add Photo"
android:textColor="#0288D1"
android:background="#drawable/buttonshape"
android:id="#+id/addPhotoButton"
android:layout_below="#+id/summaryTextView"
android:layout_centerHorizontal="true"
android:textSize="25sp"
android:layout_marginTop="25dp" />
<ImageView
android:layout_width="100dp"
android:layout_height="100dp"
android:id="#+id/userPhotoImageView"
android:layout_marginTop="25dp"
android:layout_below="#+id/addPhotoButton"
android:layout_centerHorizontal="true"
android:contentDescription="#string/current_user_s_image"
android:src="#drawable/blank_user" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Change Email"
android:textColor="#0288D1"
android:id="#+id/changeEmailButton"
android:background="#drawable/buttonshape"
android:layout_below="#+id/userPhotoImageView"
android:layout_centerHorizontal="true"
android:textSize="25sp"
android:layout_marginTop="25dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Change Password"
android:textSize="25sp"
android:textColor="#0288D1"
android:layout_marginTop="25dp"
android:background="#drawable/buttonshape"
android:id="#+id/changePasswordEmailButton"
android:layout_below="#+id/changeEmailButton"
android:layout_centerHorizontal="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Delete Account"
android:textSize="25sp"
android:textColor="#0288D1"
android:layout_marginTop="25dp"
android:id="#+id/deleteAccountButton"
android:layout_below="#+id/changePasswordEmailButton"
android:layout_centerHorizontal="true"
android:background="#drawable/buttonshape" />
Here is the layout for the AlertDialog:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<EditText
android:id="#+id/userEmailEditText"
android:inputType="textEmailAddress"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:layout_marginLeft="4dp"
android:layout_marginRight="4dp"
android:layout_marginBottom="4dp"
android:hint="Enter Email" />
<EditText
android:id="#+id/userPasswordEditText"
android:inputType="textPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="4dp"
android:layout_marginLeft="4dp"
android:layout_marginRight="4dp"
android:layout_marginBottom="16dp"
android:fontFamily="sans-serif"
android:hint="Enter Password"/>
</LinearLayout>
Any example to show how to correctly Bind the mUserEmailEditText and mUserPasswordEditText
Thank you
When you use bind like that is supposed to be from a existing ID in your activity layout, since you dont have it in your xml it will crash.. Dont bind the EditText and just create them in the AlertDialog, and im use setParams to change their layout
#Bind(R.id.userEmailEditText) EditText mUserEmailEditText;
#Bind(R.id.userPasswordEditText) EditText mUserPasswordEditText;
You can not bind the view from the xml which is not inflated as part of Activity's view. So remove #Bind from above line as follows.
EditText mUserEmailEditText;
EditText mUserPasswordEditText;
and inflate them while creating your dialog in createAlertDialog() method. So replace following 2 lines
View rootView = inflater.inflate(R.layout.delete_user_dialog, null);
builder.setView(inflater.inflate(R.layout.delete_user_dialog, null));
with
View rootView = inflater.inflate(R.layout.delete_user_dialog, null);
mUserEmailEditText = rootView.findViewById(R.id.userEmailEditText);
mUserPasswordEditText = rootView.findViewById(R.id.userPasswordEditText);
builder.setView(rootView);
I am new to android programming.i started working with multiple activities and run the app, when I click on the "Open Second Activity"/"Open Third Activity" Buttons, the Genymotion emulator says: Unfortunately, FirstApp has stopped. And returns to the android menu..why?i am getting error like this
java.lang.IllegalStateException: Could not execute method of the activity
at android.view.View$1.onClick(View.java:4020)
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:5257)
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)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at android.view.View$1.onClick(View.java:4015)
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:5257)
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)
Caused by: android.content.ActivityNotFoundException: Unable to find explicit activity class {com.jahnaviswaroop11gmail.firstapp/com.jahnaviswaroop11gmail.firstapp.SecondActivity}; have you declared this activity in your AndroidManifest.xml?
at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1777)
at android.app.Instrumentation.execStartActivity(Instrumentation.java:1501)
at android.app.Activity.startActivityForResult(Activity.java:3745)
at android.app.Activity.startActivityForResult(Activity.java:3706)
at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:820)
at android.app.Activity.startActivity(Activity.java:4016)
at android.app.Activity.startActivity(Activity.java:3984)
at com.jahnaviswaroop11gmail.firstapp.FirstActivity.showGreetings(FirstActivity.java:25)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at android.view.View$1.onClick(View.java:4015)
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:5257)
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)
my FirstActivity.java
public class FirstActivity extends AppCompatActivity {
TextView textView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.first_layout);
textView = (TextView) findViewById(R.id.greetings_text_view);
}
public void showGreetings(View view){
String button_text = ( (Button) view).getText().toString();
if (button_text.equals("Open Second Activity"))
{
Intent intent = new Intent(this,SecondActivity.class);
startActivity(intent);
}
else if (button_text.equals("Open Third Activity"))
{
Intent intent = new Intent(this,ThirdActivity.class);
startActivity(intent);
}
}
}
my SecondActvity.java
import android.app.Activity;
import android.os.Bundle;
public class SecondActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second_layout);
}
}
my ThirdActivity.java
import android.app.Activity;
import android.os.Bundle;
public class ThirdActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.third_layout);
}
}
my firstlayout.xml file
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="greetings appear here"
android:id="#+id/greetings_text_view"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Open Second Activity"
android:onClick="showGreetings"
android:layout_below="#+id/greetings_text_view"
android:id="#+id/button" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Open Third Activity"
android:onClick="showGreetings"
android:id="#+id/button2"
android:layout_below="#+id/button"
android:layout_centerHorizontal="true"
android:layout_marginTop="71dp" />
my secondlyout xml file
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Welcome to Second Activity"
android:id="#+id/textView"
android:layout_gravity="center_horizontal" />
my third layout xml file
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Welcome to third Activity"
android:id="#+id/textView2"
android:layout_gravity="center_horizontal" />
Please verify the Exception stack trace before posting a question.
The exception clearly mentions the cause for the issue..
Caused by: android.content.ActivityNotFoundException: Unable to find explicit activity class {com.jahnaviswaroop11gmail.firstapp/com.jahnaviswaroop11gmail.firstapp.SecondActivity}; have you declared this activity in your AndroidManifest.xml?
Add the SecondActvity to your AndroidManifest.xml inside <application> tag
<activity android:name="com.jahnaviswaroop11gmail.firstapp.SecondActivity" />
Add this to your manifest inside <application> tag. It should solve your problem.
<activity android:name="com.jahnaviswaroop11gmail.firstapp.SecondActivity"/>
You need to put the both First and second activity to you manifest ...
<activity android:name=".SecondActivity"/>
<activity android:name=".ThirdActivity"/>
and also get text
Button button = (Button)view;
String button_text= button.getText().toString();
and also add the permission
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
Androidmanifest.xml has got incorrect data, you have to add information about second and third activities
<activity
android:name=".SecondActivity"/>
<activity
android:name=".ThirdActivity"/>