My app crashes after clicking on the button when I call invalidate() to refresh the view. I can't figure out why. If I delete canvasView.invalidate(), the app does not crash. Is it allowed to call invalidate() from another class? I'm new to Android Studio. Surely, somebody can help me?
EDIT: the problem is solved, the answer is below, and I added the LogCat.
Here is the code:
public class MainActivity extends AppCompatActivity implements
View.OnClickListener {
CanvasView canvasView;
Button btnChangeColor;
static boolean colorRed;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
CanvasView canvasView = new CanvasView(this, null);
btnChangeColor = (Button) findViewById(R.id.button);
btnChangeColor.setOnClickListener(this);
colorRed = false;
}
#Override
public void onClick(View v) {
if(colorRed) {
colorRed = false;
canvasView.invalidate();
return;
}
else {
colorRed = true;
canvasView.invalidate();
return;
}
}
}
class CanvasView extends View {
Paint paint;
public CanvasView(Context context, #Nullable AttributeSet attrs) {
super(context, attrs);
paint = new Paint();
}
#Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
if(MainActivity.colorRed == true)
paint.setColor(Color.RED);
else
paint.setColor(Color.BLACK);
paint.setStyle(Paint.Style.FILL);
canvas.drawCircle(400, 400, 50, paint);
}
}
LogCat:
05-31 21:10:41.341 27902-27902/? I/art: Late-enabling -Xcheck:jni
Reinit property: dalvik.vm.checkjni= false
05-31 21:10:41.351 27902-27909/? I/art: Debugger is no longer active
Starting a blocking GC Instrumentation
05-31 21:10:41.425 27902-27902/? W/System: ClassLoader referenced unknown path: /data/app/com.example.andreas.drawing_exp-2/lib/arm64
05-31 21:10:41.433 27902-27902/? I/InstantRun: starting instant run server: is main process
05-31 21:10:41.450 27902-27902/? I/HwCust: Constructor found for class android.app.HwCustActivityImpl
05-31 21:10:41.465 27902-27902/? I/HwCust: Constructor found for class android.app.HwCustHwWallpaperManagerImpl
05-31 21:10:41.477 27902-27902/? W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
05-31 21:10:41.533 27902-27913/? I/art: Background sticky concurrent mark sweep GC freed 2801(763KB) AllocSpace objects, 0(0B) LOS objects, 22% free, 3MB/4MB, paused 5.329ms total 13.242ms
05-31 21:10:41.567 27902-27902/? W/VRSystemServiceManager: vr service is not alive
05-31 21:10:41.568 27902-27902/? I/HwSecImmHelper: mSecurityInputMethodService is null
05-31 21:10:41.571 27902-27902/? I/HwPointEventFilter: support AFT
05-31 21:10:41.615 27902-27922/? I/OpenGLRenderer: Initialized EGL, version 1.4
05-31 21:10:41.621 27902-27922/? W/linker: /vendor/lib64/libhwuibp.so: unused DT entry: type 0xf arg 0xe3a
05-31 21:10:41.629 27902-27902/? W/art: Before Android 4.1, method int android.support.v7.widget.DropDownListView.lookForSelectablePosition(int, boolean) would have incorrectly overridden the package-private method in android.widget.ListView
05-31 21:10:48.728 27902-27902/com.example.andreas.drawing_exp I/hwaps: JNI_OnLoad
05-31 21:10:48.802 27902-27902/com.example.andreas.drawing_exp E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.andreas.drawing_exp, PID: 27902
java.lang.NullPointerException: Attempt to read from field 'boolean com.example.andreas.drawing_exp.CanvasView.colorRed' on a null object reference
at com.example.andreas.drawing_exp.MainActivity.onClick(MainActivity.java:37)
at android.view.View.performClick(View.java:5646)
at android.view.View$PerformClick.run(View.java:22473)
at android.os.Handler.handleCallback(Handler.java:761)
at android.os.Handler.dispatchMessage(Handler.java:98)
at android.os.Looper.loop(Looper.java:156)
at android.app.ActivityThread.main(ActivityThread.java:6523)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:942)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:832)
05-31 21:10:48.837 27902-27902/com.example.andreas.drawing_exp I/Process: Sending signal. PID: 27902 SIG: 9
You are using invalidate() incorrectly.
This must be called from a UI thread. To call from a non-UI thread, call postInvalidate().
You use postInvalidate() to do this.
I found out why my app crashes after watching a few tutorials on how to draw in a custom view in YouTube.
It is because I made the initialisation of my canvas view class the wrong way. I did not add an ID for the view and did not initialize with 'findViewById', instead I used the constructor of the class. But anyway, thanks for your answers!
Here is how the changed code looks like (short excerpt of original code):
public class MainActivity extends AppCompatActivity implements
View.OnClickListener{
CanvasView canvasView;
Button btnChangeColor;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
canvasView = (CanvasView) findViewById(R.id.canvasview);
btnChangeColor = (Button) findViewById(R.id.button);
btnChangeColor.setOnClickListener(this);
canvasView.colorRed = false;
}
Related
I theoretically have 2 related questions.
1) I tried to make a night mode for my app. That works so far pretty good. Then I wanted to put the setting the User chooses to SharedPreferences. In my first try, It just didn't work. All I did was getting red and causing errors. After a lot of research on the Internet, I found another idea. Now the app is starting but crashing immediately again. The error has to be in the SharedPreferences because as soon as I delete this part it is working again. I just can't find the error and it doesn't show me a red area or so either.
2) Just to check if my research are right. Is it true that it is not possible anymore to make a time-related Night mode? Means dark at night, light at day? I found out that MODE_NIGHT_AUTO_TIME was supposed to do that but is deprecated and they suggest to use MODE_NIGHT_AUTO_BATTERY which is kinda something really different.
btw. here is my code if it helps from the Setting:
public class Settings extends AppCompatActivity {
private static final String TAG = "SettingsActivity";
private RelativeLayout layout;
private SharedPreferences preferences;
private Bundle savedInstanceState;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_settings);
assert getSupportActionBar() != null;
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
Spinner spinner = findViewById(DarkMode);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Log.e(TAG, "onItemSelected: " + position);
handleNightMode(position);
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
Log.e(TAG, "onNothingSelected: ");
}
});
}
private void handleNightMode(int position) {
switch (position) {
case 0:
Log.e(TAG, "Nothing Selected");
break;
case 1:
Log.e(TAG, "FOLLLOW_SYSTEM");
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM);
getDelegate().applyDayNight();
break;
case 2:
Log.e(TAG, "YES");
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
getDelegate().applyDayNight();
break;
case 3:
Log.e(TAG, "NO");
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
getDelegate().applyDayNight();
break;
case 4:
Log.e(TAG, "AUTO");
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_AUTO_BATTERY);
getDelegate().applyDayNight();
break;
default:
Log.e(TAG, "FOLLLOW_SYSTEM");
break;
}
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
String NightMode = preferences.getString("prefTheme", "NO");
if (NightMode.equals("YES"))
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
if (NightMode.equals("NO"))
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_AUTO_BATTERY);
else if (NightMode.equals("AUTO"))
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_AUTO_BATTERY);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
the MainActivity:
public class MainActivity extends AppCompatActivity {
int currentDayNight;
private Button settings;
private RelativeLayout layout;
private SharedPreferences preferences;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
layout = findViewById(R.id.layout);
settings = findViewById(R.id.btn_settings);
currentDayNight = AppCompatDelegate.getDefaultNightMode();
settings.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
openSettings();
}
});
}
private void openSettings() {
Intent intent = new Intent(this, Settings.class);
startActivity(intent);
}
}
logcat:
04-16 21:31:59.861 11136-11136/? I/art: Not late-enabling -Xcheck:jni (already on)
04-16 21:32:00.033 11136-11136/com.goldenegg.darkmode W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter androidx.vectordrawable.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
04-16 21:32:00.086 11136-11136/com.goldenegg.darkmode I/art: Rejecting re-init on previously-failed class java.lang.Class<androidx.core.view.ViewCompat$2>
04-16 21:32:00.087 11136-11136/com.goldenegg.darkmode I/art: Rejecting re-init on previously-failed class java.lang.Class<androidx.core.view.ViewCompat$2>
04-16 21:32:00.168 11136-11149/com.goldenegg.darkmode I/art: Background partial concurrent mark sweep GC freed 523(79KB) AllocSpace objects, 0(0B) LOS objects, 50% free, 1007KB/2031KB, paused 6.320ms total 27.683ms
04-16 21:32:00.204 11136-11136/com.goldenegg.darkmode D/AndroidRuntime: Shutting down VM
--------- beginning of crash
04-16 21:32:00.205 11136-11136/com.goldenegg.darkmode E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.goldenegg.darkmode, PID: 11136
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.goldenegg.darkmode/com.goldenegg.darkmode.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at com.goldenegg.darkmode.MainActivity.onCreate(MainActivity.java:34)
at android.app.Activity.performCreate(Activity.java:5990)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Thanks for your Help.
have a great day
I am using text to speech engine in my App. It works fine on emulator Nexus 6 with API 23 and higher. But on emulator Nexus 6 with API 22 it does not speak.
Both emulators use Pico TTS as preferred engine.
My activity layout contains only one button "Speak".
This is my activity code:
public class MainActivity extends AppCompatActivity {
private TextToSpeech mTTS;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button speakBtn = findViewById(R.id.button);
speakBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
speak();
}
});
mTTS = new TextToSpeech(this, new TextToSpeech.OnInitListener() {
#Override
public void onInit(int status) {
if (status == TextToSpeech.SUCCESS) {
int result = mTTS.setLanguage(Locale.US);
if (result == TextToSpeech.LANG_MISSING_DATA
|| result == TextToSpeech.LANG_NOT_SUPPORTED) {
Log.e("TTS", "Language not supported");
}
mTTS.setOnUtteranceProgressListener(new UtteranceProgressListener() {
#Override
public void onStart(String utteranceId) {
Log.d("TTS", "onStart called, utteranceId = " + utteranceId);
}
#Override
public void onDone(String utteranceId) {
Log.d("TTS", "onDone called, utteranceId = " + utteranceId);
}
#Override
public void onError(String utteranceId) {
Log.d("TTS", "onError called, utteranceId = " + utteranceId);
}
});
} else {
Log.e("TTS", "Initialization failed");
}
}
});
}
private void speak() {
Log.d("TTS", "speak() method called");
HashMap<String, String> map = new HashMap<>();
map.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, "greeting");
mTTS.speak("hello", TextToSpeech.QUEUE_FLUSH, map);
}
}
This is all logs from emulator Nexus 6 API 22:
02-18 13:54:09.942 9739-9739/? E/libprocessgroup: failed to make and chown /acct/uid_10059: Read-only file system
02-18 13:54:09.943 9739-9739/? W/Zygote: createProcessGroup failed, kernel missing CONFIG_CGROUP_CPUACCT?
02-18 13:54:09.943 9739-9739/? I/art: Not late-enabling -Xcheck:jni (already on)
02-18 13:54:09.961 9739-9748/? E/art: Failed sending reply to debugger: Broken pipe
02-18 13:54:09.961 9739-9748/? I/art: Debugger is no longer active
02-18 13:54:09.992 9739-9739/? W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
02-18 13:54:10.001 9739-9739/? I/art: Rejecting re-init on previously-failed class java.lang.Class
02-18 13:54:10.001 9739-9739/? I/art: Rejecting re-init on previously-failed class java.lang.Class
02-18 13:54:10.064 9739-9739/? I/TextToSpeech: Sucessfully bound to com.svox.pico
02-18 13:54:10.072 9739-9757/? D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true
02-18 13:54:10.074 9739-9739/? D/Atlas: Validating map...
02-18 13:54:10.093 9739-9739/? I/TextToSpeech: Connected to ComponentInfo{com.svox.pico/com.svox.pico.PicoService}
02-18 13:54:10.096 9739-9758/? I/TextToSpeech: Set up connection to ComponentInfo{com.svox.pico/com.svox.pico.PicoService}
02-18 13:54:10.111 9739-9757/? I/OpenGLRenderer: Initialized EGL, version 1.4
02-18 13:54:10.111 9739-9757/? W/OpenGLRenderer: Failed to choose config with EGL_SWAP_BEHAVIOR_PRESERVED, retrying without...
02-18 13:54:10.128 9739-9757/? D/EGL_emulation: eglCreateContext: 0xae434e20: maj 2 min 0 rcv 2
02-18 13:54:10.131 9739-9757/? D/EGL_emulation: eglMakeCurrent: 0xae434e20: ver 2 0
02-18 13:54:10.134 9739-9757/? D/OpenGLRenderer: Enabling debug mode 0
02-18 13:54:10.174 9739-9757/? D/EGL_emulation: eglMakeCurrent: 0xae434e20: ver 2 0
02-19 09:32:25.570 9739-9739/com.example.ttsapp D/TTS: speak() method called
Figured out how to solve the problem. Everything works if launch the emulator not via Debugging or Execution but from AVD Manager after starting Android Studio.
I am quite new to android development and I am facing a NullPointerException when I am trying to call an AsyncTask which is an inner class in another class.
I believe it is the way I am instantiating it but this is what I have:
// This onClick is in my adapter class - which is a separate Java File and class
#Override
public void onClick(View v) {
UploadRes uploadRes = new UploadRes();
UploadRes.UploadResConfirm uploadResConfirm = uploadRes.new UploadResConfirm(v.getContext());
uploadResConfirm.execute(fileName, filePath);
}
public class UploadResConfirm extends AsyncTask<String, String, String> {
Dialog dialog;
Context context;
public UploadResConfirm(Context context){
this.context = context;
}
#Override
protected void onPreExecute(){
dialog = new Dialog(UploadRes.this);
dialog.setTitle("Currently uploading");
dialog.show();
}
I believe it has got something to do with the dialog box itself being instantiated in the AsyncTask class.
The error stack trace on Logcat - its the Dialog, I think its in the wrong place...
java.lang.NullPointerException
at codeman.androapp.UploadRes$UploadResConfirm.onPreExecute(UploadRes.java:246)
at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:586)
at android.os.AsyncTask.execute(AsyncTask.java:534)
at android.view.View.performClick(View.java:4240)
at android.view.View$PerformClick.run(View.java:17721)
at android.os.Handler.handleCallback(Handler.java:730)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5103)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
Some help if any would be much obliged.
Instead of dialog = new Dialog(UploadRes.this);
Try
dialog = new Dialog(context);
your are passing a wrong context to create dialog.
I've been trying develop a custom camera application for my project. I have followed pretty much every steps on the android developers website for the deprecated android.hardware.Camera class, as I need to stay compatible for older devices. I have added the permission for the camera and the features for the camera and the autofocus. I am currently testing with the emulator provided by android studio, the Nexus 5 API 23 x86 with both front-facing and back-facing cameras set on emulated. When my code reaches the Camera.open() bit, I get the error -13: Fail to connect to camera service.
I moved my camera initialization at many places but it always throws that error and returns null. The built-in Camera app in the emulator does work properly for both camera and they are both detected in my code. I simply cannot find why it doesn't work.
Here's my code just in case:
CameraActivity.java
/**
* An example full-screen activity that shows and hides the system UI (i.e.
* status bar and navigation/system bar) with user interaction.
*/
public class CameraActivity extends AppCompatActivity {
private static final boolean AUTO_HIDE = true;
private static final int AUTO_HIDE_DELAY_MILLIS = 3000;
private static final int UI_ANIMATION_DELAY = 300;
private View mControlsView;
private boolean mVisible;
private CameraView mCameraView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_camera);
mVisible = true;
mControlsView = findViewById(R.id.fullscreen_content_controls);
mCameraView = new CameraView(this);
FrameLayout preview = (FrameLayout) findViewById(R.id.camera_view);
preview.addView(mCameraView);
// Set up the user interaction to manually show or hide the system UI.
mCameraView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
toggle();
}
});
// Upon interacting with UI controls, delay any scheduled hide()
// operations to prevent the jarring behavior of controls going away
// while interacting with the UI.
findViewById(R.id.capture_button).setOnTouchListener(mDelayHideTouchListener);
//// TODO: 2015-11-05 Create a preview class extending SurfaceView and implementing SurfaceHolder.callback and put camera code there
//// TODO: 2015-11-05 http://developer.android.com/guide/topics/media/camera.html#custom-camera
//// TODO: 2015-11-05 http://stackoverflow.com/questions/26305107/how-to-fix-fail-to-connect-to-camera-service-exception-in-android-emulator
}
#Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
// Trigger the initial hide() shortly after the activity has been
// created, to briefly hint to the user that UI controls
// are available.
delayedHide(100);
}
/**
* Touch listener to use for in-layout UI controls to delay hiding the
* system UI. This is to prevent the jarring behavior of controls going away
* while interacting with activity UI.
*/
private final View.OnTouchListener mDelayHideTouchListener = new View.OnTouchListener() {
#Override
public boolean onTouch(View view, MotionEvent motionEvent) {
if (AUTO_HIDE) {
delayedHide(AUTO_HIDE_DELAY_MILLIS);
}
return false;
}
};
private void toggle() {
if (mVisible) {
hide();
} else {
show();
}
}
private void hide() {
// Hide UI first
ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
actionBar.hide();
}
mControlsView.setVisibility(View.GONE);
mVisible = false;
// Schedule a runnable to remove the status and navigation bar after a delay
mHideHandler.removeCallbacks(mShowPart2Runnable);
mHideHandler.postDelayed(mHidePart2Runnable, UI_ANIMATION_DELAY);
}
private final Runnable mHidePart2Runnable = new Runnable() {
#SuppressLint("InlinedApi")
#Override
public void run() {
// Delayed removal of status and navigation bar
// Note that some of these constants are new as of API 16 (Jelly Bean)
// and API 19 (KitKat). It is safe to use them, as they are inlined
// at compile-time and do nothing on earlier devices.
mCameraView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LOW_PROFILE
| View.SYSTEM_UI_FLAG_FULLSCREEN
| View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);
}
};
#SuppressLint("InlinedApi")
private void show() {
// Show the system bar
mCameraView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
mVisible = true;
// Schedule a runnable to display UI elements after a delay
mHideHandler.removeCallbacks(mHidePart2Runnable);
mHideHandler.postDelayed(mShowPart2Runnable, UI_ANIMATION_DELAY);
}
private final Runnable mShowPart2Runnable = new Runnable() {
#Override
public void run() {
// Delayed display of UI elements
ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
actionBar.show();
}
mControlsView.setVisibility(View.VISIBLE);
}
};
private final Handler mHideHandler = new Handler();
private final Runnable mHideRunnable = new Runnable() {
#Override
public void run() {
hide();
}
};
/**
* Schedules a call to hide() in [delay] milliseconds, canceling any
* previously scheduled calls.
*/
private void delayedHide(int delayMillis) {
mHideHandler.removeCallbacks(mHideRunnable);
mHideHandler.postDelayed(mHideRunnable, delayMillis);
}
#Override
protected void onPause(){
super.onPause();
mCameraView.releaseCamera();
}
}
CameraView.java
import android.hardware.Camera; //This is the good import!
/**
* TODO: document your custom view class.
*/
#SuppressWarnings("deprecation")
public class CameraView extends SurfaceView implements SurfaceHolder.Callback {
private SurfaceHolder mHolder;
private Camera mCamera;
public CameraView(Context context) {
super(context);
try{
mCamera = Camera.open();
} catch (Exception e){
Log.e("CameraView", "Error creating camera: " + e.getMessage());
}
mHolder = getHolder();
mHolder.addCallback(this);
mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}
#Override
public void surfaceCreated(SurfaceHolder holder) {
try{
mCamera.setPreviewDisplay(mHolder);
mCamera.startPreview();
} catch (IOException e){
Log.d("surfaceCreated", "Error starting camera preview: " + e.getMessage());
}
}
#Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
if(mHolder.getSurface() == null)
return;
try{
mCamera.stopPreview();
} catch (Exception e) {
//Non-existent o.O
}
//Resize, etc
try{
mCamera.setPreviewDisplay(mHolder);
mCamera.startPreview();
} catch (IOException e) {
Log.d("CameraView", "Error starting camera preview: " + e.getMessage());
}
}
#Override
public void surfaceDestroyed(SurfaceHolder holder) {
if(mCamera != null)
mCamera.stopPreview();
}
public void releaseCamera(){
if(mCamera != null){
mCamera.release();
mCamera = null;
}
}
}
The logcat:
11-09 14:13:13.144 7791-7791/? I/art: Not late-enabling -Xcheck:jni (already on)
11-09 14:13:13.144 7791-7791/? I/art: Late-enabling JIT
11-09 14:13:13.146 7791-7791/? I/art: JIT created with code_cache_capacity=2MB compile_threshold=1000
11-09 14:13:13.212 7791-7791/com.weebly.olipro007.cameracontrol W/System: ClassLoader referenced unknown path: /data/app/com.weebly.olipro007.cameracontrol-1/lib/x86
11-09 14:13:13.427 7791-7791/com.weebly.olipro007.cameracontrol W/CameraBase: An error occurred while connecting to camera: 0
11-09 14:13:13.427 7791-7791/com.weebly.olipro007.cameracontrol E/CameraView: Error creating camera: Fail to connect to camera service
11-09 14:13:13.489 7791-7820/com.weebly.olipro007.cameracontrol D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true
11-09 14:13:13.508 7791-7791/com.weebly.olipro007.cameracontrol D/: HostConnection::get() New Host Connection established 0xab645030, tid 7791
11-09 14:13:13.584 7791-7820/com.weebly.olipro007.cameracontrol D/: HostConnection::get() New Host Connection established 0xab6451c0, tid 7820
11-09 14:13:13.611 7791-7820/com.weebly.olipro007.cameracontrol I/OpenGLRenderer: Initialized EGL, version 1.4
11-09 14:13:13.693 7791-7820/com.weebly.olipro007.cameracontrol W/EGL_emulation: eglSurfaceAttrib not implemented
11-09 14:13:13.693 7791-7820/com.weebly.olipro007.cameracontrol W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xabdff9a0, error=EGL_SUCCESS
11-09 14:13:13.699 7791-7791/com.weebly.olipro007.cameracontrol D/AndroidRuntime: Shutting down VM
11-09 14:13:13.699 7791-7791/com.weebly.olipro007.cameracontrol E/AndroidRuntime: FATAL EXCEPTION: main
11-09 14:13:13.699 7791-7791/com.weebly.olipro007.cameracontrol E/AndroidRuntime: Process: com.weebly.olipro007.cameracontrol, PID: 7791
11-09 14:13:13.699 7791-7791/com.weebly.olipro007.cameracontrol E/AndroidRuntime: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.hardware.Camera.setPreviewDisplay(android.view.SurfaceHolder)' on a null object reference
11-09 14:13:13.699 7791-7791/com.weebly.olipro007.cameracontrol E/AndroidRuntime: at com.weebly.olipro007.cameracontrol.CameraView.surfaceCreated(CameraView.java:43)
11-09 14:13:13.699 7791-7791/com.weebly.olipro007.cameracontrol E/AndroidRuntime: at android.view.SurfaceView.updateWindow(SurfaceView.java:582)
11-09 14:13:13.699 7791-7791/com.weebly.olipro007.cameracontrol E/AndroidRuntime: at android.view.SurfaceView$3.onPreDraw(SurfaceView.java:177)
11-09 14:13:13.699 7791-7791/com.weebly.olipro007.cameracontrol E/AndroidRuntime: at android.view.ViewTreeObserver.dispatchOnPreDraw(ViewTreeObserver.java:944)
11-09 14:13:13.699 7791-7791/com.weebly.olipro007.cameracontrol E/AndroidRuntime: at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2055)
11-09 14:13:13.699 7791-7791/com.weebly.olipro007.cameracontrol E/AndroidRuntime: at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1107)
11-09 14:13:13.699 7791-7791/com.weebly.olipro007.cameracontrol E/AndroidRuntime: at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6013)
11-09 14:13:13.699 7791-7791/com.weebly.olipro007.cameracontrol E/AndroidRuntime: at android.view.Choreographer$CallbackRecord.run(Choreographer.java:858)
11-09 14:13:13.699 7791-7791/com.weebly.olipro007.cameracontrol E/AndroidRuntime: at android.view.Choreographer.doCallbacks(Choreographer.java:670)
11-09 14:13:13.699 7791-7791/com.weebly.olipro007.cameracontrol E/AndroidRuntime: at android.view.Choreographer.doFrame(Choreographer.java:606)
11-09 14:13:13.699 7791-7791/com.weebly.olipro007.cameracontrol E/AndroidRuntime: at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:844)
11-09 14:13:13.699 7791-7791/com.weebly.olipro007.cameracontrol E/AndroidRuntime: at android.os.Handler.handleCallback(Handler.java:739)
11-09 14:13:13.699 7791-7791/com.weebly.olipro007.cameracontrol E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:95)
11-09 14:13:13.699 7791-7791/com.weebly.olipro007.cameracontrol E/AndroidRuntime: at android.os.Looper.loop(Looper.java:148)
11-09 14:13:13.699 7791-7791/com.weebly.olipro007.cameracontrol E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5417)
11-09 14:13:13.699 7791-7791/com.weebly.olipro007.cameracontrol E/AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method)
11-09 14:13:13.699 7791-7791/com.weebly.olipro007.cameracontrol E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
11-09 14:13:13.699 7791-7791/com.weebly.olipro007.cameracontrol E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
11-09 14:13:16.909 7791-7791/? I/Process: Sending signal. PID: 7791 SIG: 9
Well it seems that the service was completely blocked for the old api in Marshmallow. I switched the emulator to lollipop and it loads...
I am have a little trouble understanding the AsyncTask and whether or not i can actually do what i am trying to do.
Within my onCreate method of my activity I am doing a few things, one of which is to create a GLSurfaceView and set my renderer. I want this process to be done by an AsyncTask in the background.
Here is my code
public class ActivityTest extends BaseGameActivity
{
/** Hold a reference to our GLSurfaceView */
private MYGLSurfaceView mGLSurfaceView;
public MYGamePlay GamePlay;
public GoogleApiClient mGoogleApiClient = null;
private AdView adView;
private static final String AD_UNIT_ID = "*******************************";
private Button RotateButton;
private CreateRenderer mCreateRenderer;
private TextView Score;
private RelativeLayout layout;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
Dialog loader_dialog = new Dialog(this,android.R.style.Theme_Black_NoTitleBar_Fullscreen);
loader_dialog.setContentView(R.layout.loading_screen);
loader_dialog.show();
// Create an ad.
adView = new AdView(this);
adView.setAdSize(AdSize.SMART_BANNER);
adView.setAdUnitId(AD_UNIT_ID);
// create the layout that holds the combined game layout
layout = new RelativeLayout(this);
layout.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
// Create an ad request. Check logcat output for the hashed device ID to
// get test ads on a physical device.
AdRequest adRequest = new AdRequest.Builder()
.addTestDevice("***********************").build();
// Start loading the ad in the background.
adView.loadAd(adRequest);
Score = new TextView(this);
Score.setText(" 0");
RelativeLayout.LayoutParams scoreParams = new
RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
Score.setLayoutParams(scoreParams);
Typeface tf = Typeface.createFromAsset(getAssets(),"Fonts/D3Euronism_b.ttf");
Score.setTextSize(getResources().getDimension(R.dimen.textsize));
Score.setTypeface(tf);
Score.setTextColor(Color.parseColor("#FDAA03"));
LayoutInflater inflater = (LayoutInflater) this.getSystemService(this.LAYOUT_INFLATER_SERVICE);
View userInterface = inflater.inflate(R.layout.user_interface, null);
RotateButton = (Button) userInterface.findViewById(R.id.rotbutton);
RotateButton.setOnTouchListener(new OnTouchListener()
{
#Override
public boolean onTouch(View v, MotionEvent event)
{
//irrelivant
}
});
RelativeLayout.LayoutParams adParams =
new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
adParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
adParams.addRule(RelativeLayout.CENTER_HORIZONTAL);
mCreateRenderer = new CreateRenderer(this, Score, mGLSurfaceView);
mCreateRenderer.execute();
layout.addView(userInterface);
layout.addView(Score);
layout.addView(adView, adParams);
setContentView(layout);
}
#Override
protected void onResume()
{
// The activity must call the GL surface view's onResume() on activity onResume().
super.onResume();
//mGLSurfaceView.onResume();
}
#Override
protected void onPause()
{
// The activity must call the GL surface view's onPause() on activity onPause().
super.onPause();
//mGLSurfaceView.onPause();
}
private class CreateRenderer extends AsyncTask<Void, Void, GLSurfaceView>
{
Context baseClassContext;
RBGLSurfaceView myGLSurfaceView;
TextView GameScore;
public CreateRenderer(Context mContext, TextView mScore, RBGLSurfaceView rGLSurfaceView )
{
baseClassContext = mContext;
GameScore = mScore;
myGLSurfaceView = rGLSurfaceView;
}
#Override
protected GLSurfaceView doInBackground(Void... params)
{
final ActivityManager activityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
final ConfigurationInfo configurationInfo = activityManager.getDeviceConfigurationInfo();
final boolean supportsEs2 = configurationInfo.reqGlEsVersion >= 0x20000;
if (supportsEs2)
{
// Request an OpenGL ES 2.0 compatible context.
/*line 226*/ myGLSurfaceView = new MYGLSurfaceView(baseClassContext); new GLSurfaceView(baseClassContext);
myGLSurfaceView.setEGLContextClientVersion(2);
final DisplayMetrics displayMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
// Set the renderer to our demo renderer, defined below.
mGoogleApiClient = getApiClient();
layout.addView(myGLSurfaceView);
myGLSurfaceView.setRenderer(new MYRenderer(baseClassContext, GameScore, mGoogleApiClient),displayMetrics);
}
else
{
// This is where you could create an OpenGL ES 1.x compatible
// renderer if you wanted to support both ES 1 and ES 2.
}
return myGLSurfaceView;
}
#Override
protected void onPostExecute(GLSurfaceView result)
{
}
#Override
protected void onPreExecute() {}
#Override
protected void onProgressUpdate(Void... values) {}
}
}
I have been reading conflicting things about AsyncTask like whether i can create a contstructor and pass things to it.
Any way it fails on line 226 which i have marked in the code above.
Here is the logCat
09-05 09:29:29.554: E/AndroidRuntime(7585): FATAL EXCEPTION: AsyncTask #2
09-05 09:29:29.554: E/AndroidRuntime(7585): java.lang.RuntimeException: An error occured while executing doInBackground()
09-05 09:29:29.554: E/AndroidRuntime(7585): at android.os.AsyncTask$3.done(AsyncTask.java:299)
09-05 09:29:29.554: E/AndroidRuntime(7585): at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352)
09-05 09:29:29.554: E/AndroidRuntime(7585): at java.util.concurrent.FutureTask.setException(FutureTask.java:219)
09-05 09:29:29.554: E/AndroidRuntime(7585): at java.util.concurrent.FutureTask.run(FutureTask.java:239)
09-05 09:29:29.554: E/AndroidRuntime(7585): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
09-05 09:29:29.554: E/AndroidRuntime(7585): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
09-05 09:29:29.554: E/AndroidRuntime(7585): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
09-05 09:29:29.554: E/AndroidRuntime(7585): at java.lang.Thread.run(Thread.java:856)
09-05 09:29:29.554: E/AndroidRuntime(7585): Caused by: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
09-05 09:29:29.554: E/AndroidRuntime(7585): at android.os.Handler.<init>(Handler.java:197)
09-05 09:29:29.554: E/AndroidRuntime(7585): at android.os.Handler.<init>(Handler.java:111)
09-05 09:29:29.554: E/AndroidRuntime(7585): at android.view.SurfaceView$1.<init>(SurfaceView.java:122)
09-05 09:29:29.554: E/AndroidRuntime(7585): at android.view.SurfaceView.<init>(SurfaceView.java:122)
09-05 09:29:29.554: E/AndroidRuntime(7585): at android.opengl.GLSurfaceView.<init>(GLSurfaceView.java:213)
09-05 09:29:29.554: E/AndroidRuntime(7585): at com.game.cuberush.MYGLSurfaceView.<init>(MYGLSurfaceView.java:34)
09-05 09:29:29.554: E/AndroidRuntime(7585): at com.game.test.ActivityTest$CreateRenderer.doInBackground(ActivityTest.java:226)
09-05 09:29:29.554: E/AndroidRuntime(7585): at com.game.test.ActivityTest$CreateRenderer.doInBackground(ActivityTest.java:1)
09-05 09:29:29.554: E/AndroidRuntime(7585): at android.os.AsyncTask$2.call(AsyncTask.java:287)
09-05 09:29:29.554: E/AndroidRuntime(7585): at java.util.concurrent.FutureTask.run(FutureTask.java:234)
09-05 09:29:29.554: E/AndroidRuntime(7585): ... 4 more
this is the first time i have tried to use AsyncTask so i realise i am probably making a mess with it. So any help with this would be great
EDIT method using annotations still produces the same result the code a put the #Background annotation doesnt seem to be running on a seperate thread as the oncreate is still waiting for it, unless i am doing it wrong
called un onCreate
CreateRender(mGLSurfaceView, this, Score, loader_dialog, mGoogleApiClient, displayMetrics);
method CreateRender
#Background
protected void CreateRender(MYGLSurfaceView myGLSurfaceView, Context mContext, TextView mScore, Dialog mDialog, GoogleApiClient gAPI, DisplayMetrics mDispMet )
{
myGLSurfaceView.setRenderer(new MYRenderer(mContext, mScore, mDialog, gAPI),mDispMet);
mGLSurfaceView = returnResult(myGLSurfaceView);
}
#UiThread
MYGLSurfaceView returnResult(MYGLSurfaceView res)
{
return res;
}
this must be wrong
If you get crazy using AsyncTask i prefer to try AndroidAnnotations. Instead of writing a complete class to handle your background tasks, you just use a #Background annotation over your method. This concept saves a lot of code and it works!
This is an example from the android annotations wiki at github:
#EActivity(R.layout.translate) // Sets content view to R.layout.translate
public class TranslateActivity extends Activity {
#ViewById // Injects R.id.textInput
EditText textInput;
#ViewById(R.id.myTextView) // Injects R.id.myTextView
TextView result;
#AnimationRes // Injects android.R.anim.fade_in
Animation fadeIn;
#Click // When R.id.doTranslate button is clicked
void doTranslate() {
translateInBackground(textInput.getText().toString());
}
#Background // Executed in a background thread
void translateInBackground(String textToTranslate) {
String translatedText = callGoogleTranslate(textToTranslate);
showResult(translatedText);
}
#UiThread // Executed in the ui thread
void showResult(String translatedText) {
result.setText(translatedText);
result.startAnimation(fadeIn);
}
// [...]
}
Ive been wrestling with AsyncTask the last couple of days myself. It seems to have some kind of an aversion to surfaces.
I would suggest a change of approach -- create the view in onCreate/onResume ( depending on your needs ) and avoid tying in nonexistent surfaces with AsyncTask.
This approach worked for me, hope it does for you as well!
Your basic problem is that you try to change the UI from a non-ui thread. From the documentation:
Additionally, the Andoid UI toolkit is not thread-safe. So, you must not manipulate your UI from a worker thread—you must do all manipulation to your user interface from the UI thread.
Asynctask is actually a convience for that. It does some background work off the main thread and then runs the onPostExecute() on the ui-thread. That means 2 things: first you can change the ui in onPostExecute() and everything done here will be executed serial to the ui (so it could be blocked)
There is the usual quickfix for that with
activity.runOnUiThread(new Runnable() {
public void run() {
//this will be executed on the ui thread
}
});
which will not help you since it will then block the ui again. Im actually not sure if you can do what you want, since some things just can be done in the background (e.g. inflating a real complex xml in onCreateView()). You could try to create your own looper like:
Class used to run a message loop for a thread. Threads by default do not have a message loop associated with them; to create one, call prepare() in the thread that is to run the loop, and then loop() to have it process messages until the loop is stopped.
class LooperThread extends Thread {
public Handler mHandler;
public void run() {
Looper.prepare();
mHandler = new Handler() {
public void handleMessage(Message msg) {
// process incoming messages here
}
};
Looper.loop();
}
}
and handle your surfaceview there, but Im not sure that works.
Further resouces:
Run SurfaceView off UI Thread
Can't create handler inside thread that has not called Looper.prepare()