Reputation: 6832
While following the tutorial: http://developer.android.com/training/basics/firstapp/starting-activity.html, and got help form: My First App crashes when pressing a button, I still (after reading the answers to the question) can't figure out how to fix the problem.
My Code: AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.buttonactivity1.app" > <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.example.buttonactivity1.app.MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name="com.example.buttonactivity1.DisplayMessageActivity" android:label="@string/title_activity_display_message" android:parentActivityName="com.example.buttonactivity1.MainActivity"
>
</manifest>
MainActivity.java:
package com.example.buttonactivity1.app;
import android.support.v7.app.ActionBarActivity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.content.Intent; import android.widget.EditText;
public class MainActivity extends ActionBarActivity { public final static String EXTRA_MESSAGE = "com.example.buttonactivity1.MESSAGE";
/** Called when the user clicks the Send button */ public void sendMessage(View view) { // Do something in response to button Intent intent = new Intent(this, DisplayMessageActivity.class); EditText editText = (EditText) findViewById(R.id.edit_message); String message = editText.getText().toString(); intent.putExtra(EXTRA_MESSAGE, message); startActivity(intent); } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); }
}
DisplayMessageActivity.java:
package com.example.buttonactivity1.app;
import android.support.v7.app.ActionBarActivity; import android.os.Bundle; //import android.view.MenuItem; //importandroid.view.View; import android.support.v4.app.Fragment; //import android.view.LayoutInflater; import android.view.*; import android.content.Intent; import android.widget.TextView;
/** * Created by Neil on 4/21/14. */ public class DisplayMessageActivity extends ActionBarActivity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);
// Get the message from the intent Intent intent = getIntent(); String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE); // Create the text view TextView textView = new TextView(this); textView.setTextSize(40); textView.setText(message); // Set the text view as the activity layout setContentView(textView); } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } /** * A placeholder fragment containing a simple view. */ public static class PlaceholderFragment extends Fragment { public PlaceholderFragment() { } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.fragment_display_message, container, false); return rootView; } }
}
ActivityMain.xml:
<EditText android:id="@+id/edit_message" android:layout_weight="1" android:layout_width="0dp" android:layout_height="wrap_content" android:hint="@string/edit_message" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/button_send" android:onClick="sendMessage" />
fragment_display_message:
tools:context="com.example.myfirstapp.DisplayMessageActivity$PlaceholderFragment"
Error (logcat):
04-22 16:15:23.433 23203-23203/com.example.buttonactivity1.app W/IInputConnectionWrapper﹕ showStatusIcon on inactive InputConnection 04-22 16:38:28.402 24414-24414/com.example.buttonactivity1.app D/AndroidRuntime﹕ Shutting down VM 04-22 16:38:28.402 24414-24414/com.example.buttonactivity1.app W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x40015578) 04-22 16:38:28.550 24414-24414/com.example.buttonactivity1.app E/AndroidRuntime﹕ FATAL EXCEPTION: main java.lang.IllegalStateException: Could not execute method of the activity at android.view.View$1.onClick(View.java:2154) at android.view.View.performClick(View.java:2537) at android.view.View$PerformClick.run(View.java:9157) at android.os.Handler.handleCallback(Handler.java:587) at android.os.Handler.dispatchMessage(Handler.java:92) at android.os.Looper.loop(Looper.java:130) at android.app.ActivityThread.main(ActivityThread.java:3687) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:507) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600) at dalvik.system.NativeStart.main(Native Method) Caused by: java.lang.reflect.InvocationTargetException at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:507) at android.view.View$1.onClick(View.java:2149) at android.view.View.performClick(View.java:2537) at android.view.View$PerformClick.run(View.java:9157) at android.os.Handler.handleCallback(Handler.java:587) at android.os.Handler.dispatchMessage(Handler.java:92) at android.os.Looper.loop(Looper.java:130) at android.app.ActivityThread.main(ActivityThread.java:3687) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:507) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600) at dalvik.system.NativeStart.main(Native Method) Caused by: android.content.ActivityNotFoundException: Unable to find explicit activity class {com.example.buttonactivity1.app/com.example.buttonactivity1.app.DisplayMessageActivity}; have you declared this activity in your AndroidManifest.xml? at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1405) at android.app.Instrumentation.execStartActivity(Instrumentation.java:1379) at android.app.Activity.startActivityForResult(Activity.java:2827) at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:839) at android.app.Activity.startActivity(Activity.java:2933) at com.example.buttonactivity1.app.MainActivity.sendMessage(MainActivity.java:21) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:507) at android.view.View$1.onClick(View.java:2149) at android.view.View.performClick(View.java:2537) at android.view.View$PerformClick.run(View.java:9157) at android.os.Handler.handleCallback(Handler.java:587) at android.os.Handler.dispatchMessage(Handler.java:92) at android.os.Looper.loop(Looper.java:130) at android.app.ActivityThread.main(ActivityThread.java:3687) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:507) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600) at dalvik.system.NativeStart.main(Native Method)
In the other question, I saw the "fragment_main.xml" and "activity_display_message.xml" files. Do I need to have them? is that what's causing the problem?
Upvotes: 0
Views: 980
Reputation: 1479
It seems you have got a problem about your Manifest file.
You added com.example.buttonactivity1.DisplayMessageActivity but code try to run different path
android.content.ActivityNotFoundException: Unable to find explicit activity class {com.example.buttonactivity1.app/com.example.buttonactivity1.app.DisplayMessageActivity}; have you declared this activity in your AndroidManifest.xml?
Upvotes: 0
Reputation: 1006584
Caused by: android.content.ActivityNotFoundException: Unable to find explicit activity class {com.example.buttonactivity1.app/com.example.buttonactivity1.app.DisplayMessageActivity}; have you declared this activity in your AndroidManifest.xml?
You failed to add DisplayMessageActivity
to your manifest in an <activity>
element.
Upvotes: 0