MainActivity.java
package com.example.myfirstapp;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.EditText;
import android.content.Intent;
public class MainActivity extends Activity {
public final static String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";
#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;
}
/** Called when the user clicks the Send button */
public void sendMessage(View view) {
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);
}
}
activitymain.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<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" />
</LinearLayout>
DisplayMessageActivity.java
import android.support.v7.app.ActionBarActivity;
import android.support.v4.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
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;
}
}
}
Error Log
04-10 01:37:00.460: W/dalvikvm(1650): threadid=1: thread exiting with uncaught exception (group=0xb3aaeba8)
04-10 01:37:00.520: E/AndroidRuntime(1650): FATAL EXCEPTION: main
04-10 01:37:00.520: E/AndroidRuntime(1650): Process: com.example.myfirstapp, PID: 1650
04-10 01:37:00.520: E/AndroidRuntime(1650): java.lang.IllegalStateException: Could not find a method sendMessage(View) in the activity class com.example.myfirstapp.MainActivity for onClick handler on view class android.widget.Button
04-10 01:37:00.520: E/AndroidRuntime(1650): at android.view.View$1.onClick(View.java:3810)
04-10 01:37:00.520: E/AndroidRuntime(1650): at android.view.View.performClick(View.java:4438)
04-10 01:37:00.520: E/AndroidRuntime(1650): at android.view.View$PerformClick.run(View.java:18422)
04-10 01:37:00.520: E/AndroidRuntime(1650): at android.os.Handler.handleCallback(Handler.java:733)
04-10 01:37:00.520: E/AndroidRuntime(1650): at android.os.Handler.dispatchMessage(Handler.java:95)
04-10 01:37:00.520: E/AndroidRuntime(1650): at android.os.Looper.loop(Looper.java:136)
04-10 01:37:00.520: E/AndroidRuntime(1650): at android.app.ActivityThread.main(ActivityThread.java:5017)
04-10 01:37:00.520: E/AndroidRuntime(1650): at java.lang.reflect.Method.invokeNative(Native Method)
04-10 01:37:00.520: E/AndroidRuntime(1650): at java.lang.reflect.Method.invoke(Method.java:515)
04-10 01:37:00.520: E/AndroidRuntime(1650): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
04-10 01:37:00.520: E/AndroidRuntime(1650): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
04-10 01:37:00.520: E/AndroidRuntime(1650): at dalvik.system.NativeStart.main(Native Method)
04-10 01:37:00.520: E/AndroidRuntime(1650): Caused by: java.lang.NoSuchMethodException: sendMessage [class android.view.View]
04-10 01:37:00.520: E/AndroidRuntime(1650): at java.lang.Class.getConstructorOrMethod(Class.java:472)
04-10 01:37:00.520: E/AndroidRuntime(1650): at java.lang.Class.getMethod(Class.java:857)
04-10 01:37:00.520: E/AndroidRuntime(1650): at android.view.View$1.onClick(View.java:3803)
04-10 01:37:00.520: E/AndroidRuntime(1650): ... 11 more
04-10 01:37:04.820: I/Process(1650): Sending signal. PID: 1650 SIG: 9
I've tried to look at other questions answered similar to mine, but I can't find an answer that seems to help my situation. Can anyone help?
I was struggling with a similar error following the android tutorial
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.myfirstapp, PID: 18300
java.lang.IllegalStateException: Could not find method sendMessage (MainActivity)(View) in a parent or ancestor Context for android:onClick attribute defined on view class android.support.v7.widget.AppCompatButton with id 'button'
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.resolveMethod(AppCompatViewInflater.java:327)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:284)
at android.view.View.performClick(View.java:5197)
at android.view.View$PerformClick.run(View.java:20909)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:5944)
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:1399)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
but in my case the WYSIWYG editor (IntelliJ w/Android Studio plugin) ended up populating the onClick property in activity_main.xml with some extra junk about MainActivity:
android:onClick="sendMessage (MainActivity)"
So I deleted " (MainActivity)" and it stopped crashing. I see this is different from your problem as your xml file already seems correct with android:onClick="sendMessage". But wanted to add it here for any others struggling with what I saw. This was the closest post to the issue I was seeing. I'm just getting started and this was killing me, so hope it helps someone else.
The problem is the onClick property in one of your Button tags:
android:onClick="sendMessage" />
Just make sure you have a method sendMessage(View) in your Activity.
It is because you need to implement implements android.view.View.OnClickListener in your class, therefore add the correct imports i.e. import android.view.View.
Related
I have 2 classes in Java and they are 2 different activities that I'd like to switch with a button click.
The problem is that when I do click the button the app crashes.
my MainActivity extends Activity and my SecondActivity extends Activity AND implements SensorEventListener. So I think this is the problem. My MainActivity CANNOT implement SensorEventListener because I am using an external sensor.
This is my MainActivity
public class MainActivity extends Activity {
//My Code goes here.
//This it the function that gets called from XML's onClick feature when I click my Button
public void gotoPathFinder(View v) {
Intent intent = new Intent(this, pathfinderActivity.class);
startActivity(intent);
}
}
This is my SecondActivity
package ca.concordia.sensortag.minimal;
import android.app.Activity;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.widget.TextView;
public class pathfinderActivity extends Activity implements SensorEventListener {
private SensorManager mSensorManager;
private Sensor mStepCounterSensor;
private Sensor mStepDetectorSensor;
private TextView textView;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = (TextView) findViewById(R.id.textView);
mSensorManager = (SensorManager) getSystemService(this.SENSOR_SERVICE);
mStepCounterSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_STEP_COUNTER);
mStepDetectorSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_STEP_DETECTOR);
}
#Override
public void onSensorChanged(SensorEvent event) {
// Sensor sensor = event.sensor;
// float[] values = event.values;
// int value = -1;
//
// if (values.length > 0) {
// value = (int) values[0];
// }
//
// if (sensor.getType() == Sensor.TYPE_STEP_COUNTER) {
// textView.setText("Step Counter Detected : " + value);
// } else if (sensor.getType() == Sensor.TYPE_STEP_DETECTOR) {
// textView.setText("Step Detector Detected : " + value);
// }
}
#Override
protected void onResume() {
super.onResume();
mSensorManager.registerListener(this, mStepCounterSensor,SensorManager.SENSOR_DELAY_FASTEST);
mSensorManager.registerListener(this, mStepDetectorSensor,SensorManager.SENSOR_DELAY_FASTEST);
}
#Override
protected void onStop() {
super.onStop();
mSensorManager.unregisterListener(this, mStepCounterSensor);
mSensorManager.unregisterListener(this, mStepDetectorSensor);
}
#Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
// TODO Auto-generated method stub
}
}
So I think the app crashes because I'm trying to switch to an activity that implements sensoreventlistener from an activity that doesn't? How can I fix this issue?
LogCat
11-27 20:00:07.312: W/dalvikvm(31178): threadid=1: thread exiting with uncaught exception (group=0x41604ba8)
11-27 20:00:07.312: E/AndroidRuntime(31178): FATAL EXCEPTION: main
11-27 20:00:07.312: E/AndroidRuntime(31178): Process: ca.concordia.sensortag.minimal, PID: 31178
11-27 20:00:07.312: E/AndroidRuntime(31178): java.lang.IllegalStateException: Could not find a method gotoPathFinder(View) in the activity class ca.concordia.sensortag.minimal.pathfinderActivity for onClick handler on view class android.widget.ImageView with id 'pf_button'
11-27 20:00:07.312: E/AndroidRuntime(31178): at android.view.View$1.onClick(View.java:3810)
11-27 20:00:07.312: E/AndroidRuntime(31178): at android.view.View.performClick(View.java:4438)
11-27 20:00:07.312: E/AndroidRuntime(31178): at android.view.View$PerformClick.run(View.java:18422)
11-27 20:00:07.312: E/AndroidRuntime(31178): at android.os.Handler.handleCallback(Handler.java:733)
11-27 20:00:07.312: E/AndroidRuntime(31178): at android.os.Handler.dispatchMessage(Handler.java:95)
11-27 20:00:07.312: E/AndroidRuntime(31178): at android.os.Looper.loop(Looper.java:136)
11-27 20:00:07.312: E/AndroidRuntime(31178): at android.app.ActivityThread.main(ActivityThread.java:5017)
11-27 20:00:07.312: E/AndroidRuntime(31178): at java.lang.reflect.Method.invokeNative(Native Method)
11-27 20:00:07.312: E/AndroidRuntime(31178): at java.lang.reflect.Method.invoke(Method.java:515)
11-27 20:00:07.312: E/AndroidRuntime(31178): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
11-27 20:00:07.312: E/AndroidRuntime(31178): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
11-27 20:00:07.312: E/AndroidRuntime(31178): at dalvik.system.NativeStart.main(Native Method)
11-27 20:00:07.312: E/AndroidRuntime(31178): Caused by: java.lang.NoSuchMethodException: gotoPathFinder [class android.view.View]
11-27 20:00:07.312: E/AndroidRuntime(31178): at java.lang.Class.getConstructorOrMethod(Class.java:472)
11-27 20:00:07.312: E/AndroidRuntime(31178): at java.lang.Class.getMethod(Class.java:857)
11-27 20:00:07.312: E/AndroidRuntime(31178): at android.view.View$1.onClick(View.java:3803)
11-27 20:00:07.312: E/AndroidRuntime(31178): ... 11 more
NOTE: When I click the button it brings me to the SAME activity... and then i click the button again THEN i get an error
MainActivity's XML
<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:background="#drawable/bg"
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="ca.concordia.sensortag.minimal.MainActivity$PlaceholderFragment" >
<ImageView
android:id="#+id/compass"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:src="#drawable/compass" />
<ImageView
android:id="#+id/pointer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:src="#drawable/pointer" />
<TextView
android:id="#+id/degree"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/imageView1"
android:layout_centerHorizontal="true"
android:text="TextView" />
<ImageView
android:id="#+id/pf_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/compass"
android:layout_centerHorizontal="true"
android:layout_marginBottom="20dp"
android:src="#drawable/pf_button"
android:onClick="gotoPathFinder" />
</RelativeLayout>
you dont have the method gotoPathFinder in your pathfinderActivity class, am I right that you have also a view with an onclick event in this class too that calls this method?
you do this in your second class:
setContentView(R.layout.activity_main);
so your second class has same layout like your first class. first time you click the button the acticity changes but layout stays the same then when you click it again there is no method in the current class that the botton onclick can run and switch to the other class
so if you have 2 separate layouts you want to use then switch the lone above with
setContentView(R.layout.other_acticity);
and if its intended then add method with same name to second class
This is your error:
Could not find a method gotoPathFinder(View)
android.widget.ImageView with id 'pf_button'
In your XML, find the ImageView with the id "pf_button".
For that ImageView, get rid of whatever android:onClick is already there, and change it to this:
android:onClick="gotoSecond"
You just had the wrong method name in the XML file...your app was looking for that name, but couldn't find it!
Let me know if that works. It should.
Hi I am new to Android stuff.Right now I'm following the tutorial given in developer.android.com/training/basics/firstapp/starting-activity.html and building my fist app.It was fine until I dealt with single activity.Later I have added second activity.When I click the button in first it should be directed to second activity.But on clicking first activity my app stops suddenly.Please help me.
here's the logcat
06-23 23:10:50.134: E/FragmentManager(437): No view found for id
0x7f05003c (com.example.honey:id/container) for fragment
PlaceholderFragment{43e98050 #0 id=0x7f05003c} 06-23 23:10:50.134:
E/FragmentManager(437): Activity state: 06-23 23:10:50.264:
E/AndroidRuntime(437): FATAL EXCEPTION: main 06-23 23:10:50.264:
E/AndroidRuntime(437): java.lang.RuntimeException: Unable to start
activity
ComponentInfo{com.example.honey/com.example.honey.DisplayMessageActivity}:
java.lang.IllegalArgumentException: No view found for id 0x7f05003c
(com.example.honey:id/container) for fragment
PlaceholderFragment{43e98050 #0 id=0x7f05003c} 06-23 23:10:50.264:
E/AndroidRuntime(437): at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
06-23 23:10:50.264: E/AndroidRuntime(437): at
android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
06-23 23:10:50.264: E/AndroidRuntime(437): at
android.app.ActivityThread.access$2300(ActivityThread.java:125) 06-23
23:10:50.264: E/AndroidRuntime(437): at
android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
06-23 23:10:50.264: E/AndroidRuntime(437): at
android.os.Handler.dispatchMessage(Handler.java:99) 06-23
23:10:50.264: E/AndroidRuntime(437): at
android.os.Looper.loop(Looper.java:123) 06-23 23:10:50.264:
E/AndroidRuntime(437): at
android.app.ActivityThread.main(ActivityThread.java:4627) 06-23
23:10:50.264: E/AndroidRuntime(437): at
java.lang.reflect.Method.invokeNative(Native Method) 06-23
23:10:50.264: E/AndroidRuntime(437): at
java.lang.reflect.Method.invoke(Method.java:521) 06-23 23:10:50.264:
E/AndroidRuntime(437): at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
06-23 23:10:50.264: E/AndroidRuntime(437): at
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 06-23
23:10:50.264: E/AndroidRuntime(437): at
dalvik.system.NativeStart.main(Native Method) 06-23 23:10:50.264:
E/AndroidRuntime(437): Caused by: java.lang.IllegalArgumentException:
No view found for id 0x7f05003c (com.example.honey:id/container) for
fragment PlaceholderFragment{43e98050 #0 id=0x7f05003c} 06-23
23:10:50.264: E/AndroidRuntime(437): at
android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:930)
06-23 23:10:50.264: E/AndroidRuntime(437): at
android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1115)
06-23 23:10:50.264: E/AndroidRuntime(437): at
android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
06-23 23:10:50.264: E/AndroidRuntime(437): at
android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1478)
06-23 23:10:50.264: E/AndroidRuntime(437): at
android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:570)
06-23 23:10:50.264: E/AndroidRuntime(437): at
android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1129)
06-23 23:10:50.264: E/AndroidRuntime(437): at
android.app.Activity.performStart(Activity.java:3781) 06-23
23:10:50.264: E/AndroidRuntime(437): at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2636)
06-23 23:10:50.264: E/AndroidRuntime(437): ... 11 more
1st activity java file
package com.example.honey;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBarActivity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
public class MainActivity extends ActionBarActivity {
static final String EXTRA_MESSAGE = "com.example.honey.MESSAGE";
public void sendMessage(View view){ Intent intent=new
Intent(this,DisplayMessageActivity.class); EditText
editText=(EditText)findViewById(R.id.edit_query); 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);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction().add(R.id.container, new
PlaceholderFragment())
.commit();
}
}
#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);
}
/**
* 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_main, container, false);
return rootView;
}
}
}
}
1st activity fragment_main.xml
For 2nd activity
package com.example.honey;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBarActivity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View; import
android.view.ViewGroup;
import android.widget.TextView;
public class DisplayMessageActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); Intent intent=getIntent();
setContentView(R.layout.activity_display_message);
String message=intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
TextView textView =new TextView(this);
textView.setText(message);
setContentView(textView);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction().add(R.id.container, new PlaceholderFragment()).commit();
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.display_message, 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);
}
/**
* 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;
}
}
}
XML file
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.honey.DisplayMessageActivity$PlaceholderFragment"
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
></TextView> </LinearLayout>
manifest
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.honey.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.honey.DisplayMessageActivity"
android:label="#string/title_activity_display_message"
android:parentActivityName="com.example.first.MainActivity" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.first.MainActivity" />
</activity>
</application>
You're making a silly error in the second activity code due to copy pasting:
DisplayMessageActivity:
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction().add(R.id.container, new PlaceholderFragment()).commit();
}
}
You're trying to add the PlaceholderFragment to the R.id.container, when the content view of the second activity is the TextView, which does NOT have the container, aka the layout specified by R.id.container does not exist in the second activity.
Remove these lines from the second activity and it should work.
You forgot to add the activity to the manifest I think...
<activity
android:name="DisplayMessageActivity"
android:label="#string/..." >
</activity>
Check your XML layout for MainActivity, you specify findViewById(R.id.edit_query) for your EditText which was never declared.
Add the stared line to your fragment_main.xml file
`<EditText
**android:id="#+id/edit_query**
android:layout_width="0dp"
android:layout_height="wrap_content"
android:hint="#string/edit_message"
android:layout_weight="1"/>`
I got problem with my first Android application. Just after running, my app crashes in emulator with "Unfortunately has stopped." message. I checked many threads on Stack and I read pretty much of official documentation and I really do not know what's a cause...
I use AVD with Android 4.4.2, API Level 19. CPU/ABI - ARM. Use Host GPU - checked (without this one emulator works very slowly).
My app should play a song after pressing the button. I would be really grateful for any ideas. Cheers.
Java file:
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.os.Build;
import android.media.MediaPlayer;
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment())
.commit();
}
Button one = (Button) findViewById(R.id.button1);
final MediaPlayer mediaPlayer1 = MediaPlayer.create(this, R.raw.elements6);
one.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mediaPlayer1.start();
}
});
}
#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);
}
/**
* 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_main, container, false);
return rootView;
}
}
}
And XML file:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>
Errors:
05-14 17:29:21.740: D/AndroidRuntime(1145): Shutting down VM
05-14 17:29:21.740: W/dalvikvm(1145): threadid=1: thread exiting with uncaught exception (group=0xb2abcba8)
05-14 17:29:21.750: E/AndroidRuntime(1145): FATAL EXCEPTION: main
05-14 17:29:21.750: E/AndroidRuntime(1145): Process: com.example.beatzlooper, PID: 1145
05-14 17:29:21.750: E/AndroidRuntime(1145): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.beatzlooper/com.example.beatzlooper.MainActivity}: java.lang.NullPointerException
05-14 17:29:21.750: E/AndroidRuntime(1145): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
05-14 17:29:21.750: E/AndroidRuntime(1145): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
05-14 17:29:21.750: E/AndroidRuntime(1145): at android.app.ActivityThread.access$800(ActivityThread.java:135)
05-14 17:29:21.750: E/AndroidRuntime(1145): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
05-14 17:29:21.750: E/AndroidRuntime(1145): at android.os.Handler.dispatchMessage(Handler.java:102)
05-14 17:29:21.750: E/AndroidRuntime(1145): at android.os.Looper.loop(Looper.java:136)
05-14 17:29:21.750: E/AndroidRuntime(1145): at android.app.ActivityThread.main(ActivityThread.java:5017)
05-14 17:29:21.750: E/AndroidRuntime(1145): at java.lang.reflect.Method.invokeNative(Native Method)
05-14 17:29:21.750: E/AndroidRuntime(1145): at java.lang.reflect.Method.invoke(Method.java:515)
05-14 17:29:21.750: E/AndroidRuntime(1145): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
05-14 17:29:21.750: E/AndroidRuntime(1145): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
05-14 17:29:21.750: E/AndroidRuntime(1145): at dalvik.system.NativeStart.main(Native Method)
05-14 17:29:21.750: E/AndroidRuntime(1145): Caused by: java.lang.NullPointerException
05-14 17:29:21.750: E/AndroidRuntime(1145): at com.example.beatzlooper.MainActivity.onCreate(MainActivity.java:31)
05-14 17:29:21.750: E/AndroidRuntime(1145): at android.app.Activity.performCreate(Activity.java:5231)
05-14 17:29:21.750: E/AndroidRuntime(1145): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
05-14 17:29:21.750: E/AndroidRuntime(1145): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
05-14 17:29:21.750: E/AndroidRuntime(1145): ... 11 more
You are working with fragments.
The Fragment cannot be added to the R.id.container View because it doesn't exists.
Just delete the whole if-clause in the onCreate() and also delete the Placeholder Class you don't need it actually.
Comment this code
/*if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment())
.commit();
}*/
I am currently trying to get the basic My First App tutorial of Android (created by google) to work, but I'm having some issues. Currently how the app is made no errors show up in Eclipse, but when I run the app and click the Send button, the app crashes displaying Unfortunately, My First App has stopped.
Looking in the Log cat for the error it states that
java.lang.IllegalStateException: Could not find a method sendMessage(View)
in the activity class com.example.myfirstapp.MainActivity for onClick handler
on view class android.widget.Button.
sendMessage does exist in the MainActivity class, but it is a nested class inside a function PlaceholderFragment. Originally I thought that this was an indexing error so I tried calling sendMessage like a nested class android:onClick="PlaceholderFragment.sendMessage" with no success. I have included the fragment_main and MainActivity class as well as my full logcat error. Thanks for any help.
MainActivity:
package com.example.myfirstapp;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBarActivity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
public class MainActivity extends ActionBarActivity {
public final static String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment())
.commit();
}
}
#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);
}
/**
* 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_main, container, false);
return rootView;
}
/** Called when the user clicks the Send button */
public void sendMessage(View view) {
Intent intent = new Intent(getActivity(), DisplayMessageActivity.class);
EditText editText = (EditText)getActivity(). findViewById(R.id.edit_message);
String message = editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
}
}
}
Fragment Main:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<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" />
</LinearLayout>
LogCat:
04-25 12:04:07.220: D/(1274): HostConnection::get() New Host Connection established 0xb8e4f238, tid 1274
04-25 12:04:07.330: W/EGL_emulation(1274): eglSurfaceAttrib not implemented
04-25 12:04:07.340: D/OpenGLRenderer(1274): Enabling debug mode 0
04-25 12:04:10.620: D/AndroidRuntime(1274): Shutting down VM
04-25 12:04:10.620: W/dalvikvm(1274): threadid=1: thread exiting with uncaught exception (group=0xb2a6bba8)
04-25 12:04:10.750: E/AndroidRuntime(1274): FATAL EXCEPTION: main
04-25 12:04:10.750: E/AndroidRuntime(1274): Process: com.example.myfirstapp, PID: 1274
04-25 12:04:10.750: E/AndroidRuntime(1274): java.lang.IllegalStateException: Could not find a method sendMessage(View) in the activity class com.example.myfirstapp.MainActivity for onClick handler on view class android.widget.Button
04-25 12:04:10.750: E/AndroidRuntime(1274): at android.view.View$1.onClick(View.java:3810)
04-25 12:04:10.750: E/AndroidRuntime(1274): at android.view.View.performClick(View.java:4438)
04-25 12:04:10.750: E/AndroidRuntime(1274): at android.view.View$PerformClick.run(View.java:18422)
04-25 12:04:10.750: E/AndroidRuntime(1274): at android.os.Handler.handleCallback(Handler.java:733)
04-25 12:04:10.750: E/AndroidRuntime(1274): at android.os.Handler.dispatchMessage(Handler.java:95)
04-25 12:04:10.750: E/AndroidRuntime(1274): at android.os.Looper.loop(Looper.java:136)
04-25 12:04:10.750: E/AndroidRuntime(1274): at android.app.ActivityThread.main(ActivityThread.java:5017)
04-25 12:04:10.750: E/AndroidRuntime(1274): at java.lang.reflect.Method.invokeNative(Native Method)
04-25 12:04:10.750: E/AndroidRuntime(1274): at java.lang.reflect.Method.invoke(Method.java:515)
04-25 12:04:10.750: E/AndroidRuntime(1274): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
04-25 12:04:10.750: E/AndroidRuntime(1274): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
04-25 12:04:10.750: E/AndroidRuntime(1274): at dalvik.system.NativeStart.main(Native Method)
04-25 12:04:10.750: E/AndroidRuntime(1274): Caused by: java.lang.NoSuchMethodException: sendMessage [class android.view.View]
04-25 12:04:10.750: E/AndroidRuntime(1274): at java.lang.Class.getConstructorOrMethod(Class.java:472)
04-25 12:04:10.750: E/AndroidRuntime(1274): at java.lang.Class.getMethod(Class.java:857)
04-25 12:04:10.750: E/AndroidRuntime(1274): at android.view.View$1.onClick(View.java:3803)
04-25 12:04:10.750: E/AndroidRuntime(1274): ... 11 more
04-25 12:04:12.720: I/Process(1274): Sending signal. PID: 1274 SIG: 9
It looks like your problem is that the sendMessage function should be included in your MainActivity instead of nested in your fragment.
The debug line:
java.lang.IllegalStateException: Could not find a method sendMessage(View) in the activity class com.example.myfirstapp.MainActivity for onClick handler on view class android.widget.Button
shows that the application is looking in your MainActivity class for this method.
place sendMessage(View view) method inside MainActivity class instead of PlaceholderFragment class
Handling clicklistener from xml is not always a good idea. Set a clicklistener in the fragment for the button and call sendMessage method from onClick method of clicklistener.
First set a ID for your button (Example : sendButton).
Then,
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_main, container, false);
Button b = (Button) rootView.findViewByID(R.id.sendButton);
//Here set the clickListener and call sendMessage method
return rootView;
}
/** Called when the user clicks the Send button */
public void sendMessage(View view) {
Intent intent = new Intent(getActivity(), DisplayMessageActivity.class);
EditText editText = (EditText)getActivity(). findViewById(R.id.edit_message);
String message = editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
}
}
Or implement View.OnClickListener in the fragment then have a case statement like:
#Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.sendButton:
sendMessage();
break;
default:
Log.i(TAG, "Unknown: " + view.getId());
break;
}
}
Do it programmatically:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
Button b = (Button) rootView.findViewById(R.id.my_button);
b.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// handle click here
}
});
return rootView;
}
and insert this into Button section in your main activity layout:
android:id="#+id/my_button"
android:onClick="sendMessage"
button onClick attribute search the corresponding method in the Activity. but you define that method in the Fragment that's why you get the exception
method could not find exception.
Android Recommended way to do handle the onClick for button inside the fragment is
First Assign id to Button
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<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:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/button_send"
/>
</LinearLayout>
your Fragment then register the button with onClick listener
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment implements OnItemClickListener{
public PlaceholderFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
Button b = (Button) rootView.findViewById(R.id.button1);
b.setOnClickListener(this);
return rootView;
}
/** Called when the user clicks the Send button */
public void sendMessage(View view) {
Intent intent = new Intent(getActivity(), DisplayMessageActivity.class);
EditText editText = (EditText)getActivity(). findViewById(R.id.edit_message);
String message = editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
}
public void onClick(View view)
{
sendMessage(view);
}
}
I just started learning Android programming and have run into trouble. I'm using the book "Android Programming The Big Nerd Ranch Guide". My IDE is Eclipse and Genymotion to emulate the app. Here's the logcat:
04-13 00:19:48.065: D/dalvikvm(1256): Late-enabling CheckJNI
04-13 00:19:48.781: D/AndroidRuntime(1256): Shutting down VM
04-13 00:19:48.785: W/dalvikvm(1256): threadid=1: thread exiting with uncaught exception (group=0xa4d8ab20)
04-13 00:19:48.785: E/AndroidRuntime(1256): FATAL EXCEPTION: main
04-13 00:19:48.785: E/AndroidRuntime(1256): Process: com.bignerdranch.android.geoquiz, PID: 1256
04-13 00:19:48.785: E/AndroidRuntime(1256): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.bignerdranch.android.geoquiz/com.bignerdranch.android.geoquiz.QuizActivity}: java.lang.NullPointerException
04-13 00:19:48.785: E/AndroidRuntime(1256): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
04-13 00:19:48.785: E/AndroidRuntime(1256): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
04-13 00:19:48.785: E/AndroidRuntime(1256): at android.app.ActivityThread.access$800(ActivityThread.java:135)
04-13 00:19:48.785: E/AndroidRuntime(1256): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
04-13 00:19:48.785: E/AndroidRuntime(1256): at android.os.Handler.dispatchMessage(Handler.java:102)
04-13 00:19:48.785: E/AndroidRuntime(1256): at android.os.Looper.loop(Looper.java:136)
04-13 00:19:48.785: E/AndroidRuntime(1256): at android.app.ActivityThread.main(ActivityThread.java:5017)
04-13 00:19:48.785: E/AndroidRuntime(1256): at java.lang.reflect.Method.invokeNative(Native Method)
04-13 00:19:48.785: E/AndroidRuntime(1256): at java.lang.reflect.Method.invoke(Method.java:515)
04-13 00:19:48.785: E/AndroidRuntime(1256): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
04-13 00:19:48.785: E/AndroidRuntime(1256): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
04-13 00:19:48.785: E/AndroidRuntime(1256): at dalvik.system.NativeStart.main(Native Method)
04-13 00:19:48.785: E/AndroidRuntime(1256): Caused by: java.lang.NullPointerException
04-13 00:19:48.785: E/AndroidRuntime(1256): at com.bignerdranch.android.geoquiz.QuizActivity.onCreate(QuizActivity.java:30)
04-13 00:19:48.785: E/AndroidRuntime(1256): at android.app.Activity.performCreate(Activity.java:5231)
04-13 00:19:48.785: E/AndroidRuntime(1256): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
04-13 00:19:48.785: E/AndroidRuntime(1256): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
04-13 00:19:48.785: E/AndroidRuntime(1256): ... 11 more
04-13 00:19:56.345: I/Process(1256): Sending signal. PID: 1256 SIG: 9
Here's the fragment_quiz.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="24dp"
android:text="#string/question_text" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:id="#+id/true_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/true_button" />
<Button
android:id="#+id/false_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/false_button" />
</LinearLayout>
</LinearLayout>
The QuizActivity.java file (most of the code imported by Eclipse):
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBarActivity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;
public class QuizActivity extends ActionBarActivity {
private Button mTrueButton;
private Button mFalseButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_quiz);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
mTrueButton = (Button) findViewById(R.id.true_button);
mTrueButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(QuizActivity.this, R.string.incorrect_toast, Toast.LENGTH_SHORT)
.show();
}
});
mFalseButton = (Button) findViewById(R.id.false_button);
mFalseButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(QuizActivity.this, R.string.correct_toast, Toast.LENGTH_SHORT)
.show();
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.quiz, 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);
}
/**
* 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_quiz, container,
false);
return rootView;
}
}
}
What am I doing wrong?
Put your layout definitions in the PlaceholderFragment class. For the findViewById, use rootView.findViewById. For the context, use getActivity().
Why does this work? There are actually 2 independent layouts shown as one in your screen. The master one shows everything, including a fragment. A Fragment is basically a self-contained mini activity, very useful in Tablet development, and mildly useful even just for a phone type device. Your layouts are in the fragment_quiz xml file, which is only inflated inside the fragment, in the onCreateView statement. So long as you use the findViewById after it's been inflated, you'll be fine.