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"/>
Related
I am very new to Android app making, but I'm able to understand java. The program which I'm making, should return relative frequency of characters that are in the text that person will write in the program (input text is meant to be written in EditText field with id inputText, and as soon as person will click button with id continueButton, onClick the program should continue with onClickFindRelevancy method). The code seems fine to me, but the problem is still occuring. I am able to input the text, but as soon as I click the button, then the app stops working and I just can't figure out what is wrong... What do I need to change in code (what is wrong), for the program to do what is intended?
I really tried to find a solution to my problem, but didn't find something like I have (just seeing the code snippets that someone else had problems with), so I hope that this topic won't be closed, because I tried and really didn't find the solution. Have been trying for some time now.
MainActivity.java
package com.example.<theprogrammnameihave>;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;
import android.widget.EditText;
import java.util.Map;
import java.util.TreeMap;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void onClickFindRelevancy(View view)
{
EditText inputText = (EditText) findViewById(R.id.inputText);
String input = inputText.getText().toString();
TextView result = (TextView) findViewById(R.id.result);
Map<Character, Float> k = new TreeMap<Character, Float>();
for (char b : input.toCharArray()) {
if (k.containsKey(b)) {
k.put(b, k.get(b) + 1);
}
else {
k.put(b, 1f);
}
}
for (char b : input.toCharArray()) {
float freq = k.get(b) / input.length();
result.setText(getString(b) + " " + freq);
}
}
}
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">
<TextView
android:id="#+id/greetingText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/greeting_message"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.109" />
<Button
android:id="#+id/continueButton"
android:layout_width="wrap_content"
android:layout_height="48dp"
android:layout_marginStart="136dp"
android:layout_marginTop="32dp"
android:layout_marginEnd="137dp"
android:layout_marginBottom="340dp"
android:onClick="onClickFindRelevancy"
android:text="#string/button_send"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/inputText" />
<EditText
android:id="#+id/inputText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="100dp"
android:layout_marginTop="10dp"
android:layout_marginEnd="101dp"
android:layout_marginBottom="191dp"
android:ems="10"
android:hint="#string/edit_message"
android:inputType="textPersonName"
app:layout_constraintBottom_toTopOf="#+id/continueButton"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/greetingText" />
<TextView
android:id="#+id/result"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="205dp"
android:layout_marginTop="418dp"
android:layout_marginEnd="206dp"
android:layout_marginBottom="294dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.<theprogrammnameihave>">
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
The error I get when the app stops working -
D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.kriptoprogramma, PID: 13645
java.lang.IllegalStateException: Could not execute method for android:onClick
at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:402)
at android.view.View.performClick(View.java:6256)
at android.view.View$PerformClick.run(View.java:24701)
at android.os.Handler.handleCallback(Handler.java:789)
at android.os.Handler.dispatchMessage(Handler.java:98)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6541)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:397)
at android.view.View.performClick(View.java:6256)
at android.view.View$PerformClick.run(View.java:24701)
at android.os.Handler.handleCallback(Handler.java:789)
at android.os.Handler.dispatchMessage(Handler.java:98)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6541)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
Caused by: android.content.res.Resources$NotFoundException: String resource ID #0x54
at android.content.res.Resources.getText(Resources.java:338)
at android.content.res.Resources.getString(Resources.java:432)
at android.content.Context.getString(Context.java:556)
at com.example.kriptoprogramma.MainActivity.onClickFindRelevancy(MainActivity.java:36)
at java.lang.reflect.Method.invoke(Native Method)
at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:397)
at android.view.View.performClick(View.java:6256)
at android.view.View$PerformClick.run(View.java:24701)
at android.os.Handler.handleCallback(Handler.java:789)
at android.os.Handler.dispatchMessage(Handler.java:98)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6541)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
You should use error stack trace, as suggested in comments. See this link.
The mistake is that you use getString method to convert a char to String.
In Android, this is the method of Activity (and Context) to get a string from resources (i. e. when you are localizing your app).
You should use String.valueOf(char) instead.
This question already has answers here:
How to fix Error inflating class null
(2 answers)
Closed 4 years ago.
Am new to Android programming and I decided to build a quiz app. I built the layout and wanted to set up a button to start a new activity which begins the quiz. I also set up the method in the MainActivity correctly but when I run the app and click on the button, the app crashes and leaves a ton of error messages on logcat.
Any help will be appreciated. Below are the codes.
XML Code:
<Button
android:id="#+id/button_view"
android:layout_width="200dp"
android:layout_height="60dp"
android:layout_below="#id/name_view"
android:layout_centerHorizontal="true"
android:layout_marginTop="30dp"
android:background="#drawable/button_round"
android:fontFamily="sans-serif-light"
android:inputType="textCapWords"
android:text="Go!"
android:onClick="startQuestionOne"
android:textSize="24dp" />
MainActivity
public class LoginActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
}
public void startQuestionOne(View view) {
Intent myIntent = new Intent(LoginActivity.this, QuestionOneActivity.class);
startActivity(myIntent);
}
}
Logcat Message
06-25 18:32:34.259 13705-13705/com.example.android.udacityquizapp E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.android.udacityquizapp, PID: 13705
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.android.udacityquizapp/com.example.android.udacityquizapp.QuestionOneActivity}: android.view.InflateException: Binary XML file line #23: Error inflating class null
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2344)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2396)
at android.app.ActivityThread.access$800(ActivityThread.java:154)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1324)
at android.os.Handler.dispatchMessage(Handler.java:110)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:5328)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:828)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:644)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.view.InflateException: Binary XML file line #23: Error inflating class null
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:713)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:287)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:139)
at com.example.android.udacityquizapp.QuestionOneActivity.onCreate(QuestionOneActivity.java:11)
at android.app.Activity.performCreate(Activity.java:5279)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1088)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2308)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2396)
at android.app.ActivityThread.access$800(ActivityThread.java:154)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1324)
at android.os.Handler.dispatchMessage(Handler.java:110)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:5328)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:828)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:644)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at android.support.v7.app.AppCompatViewInflater.createView(AppCompatViewInflater.java:99)
at android.support.v7.app.AppCompatDelegateImplV9.createView(AppCompatDelegateImplV9.java:1035)
at android.support.v7.app.AppCompatDelegateImplV9.onCreateView(AppCompatDelegateImplV9.java:1092)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:684)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:287)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:139)
at com.example.android.udacityquizapp.QuestionOneActivity.onCreate(QuestionOneActivity.java:11)
at android.app.Activity.performCreate(Activity.java:5279)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1088)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2308)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2396)
at android.app.ActivityThread.access$800(ActivityThread.java:154)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1324)
at android.os.Handler.dispatchMessage(Handler.java:110)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:5328)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:828)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:644)
at dalvik.system.NativeStart.main(Native Method)
This is the full 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"
android:background="#drawable/gradient"
tools:context=".LoginActivity">
<ImageView
android:id="#+id/udacity_logo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="30dp"
android:src="#drawable/udacity" />
<TextView
android:id="#+id/textview_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/udacity_logo"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:fontFamily="sans-serif-ligh"
android:text="Udacity Quiz App"
android:textColor="#faebd7"
android:textSize="36dp"
android:textStyle="bold" />
<EditText
android:id="#+id/name_view"
android:layout_width="300dp"
android:layout_height="40dp"
android:layout_below="#id/textview_text"
android:layout_centerHorizontal="true"
android:layout_marginTop="30dp"
android:background="#drawable/edit_round"
android:drawableLeft="#drawable/ic_action_user"
android:ems="10"
android:fontFamily="sans-serif-light"
android:hint="Name"
android:inputType="textCapWords"
android:textColor="#000000"
android:textColorHint="#000000"
android:textSize="16dp" />
<Button
android:id="#+id/button_view"
android:layout_width="200dp"
android:layout_height="60dp"
android:layout_below="#id/name_view"
android:layout_centerHorizontal="true"
android:layout_marginTop="30dp"
android:background="#drawable/button_round"
android:fontFamily="sans-serif-light"
android:inputType="textCapWords"
android:text="Go!"
android:onClick="startQuestionOne"
android:textSize="24dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_marginBottom="8dp"
android:layout_marginRight="8dp"
android:fontFamily="sans-serif-light"
android:text="powered by: #William"
android:textColor="#faebd7" />
</RelativeLayout>
Above is the XML for LoginActivity which contains a button. When the button is clicked, its supposed to start a new activity called QuestionOneActivity, but instead, the app crashes.
LoginActivity
package com.example.android.udacityquizapp;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class LoginActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
}
public void startQuestionOne(View view) {
Intent myIntent = new Intent(LoginActivity.this,
QuestionOneActivity.class);
startActivity(myIntent);
}
}
firstly bind Button in onCreate() as
Button b = (Button) findViewById(R.id.button_view);
then check if your activity QuestionOneActivity is declared in manifest file
after that check QuestionOneActivity activity xml file for error if have any...
Thanks clay_to_n, based on your suggestions, i found out that i used a lower case 'v' for my View tag. When i adjusted it, the app runs as expected.
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 concatenate strings in Android using two activities. Every time I enter two string and hit the button to run the app crashes on the emulator. Can you help me find what I am doing wrong here? My code and the error are below.
Main Activity (Activity 1)
package com.example.tristan.a1p1n1;
import android.content.Intent;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity {
public final static String EXTRA_MESSAGE = "com.example.tristan.a1p1n1.MESSAGE";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// ATTENTION: This was auto-generated to implement the App Indexing API.
// See https://g.co/AppIndexing/AndroidStudio for more information.
client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build();
}
public void sendMessage(View view) {
Intent intent = new Intent(this, DICS.class);
EditText editText = (EditText) findViewById(R.id.editText);
EditText editText2 = (EditText) findViewById(R.id.editText2);
String message = editText.getText().toString() + editText2.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
}}
DICS (Activity 2)
package com.example.tristan.a1p1n1;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
/**
* Created by Tristan on 2/11/2017.
*/
public class DICS extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.dics);
//TextView mTextView = (TextView)findViewById(R.id.textView2);
// mTextView.setText(getIntent().getStringExtra("message"))
}
}
My Error Message:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.tristan.a1p1n1, PID: 3082
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:5610)
at android.view.View$PerformClick.run(View.java:22265)
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:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
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:5610)
at android.view.View$PerformClick.run(View.java:22265)
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:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
Caused by: android.content.ActivityNotFoundException: Unable to find explicit activity class {com.example.tristan.a1p1n1/com.example.tristan.a1p1n1.DICS}; have you declared this activity in your AndroidManifest.xml?
at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1805)
at android.app.Instrumentation.execStartActivity(Instrumentation.java:1523)
at android.app.Activity.startActivityForResult(Activity.java:4224)
at android.support.v4.app.BaseFragmentActivityJB.startActivityForResult(BaseFragmentActivityJB.java:50)
at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:79)
at android.app.Activity.startActivityForResult(Activity.java:4183)
at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:859)
at android.app.Activity.startActivity(Activity.java:4507)
at android.app.Activity.startActivity(Activity.java:4475)
at com.example.tristan.a1p1n1.MainActivity.sendMessage(MainActivity.java:41)
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:5610)
at android.view.View$PerformClick.run(View.java:22265)
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:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
activity_main.xml
<?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: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.example.tristan.a1p1n1.MainActivity">
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:text="Enter a String"
android:ems="10"
android:layout_below="#+id/textView"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:id="#+id/editText" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:text="Enter a String"
android:ems="10"
android:id="#+id/editText2"
android:layout_below="#+id/editText"
android:layout_alignRight="#+id/editText"
android:layout_alignEnd="#+id/editText"
android:layout_marginRight="11dp"
android:layout_marginEnd="11dp"
android:layout_marginTop="13dp" />
<Button
android:text="+"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/editText2"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="23dp"
android:id="#+id/button"
android:onClick="sendMessage" />
</RelativeLayout>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.tristan.a1p1n1">
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity><!-- ATTENTION: This was auto-generated to add Google Play services to your project for
App Indexing. See https://g.co/AppIndexing/AndroidStudio for more information. -->
<meta-data
android:name="com.google.android.gms.version"
android:value="#integer/google_play_services_version" />
</application>
</manifest>
dics.xml
<?xml version="1.0" encoding="utf-8"?>
<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:text="TextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/textView2" />
</LinearLayout>
your Button Xml should be:
<Button
android:text="+"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/editText2"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="23dp"
android:id="#+id/button"
android:onClick="sendMessage" /> //change here
Try this: in your MainActivity
String message = editText.getText().toString() + editText2.getText().toString();
intent.putExtra("message", message);
Now in Dics activity:
TextView mTextView = (TextView)findViewById(R.id.textView2);
mTextView.setText(getIntent().getStringExtra("message")); //the key ID should match
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();