Activity Recreates and blinking - java

I have FirstRunActivity and MainActivity (launcher). When app opening and if app opened for the first time, i start FirstRunActivity (with clearing activity history).
It's working ok. But when turning on autorotation in device, then opening app with rotated device, screen is blinking. In log i can see that activity is recreates itself in loop.
Log :
3028-3028/tm.mobile.payment D/SazHyzmat﹕ FirstRunActivity onPause
04-17 22:49:00.390 3028-3028/tm.mobile.payment D/SazHyzmat﹕ FirstRunActivity onStop
04-17 22:49:00.410 3028-3028/tm.mobile.payment D/SazHyzmat﹕ FirstRunActivity onStart
04-17 22:49:00.410 3028-3028/tm.mobile.payment D/SazHyzmat﹕ FirstRunActivity onResume
04-17 22:49:00.480 3028-3028/tm.mobile.payment D/SazHyzmat﹕ FirstRunActivity onPause
04-17 22:49:00.480 3028-3028/tm.mobile.payment D/SazHyzmat﹕ FirstRunActivity onStop
04-17 22:49:00.520 3028-3028/tm.mobile.payment D/SazHyzmat﹕ FirstRunActivity onStart
04-17 22:49:00.520 3028-3028/tm.mobile.payment D/SazHyzmat﹕ FirstRunActivity onResume
04-17 22:49:00.630 3028-3028/tm.mobile.payment D/SazHyzmat﹕ FirstRunActivity onPause
04-17 22:49:00.630 3028-3028/tm.mobile.payment D/SazHyzmat﹕ FirstRunActivity onStop
04-17 22:49:00.680 3028-3028/tm.mobile.payment D/SazHyzmat﹕ FirstRunActivity onStart
04-17 22:49:00.680 3028-3028/tm.mobile.payment D/SazHyzmat﹕ FirstRunActivity onResume
04-17 22:49:00.800 3028-3028/tm.mobile.payment D/SazHyzmat﹕ FirstRunActivity onPause
04-17 22:49:00.810 3028-3028/tm.mobile.payment D/SazHyzmat﹕ FirstRunActivity onStop
04-17 22:49:00.870 3028-3028/tm.mobile.payment D/SazHyzmat﹕ FirstRunActivity onStart
04-17 22:49:00.870 3028-3028/tm.mobile.payment D/SazHyzmat﹕ FirstRunActivity onResume
04-17 22:49:00.960 3028-3028/tm.mobile.payment D/SazHyzmat﹕ FirstRunActivity onPause
04-17 22:49:00.970 3028-3028/tm.mobile.payment D/SazHyzmat﹕ FirstRunActivity onStop
04-17 22:49:00.990 3028-3028/tm.mobile.payment D/SazHyzmat﹕ FirstRunActivity onStart
04-17 22:49:00.990 3028-3028/tm.mobile.payment D/SazHyzmat﹕ FirstRunActivity onResume
04-17 22:49:01.060 3028-3028/tm.mobile.payment D/SazHyzmat﹕ FirstRunActivity onPause
04-17 22:49:01.060 3028-3028/tm.mobile.payment D/SazHyzmat﹕ FirstRunActivity onStop
04-17 22:49:01.080 3028-3028/tm.mobile.payment D/SazHyzmat﹕ FirstRunActivity onStart
04-17 22:49:01.080 3028-3028/tm.mobile.payment D/SazHyzmat﹕ FirstRunActivity onResume
04-17 22:49:01.150 3028-3028/tm.mobile.payment D/SazHyzmat﹕ FirstRunActivity onPause
04-17 22:49:01.150 3028-3028/tm.mobile.payment D/SazHyzmat﹕ FirstRunActivity onStop
04-17 22:49:01.180 3028-3028/tm.mobile.payment D/SazHyzmat﹕ FirstRunActivity onStart
04-17 22:49:01.180 3028-3028/tm.mobile.payment D/SazHyzmat﹕ FirstRunActivity onResume
04-17 22:49:01.250 3028-3028/tm.mobile.payment D/SazHyzmat﹕ FirstRunActivity onPause
04-17 22:49:01.250 3028-3028/tm.mobile.payment D/SazHyzmat﹕ FirstRunActivity onStop
04-17 22:49:01.280 3028-3028/tm.mobile.payment D/SazHyzmat﹕ FirstRunActivity onStart
04-17 22:49:01.280 3028-3028/tm.mobile.payment D/SazHyzmat﹕ FirstRunActivity onResume
04-17 22:49:01.430 3028-3028/tm.mobile.payment D/SazHyzmat﹕ FirstRunActivity onPause
FirstRunActivity.java :
public class FirstRunActivity extends ActionBarActivity {
#InjectView(R.id.password) EditText passwordView;
#InjectView(R.id.password_retype) EditText passwordRetypeView;
#InjectView(R.id.save) View saveButton;
#Override
protected void onCreate( Bundle savedInstanceState ) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_first_run);
ButterKnife.inject(this);
}
#Override protected void onStart() {
super.onStart();
U.l("FirstRunActivity onStart");
}
#Override protected void onStop() {
super.onStart();
U.l("FirstRunActivity onStop");
}
#Override protected void onPause() {
super.onStart();
U.l("FirstRunActivity onPause");
}
#Override protected void onResume() {
super.onStart();
U.l("FirstRunActivity onResume");
}
}
MainActivity.java :
#Override
protected void onCreate( Bundle savedInstanceState ) {
super.onCreate(savedInstanceState);
settingsManager = SettingsManager.getInstance(this);
//If app not initialized
if (! settingsManager.isAppInitialized()) {
Intent intent = new Intent(this, FirstRunActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
ComponentName cn = intent.getComponent();
Intent mainIntent = IntentCompat.makeRestartActivityTask(cn);
startActivity(mainIntent);
finish();
} else {
//Other code
}
}
If i change code that starting first run activity (in MainActivity.java) with just starting activity, activity is not blinking, but when pressing back button it will open MainActivity with white screen.
Or clearing activity history with other way like here: Clear the entire history stack and start a new activity on Android screen is blinking anyway.
So how can i stop blinking and clearing activity history?
EDIT
After tests i have found that in my other activities, that have no connection with above code, have same problem, blinking. When opening activity with rotated device.

Looking at the documentation for IntentCompat.makeRestartActivityTask, it sounds like it creates an intent that serves to restart your current activity. I recommend removing these two lines:
ComponentName cn = intent.getComponent();
Intent mainIntent = IntentCompat.makeRestartActivityTask(cn);
I've never needed them while changing activities.
Additionally, the call to mContext.finish() can be accomplished with just finish() and can never be null in that case, most likely the reason that you were seeing the white screen was that mContext was null, so you never successfully finished that activity.

You need to define the Activity in the manifest with a NoDisplay Theme. Then, start FirstRunActivity or LoginActivity (or whatever). The IntentCompat is not needed unless you support versions below HoneyComb, where you can use FLAG_ACTIVITY_CLEAR_TASK
See https://stackoverflow.com/a/4892712/218473

Related

YouTube Android Player API - ExceptionInInitializerError

Note: I'm using the YouTube Android Player API
Expected behavior:
The activity remains in portrait mode when the video is not fullscreen (Enforced by AndroidManifest ok).
The activity is set to landscape orientation when the video enters fullscreen mode (ok).
The activity returns to portrait orientation when the user exits fullscreen mode (ExceptionInInitializerError occurs here).
See the problem in action here
YouTubeFailureRecoveryActivity.java (This is included in the library under sample/src/com/examples/youtubeapidemo)
Main Activity
package test.testapp;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import com.google.android.youtube.player.YouTubePlayer;
import com.google.android.youtube.player.YouTubePlayerView;
public class MainActivity extends YouTubeFailureRecoveryActivity implements YouTubePlayer.OnFullscreenListener {
static final String API_KEY = "PLACE YOUTUBE DATA API KEY HERE";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
YouTubePlayerView youTubeView = (YouTubePlayerView) findViewById(R.id.youtube_view);
youTubeView.initialize(API_KEY, this);
}
#Override
public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer youTubePlayer, boolean wasRestored) {
youTubePlayer.setOnFullscreenListener(this);
youTubePlayer.setFullscreenControlFlags(YouTubePlayer.FULLSCREEN_FLAG_CONTROL_SYSTEM_UI);
if(!wasRestored){
youTubePlayer.cueVideo("wKJ9KzGQq0w");
}
}
protected YouTubePlayer.Provider getYouTubePlayerProvider() {
return (YouTubePlayerView) findViewById(R.id.youtube_view);
}
#Override
public void onFullscreen(boolean isFullscreen) {
if(isFullscreen){
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}else if(!isFullscreen){
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
}
}
activity_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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="test.testapp.MainActivity">
<com.google.android.youtube.player.YouTubePlayerView
android:id="#+id/youtube_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</com.google.android.youtube.player.YouTubePlayerView>
</LinearLayout>
Stack trace
06-10 09:47:09.205 2646-2646/test.testapp E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.ExceptionInInitializerError
at android.support.v7.widget.RecyclerView.onSaveInstanceState(SourceFile:201)
at android.view.View.dispatchSaveInstanceState(View.java:11839)
at android.view.ViewGroup.dispatchFreezeSelfOnly(ViewGroup.java:2576)
at android.support.v7.widget.RecyclerView.dispatchSaveInstanceState(SourceFile:220)
at android.view.ViewGroup.dispatchSaveInstanceState(ViewGroup.java:2562)
at android.view.ViewGroup.dispatchSaveInstanceState(ViewGroup.java:2562)
at android.view.ViewGroup.dispatchSaveInstanceState(ViewGroup.java:2562)
at android.view.ViewGroup.dispatchSaveInstanceState(ViewGroup.java:2562)
at android.view.ViewGroup.dispatchSaveInstanceState(ViewGroup.java:2562)
at android.view.ViewGroup.dispatchSaveInstanceState(ViewGroup.java:2562)
at android.view.ViewGroup.dispatchSaveInstanceState(ViewGroup.java:2562)
at android.view.View.saveHierarchyState(View.java:11822)
at com.android.internal.policy.impl.PhoneWindow.saveHierarchyState(PhoneWindow.java:1566)
at android.app.Activity.onSaveInstanceState(Activity.java:1188)
at com.google.android.youtube.player.YouTubeBaseActivity.onSaveInstanceState(Unknown Source)
at android.app.Activity.performSaveInstanceState(Activity.java:1137)
at android.app.Instrumentation.callActivityOnSaveInstanceState(Instrumentation.java:1215)
at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3486)
at android.app.ActivityThread.access$700(ActivityThread.java:130)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1201)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4745)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NoClassDefFoundError: rt
at rs.<clinit>(SourceFile:17)
at android.support.v7.widget.RecyclerView.onSaveInstanceState(SourceFile:201) 
at android.view.View.dispatchSaveInstanceState(View.java:11839) 
at android.view.ViewGroup.dispatchFreezeSelfOnly(ViewGroup.java:2576) 
at android.support.v7.widget.RecyclerView.dispatchSaveInstanceState(SourceFile:220) 
at android.view.ViewGroup.dispatchSaveInstanceState(ViewGroup.java:2562) 
at android.view.ViewGroup.dispatchSaveInstanceState(ViewGroup.java:2562) 
at android.view.ViewGroup.dispatchSaveInstanceState(ViewGroup.java:2562) 
at android.view.ViewGroup.dispatchSaveInstanceState(ViewGroup.java:2562) 
at android.view.ViewGroup.dispatchSaveInstanceState(ViewGroup.java:2562) 
at android.view.ViewGroup.dispatchSaveInstanceState(ViewGroup.java:2562) 
at android.view.ViewGroup.dispatchSaveInstanceState(ViewGroup.java:2562) 
at android.view.View.saveHierarchyState(View.java:11822) 
at com.android.internal.policy.impl.PhoneWindow.saveHierarchyState(PhoneWindow.java:1566) 
at android.app.Activity.onSaveInstanceState(Activity.java:1188) 
at com.google.android.youtube.player.YouTubeBaseActivity.onSaveInstanceState(Unknown Source) 
at android.app.Activity.performSaveInstanceState(Activity.java:1137) 
at android.app.Instrumentation.callActivityOnSaveInstanceState(Instrumentation.java:1215) 
at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3486) 
at android.app.ActivityThread.access$700(ActivityThread.java:130) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1201) 
at android.os.Handler.dispatchMessage(Handler.java:99) 
at android.os.Looper.loop(Looper.java:137) 
at android.app.ActivityThread.main(ActivityThread.java:4745) 
at java.lang.reflect.Method.invokeNative(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:511) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 
at dalvik.system.NativeStart.main(Native Method) 
Looks like the application is crashing when restarting because of a configuration change
https://developer.android.com/guide/topics/resources/runtime-changes.html
You can disable the automatic activity recreation for orientation changes, and handle it yourself.
To do that, add
android:configChanges="orientation|screenLayout|screenSize"
With orientation alone it probably works, but the other ones I think were triggered also on some devices.
You can then override onConfigurationChanged() to do stuff on an orientation change if you need it, like showing / hiding views, etc.. as Android will not recreate your layouts automatically (that's what it does when restarts the activity on an orientation change)
So you can do:
#Override
public void onConfigurationChanged(final Configuration newConfiguration) {
Log.wtf(
"Orientation",
newConfiguration.orientation == Configuration.ORIENTATION_PORTRAIT
? "portrait"
: "landscape"
);
}

Why is my app crashing when I set DisplayHomeAsUpEnabled to true?

Each time I run this activity my app crashes right away. The app runs fine if I remove:
ActionBar actionBar = getActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
Here's my activity's class code:
public class WebActivity extends AppCompatActivity {
private WebView mWebView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_web);
mWebView = (WebView) findViewById(R.id.activity_main_webview);
WebSettings webSettings = mWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
mWebView.loadUrl("http://www.example.com/");
mWebView.setWebViewClient(new MyAppWebViewClient());
ActionBar actionBar = getActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
}
public boolean onOptionsItemSelected(MenuItem item){
super.onBackPressed();
return true;
}
}
Edit:
I checked my logcat and this was the error message:
08-03 09:06:53.952 4650-4650/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: ca.davesautoservice.davesautoservice, PID: 4650
java.lang.RuntimeException: Unable to start activity ComponentInfo{ca.davesautoservice.davesautoservice/ca.davesautoservice.davesautoservice.WebActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2377)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2429)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1342)
at android.os.Handler.dispatchMessage(Handler.java:110)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:5333)
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 ca.davesautoservice.davesautoservice.WebActivity.onCreate(WebActivity.java:24)
at android.app.Activity.performCreate(Activity.java:5343)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1088)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2331)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2429) 
at android.app.ActivityThread.access$800(ActivityThread.java:151) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1342) 
at android.os.Handler.dispatchMessage(Handler.java:110) 
at android.os.Looper.loop(Looper.java:193) 
at android.app.ActivityThread.main(ActivityThread.java:5333) 
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)
I personally don't see anything particularly interesting here. The only this I notice is that when it says:
Caused by: java.lang.NullPointerException
at ca.davesautoservice.davesautoservice.WebActivity.onCreate(WebActivity.java:24)
It confirms that it is the line initiating the actionbar that is the problem.
Try to import android.support.v7.app.ActionBar
and use:
mActionBar = getSupportActionBar();
Make sure that you have minimum api level in your manifest file is above 11. Because the package android.support.v7.app.ActionBar is only supported in api level below 11. Go to manifest and change API version.
Because used AppCompatActivity, if you want to use ActionBar, you should use this way:
mActionBar = getSupportActionBar();

