Reputation: 11
I am using the official Android Studio tutorials. And even though my code and everything else, is like the tutorial teaches me, my app keeps crashing, as soon as I press send. (https://developer.android.com/training/basics/firstapp/starting-activity.html | that´s where I am rn).
That´s my code for the main activity:
package example.myfirstapp;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity {
public static final String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
/** Called when the user taps the Send button */
public void sendMessage(View view) {
Intent intent = new Intent(this, DisplayMessageActivity.class);
EditText editText = (EditText) findViewById(R.id.editText);
String message = editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
}
}
And that´s my code for the Display Message Activity
package example.myfirstapp;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
public class DisplayMessageActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_message);
// Get the Intent that started this activity and extract the string
Intent intent = getIntent();
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
// Capture the layout's TextView and set the string as its text
TextView textView = findViewById(R.id.textView);
textView.setText(message);
}
And that´s what Logcat says is wrong
11-05 12:36:51.627 2265-2265/com.google.android.googlequicksearchbox:search E/SearchServiceStarter: Task 174 failed or timed out. Client 9963085251046432 disconnecting from SearchService! java.util.concurrent.CancellationException: Task was cancelled. at com.google.common.util.concurrent.d.da(SourceFile:80) at com.google.common.util.concurrent.d.get(SourceFile:62) at com.google.common.util.concurrent.cf.o(SourceFile:2) at com.google.common.util.concurrent.ax.m(SourceFile:50) at com.google.common.util.concurrent.az.run(SourceFile:5) at com.google.android.apps.gsa.shared.util.concurrent.a.bf.run(SourceFile:2) at android.os.Handler.handleCallback(Handler.java:790) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:164) at android.app.ActivityThread.main(ActivityThread.java:6494) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
Here´s what Logcat says now
11-07 12:24:47.927 4134-4134/example.myfirstapp E/AndroidRuntime: FATAL EXCEPTION: main Process: example.myfirstapp, PID: 4134 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:6294) at android.view.View$PerformClick.run(View.java:24770) at android.os.Handler.handleCallback(Handler.java:790) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:164) at android.app.ActivityThread.main(ActivityThread.java:6494) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807) 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:6294) at android.view.View$PerformClick.run(View.java:24770) at android.os.Handler.handleCallback(Handler.java:790) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:164) at android.app.ActivityThread.main(ActivityThread.java:6494) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807) Caused by: android.content.ActivityNotFoundException: Unable to find explicit activity class {example.myfirstapp/example.myfirstapp.DisplayMessageActivity}; have you declared this activity in your AndroidManifest.xml? at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1933) at android.app.Instrumentation.execStartActivity(Instrumentation.java:1616) at android.app.Activity.startActivityForResult(Activity.java:4488) at android.support.v4.app.BaseFragmentActivityApi16.startActivityForResult(BaseFragmentActivityApi16.java:54) at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:67) at android.app.Activity.startActivityForResult(Activity.java:4446) at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:720) at android.app.Activity.startActivity(Activity.java:4807) at android.app.Activity.startActivity(Activity.java:4775) at example.myfirstapp.MainActivity.sendMessage(MainActivity.java:22) 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:6294) at android.view.View$PerformClick.run(View.java:24770) at android.os.Handler.handleCallback(Handler.java:790) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:164) at android.app.ActivityThread.main(ActivityThread.java:6494) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
Upvotes: 0
Views: 4186
Reputation: 659
The Logcat error you copied is not related to your application. You can use Logcat filters in Android Studio in order to find actual cause of the application crash. There are at least two variants for you:
PS: most likely you forgot to register one of the activities (or both) in AndroidManifest.xml file
PPS (for other commentators): Android Studio 3.0 with new Gradle plugin do not require to cast result of findViewById anymore. Upgrade and enjoy!
Upvotes: 0
Reputation: 60
In the "public void sendMessage(View view)" part you can remove the View argument as you don't need it.
So it can be just like this "public void sendMessage()"
Also, in the display message activity, "TextView textView = findViewById(R.id.textView);" over here he needs to add casting so it looks like this:
"TextView textView = (TextView) findViewById(R.id.textView);"
Should work fine after this.
Upvotes: -1
Reputation: 113
Try this and also check that whether you have added both activities in manifest or not.
TextView textView = (TextView)findViewById(R.id.textView);......modify this in DisplayMessageActivity.
public static final String EXTRA_MESSAGE = "MESSAGE";......modify this in MainActivity.
Upvotes: -1