Android - Switching activity with different implementation

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.

Could not find a method sendMessage(View) in the activity class

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.

How do I create custom classes as subroutines

I may have my terms mixed up, but I'm creating an android app and I want to encapsulate some of the routine functions. For instance my actionBar. At first I had the code on all my activities and if I change one thing I have to change it else where. I want to create a NavigationActionBarManager.java file to handle the inital setup, onNavigationListener, setListNavigationCallbacks, etc.
Here's the class so far:
import android.app.ActionBar;
import android.app.ActionBar.OnNavigationListener;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.widget.ArrayAdapter;
import android.widget.SpinnerAdapter;
public class NavigationActionBarManager extends Activity {
public ActionBar actionBar = getActionBar(); // actionbar object
// METHOD: display
public void display() {
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
}
// METHOD: inflate
public void inflate(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_adventurers_new, menu);
}
// METHOD: listen
public void listen() {
SpinnerAdapter mSpinnerAdapter = ArrayAdapter.createFromResource(this, R.array.array_character_views, android.R.layout.simple_spinner_dropdown_item);
OnNavigationListener mOnNavigationListener = new OnNavigationListener() {
//String[] strings = getResources().getStringArray(R.array.array_character_views);
#Override
public boolean onNavigationItemSelected(int position, long itemId) {
Intent nextScreen = null;
switch(position) {
case 0:
break;
case 1:
nextScreen = new Intent(getApplicationContext(), AdventurersNewAbilitiesActivity.class);
break;
case 2:
break;
}
if(nextScreen != null) {
startActivity(nextScreen);
}
return false;
}
};
actionBar.setListNavigationCallbacks(mSpinnerAdapter, mOnNavigationListener);
}
}
Back in my activity class, I want to apply it as such:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_adventurer_new_character);
navBar.display();
navBar.listen();
}
I'm running into an error and I don't understand LogCat's output. Here's the LogCat ouput:
04-17 23:12:01.110: E/AndroidRuntime(14013): FATAL EXCEPTION: main
04-17 23:12:01.110: E/AndroidRuntime(14013): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.app/com.app.AdventurersNewCharacterActivity}: java.lang.NullPointerException
04-17 23:12:01.110: E/AndroidRuntime(14013): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1903)
04-17 23:12:01.110: E/AndroidRuntime(14013): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2004)
04-17 23:12:01.110: E/AndroidRuntime(14013): at android.app.ActivityThread.access$600(ActivityThread.java:132)
04-17 23:12:01.110: E/AndroidRuntime(14013): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1157)
04-17 23:12:01.110: E/AndroidRuntime(14013): at android.os.Handler.dispatchMessage(Handler.java:99)
04-17 23:12:01.110: E/AndroidRuntime(14013): at android.os.Looper.loop(Looper.java:137)
04-17 23:12:01.110: E/AndroidRuntime(14013): at android.app.ActivityThread.main(ActivityThread.java:4580)
04-17 23:12:01.110: E/AndroidRuntime(14013): at java.lang.reflect.Method.invokeNative(Native Method)
04-17 23:12:01.110: E/AndroidRuntime(14013): at java.lang.reflect.Method.invoke(Method.java:511)
04-17 23:12:01.110: E/AndroidRuntime(14013): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
04-17 23:12:01.110: E/AndroidRuntime(14013): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:558)
04-17 23:12:01.110: E/AndroidRuntime(14013): at dalvik.system.NativeStart.main(Native Method)
04-17 23:12:01.110: E/AndroidRuntime(14013): Caused by: java.lang.NullPointerException
04-17 23:12:01.110: E/AndroidRuntime(14013): at android.app.Activity.initActionBar(Activity.java:2071)
04-17 23:12:01.110: E/AndroidRuntime(14013): at android.app.Activity.getActionBar(Activity.java:2058)
04-17 23:12:01.110: E/AndroidRuntime(14013): at com.app.NavigationActionBarManager.<init>(NavigationActionBarManager.java:13)
04-17 23:12:01.110: E/AndroidRuntime(14013): at com.app.AdventurersNewCharacterActivity.<init>(AdventurersNewCharacterActivity.java:13)
04-17 23:12:01.110: E/AndroidRuntime(14013): at java.lang.Class.newInstanceImpl(Native Method)
04-17 23:12:01.110: E/AndroidRuntime(14013): at java.lang.Class.newInstance(Class.java:1319)
04-17 23:12:01.110: E/AndroidRuntime(14013): at android.app.Instrumentation.newActivity(Instrumentation.java:1025)
04-17 23:12:01.110: E/AndroidRuntime(14013): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1894)
04-17 23:12:01.110: E/AndroidRuntime(14013): ... 11 more
Have you implemented the onCreateOptionsMenu() method in your activity?
doc available at https://developer.android.com/guide/topics/ui/actionbar.html

Categories