I keep getting this error message when i run my game:
01-21 16:14:00.911: E/AndroidRuntime(15779): android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
01-21 16:14:00.911: E/AndroidRuntime(15779): at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:6373)
01-21 16:14:00.911: E/AndroidRuntime(15779): at android.view.ViewRootImpl.requestLayout(ViewRootImpl.java:878)
01-21 16:14:00.911: E/AndroidRuntime(15779): at android.view.View.requestLayout(View.java:17566)
01-21 16:14:00.911: E/AndroidRuntime(15779): at android.view.View.requestLayout(View.java:17566)
01-21 16:14:00.911: E/AndroidRuntime(15779): at android.view.View.requestLayout(View.java:17566)
01-21 16:14:00.911: E/AndroidRuntime(15779): at android.view.View.requestLayout(View.java:17566)
01-21 16:14:00.911: E/AndroidRuntime(15779): at android.widget.RelativeLayout.requestLayout(RelativeLayout.java:361)
01-21 16:14:00.911: E/AndroidRuntime(15779): at android.view.View.requestLayout(View.java:17566)
01-21 16:14:00.911: E/AndroidRuntime(15779): at android.widget.ScrollView.requestLayout(ScrollView.java:1483)
01-21 16:14:00.911: E/AndroidRuntime(15779): at android.view.View.requestLayout(View.java:17566)
01-21 16:14:00.911: E/AndroidRuntime(15779): at android.widget.RelativeLayout.requestLayout(RelativeLayout.java:361)
01-21 16:14:00.911: E/AndroidRuntime(15779): at android.view.View.requestLayout(View.java:17566)
01-21 16:14:00.911: E/AndroidRuntime(15779): at android.widget.TextView.checkForRelayout(TextView.java:6914)
01-21 16:14:00.911: E/AndroidRuntime(15779): at android.widget.TextView.setText(TextView.java:4096)
01-21 16:14:00.911: E/AndroidRuntime(15779): at android.widget.TextView.setText(TextView.java:3954)
01-21 16:14:00.911: E/AndroidRuntime(15779): at android.widget.TextView.setText(TextView.java:3929)
01-21 16:14:00.911: E/AndroidRuntime(15779): at com.gamerscave.corpboss.Game.Overview_Viewupdate(Game.java:305)
01-21 16:14:00.911: E/AndroidRuntime(15779): at com.gamerscave.corpboss.Game.tick(Game.java:174)
01-21 16:14:00.911: E/AndroidRuntime(15779): at com.gamerscave.corpboss.game.GameThread.run(GameThread.java:27)
this error code is caused by:
public void Overview_Viewupdate(){
tv1.setText("Balance: " + Maths.coustomFormat(DEFAULT_BIG_NUMBER_PATTERN, bal));
tv2.setText("Income: " + Maths.coustomFormat(DEFAULT_BIG_NUMBER_PATTERN, inc));
tv3.setText("Nett worth: " + Maths.coustomFormat(DEFAULT_BIG_NUMBER_PATTERN, NettWorth));
tv4.setText("Shares: " + sharePercent + "%");
tv5.setText("Share value: " + ShareVal);
tv6.setText("Date: " + Date);
}
and it is called at:
public void tick(){
player.tick();
long elapsed = (System.nanoTime()-startTime)/1000000;
if(elapsed>1000)
{
nextDay(day, year, month);
System.out.println("New day: " + day);
startTime = System.nanoTime();
}
if(view1){
Overview_Viewupdate();
}
}
for some reason it isnt triggered on the launch. the launch change is called in the init method. the init method is called from the GameThread-class' run method. So how do I avoid this error and still have a game thread that works?
The error above happens because Android doesn’t want other threads, such as your timer, messing with its GUI. So, what you need to do is politely ask Android to make the update for you.
public void tick(){
player.tick();
long elapsed = (System.nanoTime()-startTime)/1000000;
if(elapsed>1000)
{
nextDay(day, year, month);
System.out.println("New day: " + day);
startTime = System.nanoTime();
}
if(view1){
runOnUiThread(new Runnable() {
public void run(){
Overview_Viewupdate();
}
});
}
}
Your problem is that you are updating UI elements on a non-UI thread. There's a couple of ways around this. In your case, I believe the best way is to use a RunOnUiThread statement, as follows (Note, this has to be done on an Activity):
RuOnUiThread(new Runnable() {
void run() {
Overview_Viewupdate();
}
});
Your problem is that you are updating UI elements on a non-UI thread. There's a couple of ways around this. In your case, I believe the best way is to use a RunOnUiThread statement, as follows (Note, this has to be done on an Activity):
RuOnUiThread(new Runnable() {
void run() {
Overview_Viewupdate();
}
});
This code would replace your current calling of Overview_ViewUpdate. Alternatively, you could consider an AysncTask, where your OnPostExecute runs Overview_ViewUpdate() and your doInBackground() does the operations that must be run in the background.
Also, make sure your UI elements are created on the UI thread. This is usually done in OnCreate or OnStart. AddContentView(...) or SetContentView(...) counts as such.
Related
So I'm using the library Swipe by Pwittchen and the code being used is this section:
CoreActivity.java
#Override
public void onSwipedDown(final MotionEvent event) {
if(m_Toolbar.getVisibility() == View.VISIBLE ){
WebShrink.cancel();
}
//WebView Animation (Shrink)
// if(m_WebView.getAnimation().hasStarted() || m_WebView.getAnimation().hasEnded()){
if(WebEnlarge.hasStarted()){
WebShrink = AnimationUtils.loadAnimation(CoreActivity.this,R.anim.m_webview_shrink);
m_WebView.startAnimation(WebShrink);
m_Preference_Toolbar.setVisibility(View.GONE);
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
m_Toolbar.setVisibility(View.VISIBLE);
}
}, 350);//3 seconds
}
}
Currently I crashes if you swipe down in the moment it is created. Im trying to find a way around it with:
if(m_Toolbar.getVisibility() == View.VISIBLE ){
WebShrink.cancel();
}
but this is not working and am wondering if anyone could help me with this. the ideal situation is that if the toolbar is visible or if WebShrink is already in place then do nothing but I can't find away around it
LogCat
01-18 20:57:23.652 29764-29764/com.equiware.mickeyt.fire E/InputEventReceiver: Exception dispatching input event.
01-18 20:57:23.653 29764-29764/com.equiware.mickeyt.fire D/AndroidRuntime: Shutting down VM
01-18 20:57:23.654 29764-29764/com.equiware.mickeyt.fire E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.equiware.mickeyt.fire, PID: 29764
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.view.animation.Animation.cancel()' on a null object reference
at com.equiware.mickeyt.fire.CoreActivity$2.onSwipedDown(CoreActivity.java:161)
at com.github.pwittchen.swipe.library.rx2.Swipe.onActionUp(Swipe.java:138)
at com.github.pwittchen.swipe.library.rx2.Swipe.dispatchTouchEvent(Swipe.java:101)
at com.equiware.mickeyt.fire.CoreActivity.dispatchTouchEvent(CoreActivity.java:216)
at android.support.v7.view.WindowCallbackWrapper.dispatchTouchEvent(WindowCallbackWrapper.java:68)
at com.android.internal.policy.DecorView.dispatchTouchEvent(DecorView.java:374)
at android.view.View.dispatchPointerEvent(View.java:10177)
at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:4628)
at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:4496)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3947)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:4000)
at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3966)
at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3974)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3947)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:4000)
at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3966)
at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:4095)
at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3974)
at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:4152)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3947)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:4000)
at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3966)
at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3974)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3947)
at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:6437)
at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:6411)
at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:6372)
at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:6571)
at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:185)
at android.view.InputEventReceiver.nativeConsumeBatchedInputEvents(Native Method)
at android.view.InputEventReceiver.consumeBatchedInputEvents(InputEventReceiver.java:176)
at android.view.ViewRootImpl.doConsumeBatchedInput(ViewRootImpl.java:6519)
at android.view.ViewRootImpl$ConsumeBatchedInputRunnable.run(ViewRootImpl.java:6594)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:871)
at android.view.Choreographer.doCallbacks(Choreographer.java:683)
at android.view.Choreographer.doFrame(Choreographer.java:613)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:857)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6247)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:872)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:762)
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.view.animation.Animation.cancel()' on a null object reference at com.equiware.mickeyt.fire.CoreActivity$2.onSwipedDown(CoreActivity.java:161)
Line 161 seems to be:
WebShrink.cancel();
WebShrink is probably null.
You should load the animation outside onSwipeDown method, maybe you can place this in the activity's onCreate:
WebShrink = AnimationUtils.loadAnimation(CoreActivity.this,R.anim.m_webview_shrink);
Then it will be instantiated before onSwipeDown is called.
When I am connecting to the CC device form my Sender application, often the Custom Receiver does not launch properly.
It will load, but will never be in a ready state.
Here is the logcat output:
01-21 14:16:09.603 19396-19396/com.example.chromecast D/PlayActivity onRouteSelected
01-21 14:16:16.913 19396-19396/com.example.chromecast D/PlayActivity onConnected
01-21 14:16:16.953 19396-19396/com.example.chromecast D/ccl_BaseCastManager﹕ [v1.11] onConnected() reached with prior suspension: false
01-21 14:16:16.953 19396-19396/com.example.chromecast D/ccl_BaseCastManager﹕ [v1.11] launchApp() is called
01-21 14:16:16.953 19396-19396/com.example.chromecast D/ccl_BaseCastManager﹕ [v1.11] Launching app
01-21 14:16:17.003 19396-19396/com.example.chromecast D/ccl_DataCastManager﹕ [v1.11] onApplicationStatusChanged() reached:
01-21 14:16:19.513 19396-19396/com.example.chromecast D/ccl_DataCastManager﹕ [v1.11] onApplicationStatusChanged() reached: null
01-21 14:16:20.633 19396-19396/com.example.chromecast D/ccl_BaseCastManager﹕ [v1.11] launchApplication() -> failure result
01-21 14:16:20.633 19396-19396/com.example.chromecast D/ccl_BaseCastManager﹕ [v1.11] disconnectDevice(true,false)
01-21 14:16:20.633 19396-19396/com.example.chromecast D/ccl_BaseCastManager﹕ [v1.11] mConnectionSuspended: false
01-21 14:16:20.633 19396-19396/com.example.chromecast D/ccl_BaseCastManager﹕ [v1.11] clearPersistedConnectionInfo(): Clearing persisted data for 0
01-21 14:16:20.653 19396-19396/com.example.chromecast D/ccl_BaseCastManager﹕ [v1.11] onDisconnected() reached
01-21 14:16:20.653 19396-19396/com.example.chromecast D/ccl_BaseCastManager﹕ [v1.11] Trying to disconnect
01-21 14:16:37.295 19396-19396/com.example.chromecast D/PlayActivity ApplicationConnectionResultCallback.onResult: statusCode15
01-21 14:16:37.295 19396-19396/com.example.chromecast E/PlayActivity application could not launch
01-21 14:16:37.295 19396-19396/com.example.chromecast D/PlayActivity teardown
If I disconnect in my Sender app, then re-connect, the Receiver will get into the ready state and I can cast my content.
What would be causing this behaviour? (It happens more often then not and is very fustrating!)
Some code:
(Media Route initialisation)
mMediaRouter = MediaRouter.getInstance(getApplicationContext());
mMediaRouteSelector = new MediaRouteSelector.Builder()
.addControlCategory(
CastMediaControlIntent.categoryForCast(getResources()
.getString(R.string.app_id))).build();
mMediaRouterCallback = new MyMediaRouterCallback();
mDataCastManager = DataCastManager.initialize(this, getResources().getString(R.string.app_id), getResources().getString(R.string.namespace));
mDataCastManager.reconnectSessionIfPossible(this, true, 20);
Media Route button
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
MenuItem mediaRouteMenuItem = menu.findItem(R.id.media_route_menu_item);
MediaRouteActionProvider mediaRouteActionProvider = (MediaRouteActionProvider) MenuItemCompat.getActionProvider(mediaRouteMenuItem);
mediaRouteActionProvider.setRouteSelector(mMediaRouteSelector);
return super.onCreateOptionsMenu(menu);
}
Callbacks
private class MyMediaRouterCallback extends MediaRouter.Callback {
#Override
public void onRouteSelected(MediaRouter router, MediaRouter.RouteInfo info) {
Log.d(TAG, "onRouteSelected");
// Handle the user route selection.
mSelectedDevice = CastDevice.getFromBundle(info.getExtras());
launchReceiver();
}
#Override
public void onRouteUnselected(MediaRouter router, MediaRouter.RouteInfo info) {
Log.d(TAG, "onRouteUnselected: info=" + info);
teardown();
mSelectedDevice = null;
}
}
As you can see, most of my implementation is from the CastHelloText Android sample application. I have added in the CCL library to allow easy session management, but I think that is causing the issue.
Commenting out the DataCastManager seems to give much better results when connecting the the CC device for casting.
If you are using CCL, then stop managing the media router button, the direct callbacks from media router, launching receiver, etc since those are handled by CCL. If you want to do those things yourself, then stop using CCL; you are currently mixing two different things in a wrong way. Reading the docs on CCL can be helpful.
i am trying to implement Runnable and run the Run() method when a thread is started. but when i run the program it crashed.
MainActivity
public class MainActivity extends Activity implements Runnable{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Thread t1;
t1=new Thread(this);
t1.start();
}
public void run() {
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this, "display something",
Toast.LENGTH_LONG).show();
}
i tried changing it to t1=new Thread(new MainActivity());(app crashed) or just t1=new Thread(); never crash but no output.
how do i implement a Runnable Run when a thread is started? i search all over the place but could not find an answer. i need to include this function in my main project code too. but i create a separate testing project just to get how this works so i can add it in my main project code myself. at my main project it crashed at this point too. it never reached the Run method.
after it crashed, this is the LogCat
01-21 13:03:06.460: W/dalvikvm(879): threadid=11: thread exiting with uncaught exception (group=0xb3a6fb90)
01-21 13:03:06.460: E/AndroidRuntime(879): FATAL EXCEPTION: Thread-51
01-21 13:03:06.460: E/AndroidRuntime(879): Process: com.example.testthreadrun, PID: 879
01-21 13:03:06.460: E/AndroidRuntime(879): java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
01-21 13:03:06.460: E/AndroidRuntime(879): at android.os.Handler.<init>(Handler.java:200)
01-21 13:03:06.460: E/AndroidRuntime(879): at android.os.Handler.<init>(Handler.java:114)
01-21 13:03:06.460: E/AndroidRuntime(879): at android.widget.Toast$TN.<init>(Toast.java:327)
01-21 13:03:06.460: E/AndroidRuntime(879): at android.widget.Toast.<init>(Toast.java:92)
01-21 13:03:06.460: E/AndroidRuntime(879): at android.widget.Toast.makeText(Toast.java:241)
01-21 13:03:06.460: E/AndroidRuntime(879): at com.example.testthreadrun.MainActivity.run(MainActivity.java:29)
01-21 13:03:06.460: E/AndroidRuntime(879): at java.lang.Thread.run(Thread.java:841)
01-21 13:03:07.010: I/Choreographer(879): Skipped 126 frames! The application may be doing too much work on its main thread.
01-21 13:03:07.970: I/Choreographer(879): Skipped 165 frames! The application may be doing too much work on its main thread.
01-21 13:03:08.840: D/gralloc_goldfish(879): Emulator without GPU emulation detected.
01-21 13:03:10.770: I/Choreographer(879): Skipped 31 frames! The application may be doing too much work on its main thread.
01-21 13:03:26.670: I/Process(879): Sending signal. PID: 879 SIG: 9
Since you are trying to update the UI, you need to do it on the UI Thread. You should use something like runOnUiThread() or AsyncTask.
runOnUiThread(new Runnable()
{
#Override
public void run()
{
Toast.makeText(MainActivity.this, "display something",
Toast.LENGTH_LONG).show();
}
});
or
Example of AsyncTask
AsyncTask Docs
You can't update ui from a background thread. You can update ui from ui thread only.
You can use runOnUiThread. But to just display a toast why do you require a thread?.
http://developer.android.com/guide/components/processes-and-threads.html
You cannot do UI changes (like a toast) on a thread that is not the UI thread. Use this instead:
public void run() {
MainActivity.this.runOnUiThread(new Runnable(){
#Override
public void run(){
Toast.makeText(MainActivity.this, "display something",
Toast.LENGTH_LONG).show();
}
});
}
However, why are you creating a whole separate thread to show a toast? You're better off just putting Toast.makeText(MainActivity.this, "display something",
Toast.LENGTH_LONG).show(); in your onCreate method.
I am currently working on the new Google Maps Android API v2.
My MapActivity class is working with my Samsung galaxy s3 ans s2 with the code below:
However, there are some particular devices are not working such as: GT-S5830i
class MapActivity extends FragmentActivity implements OnMapClickListener, OnMapLongClickListener, OnMarkerClickListener
{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_map);
tvLocInfo = (TextView)findViewById(R.id.locinfo);
android.support.v4.app.FragmentManager myFragmentManager = getSupportFragmentManager();
SupportMapFragment myMapFragment = (SupportMapFragment)myFragmentManager.findFragmentById(R.id.map);
myMap = myMapFragment.getMap();
myMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
ArrayList<Cooridnates> cooridnatesList = MainActivity.getList();
for(int i=0;i<cooridnatesList.size();i++)
{
//Toast.makeText(MapActivity.this, "SIZE : " + " " + cooridnatesList.size(), Toast.LENGTH_LONG).show();
LatLng point = new LatLng(cooridnatesList.get(i).getLat(),cooridnatesList.get(i).getLon());
tvLocInfo.setText("markers added");
myMap.addMarker(new MarkerOptions().position(point).title(point.toString()));
center=CameraUpdateFactory.newLatLng(point);
}
CameraUpdate zoom=CameraUpdateFactory.zoomTo(16);
myMap.moveCamera(center);
myMap.animateCamera(zoom);
myMap.setOnMapClickListener(this);
myMap.setOnMarkerClickListener(this);
markerClicked = false;
}
For some reason, I am getting this error:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.coldice.plotfinder/com.coldice.plotfinder.MapActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
at android.app.ActivityThread.access$1500(ActivityThread.java:117)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:130)
at android.app.ActivityThread.main(ActivityThread.java:3687)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.google.android.gms.maps.GoogleMap.moveCamera(Unknown Source)
at com.coldice.plotfinder.MapActivity.onCreate(MapActivity.java:70)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)
... 11 more
You function MainActivity.getList() probably returned an empty list, so for loop doesn't execute center=CameraUpdateFactory.newLatLng(point);.
it seems center is null in this >> myMap.moveCamera(center);, make an appropriate check on this variable before passing it in moveCamera method.
It seems problem is here. center might have null value, So to get proper explanation debug your application.
center is initialzed here.
center=CameraUpdateFactory.newLatLng(point);
It might assign null in center.
myMap.moveCamera(center); <<<PROBLEM is here
if(center != null )
myMap.moveCamera(center);
i cannot find the null pointer exception in my code, i was wondering if anyone could help me find what is giving an error.
code:
package com.dingle.ubat;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.os.PowerManager;
import android.view.Display;
import android.view.MotionEvent;
import android.widget.Toast;
public class UltraBrightAndroidTorchActivity extends Activity {
/** Called when the activity is first created. */
PowerManager pm;
PowerManager.WakeLock wl;
public boolean flash = false;
public boolean enableFlash = false;
public boolean dimDisplay = false;
Display display = getWindowManager().getDefaultDisplay();
public int windowWidth = display.getWidth();
public int windowHeight = display.getHeight();
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
wl = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK, "My Tag");
flash = this.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH);
if(flash == true && enableFlash == true){
try {
DroidLED led = new DroidLED();
led.enable(!led.isEnabled());
}
catch(Exception e) {
Toast.makeText(this, "Error interacting with LED.", Toast.LENGTH_SHORT).show();
throw new RuntimeException(e);
} }
wl.acquire();
}
#Override
public boolean onTouchEvent(MotionEvent event) {
//int tx = (int) event.getRawX();
int ty = (int) event.getRawY();
if (ty <= (windowHeight/2)){
dimDisplay = !dimDisplay;
}
if (ty >= (windowHeight/2)){
enableFlash = !enableFlash;
}
return super.onTouchEvent(event);
}
#Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
wl.release();
}
#Override
protected void onNewIntent(Intent intent) {
// TODO Auto-generated method stub
super.onNewIntent(intent);
wl.release();
}
#Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
wl.release();
}
#Override
protected void onStop() {
// TODO Auto-generated method stub
super.onStop();
wl.release();
}
}
Error list:
12-10 20:40:42.224: W/dalvikvm(274): threadid=3: thread exiting with uncaught exception (group=0x4001b188)
12-10 20:40:42.232: E/AndroidRuntime(274): Uncaught handler: thread main exiting due to uncaught exception
12-10 20:40:42.282: E/AndroidRuntime(274): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.dingle.ubat/com.dingle.ubat.UltraBrightAndroidTorchActivity}: java.lang.NullPointerException
12-10 20:40:42.282: E/AndroidRuntime(274): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2417)
12-10 20:40:42.282: E/AndroidRuntime(274): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
12-10 20:40:42.282: E/AndroidRuntime(274): at android.app.ActivityThread.access$2200(ActivityThread.java:119)
12-10 20:40:42.282: E/AndroidRuntime(274): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
12-10 20:40:42.282: E/AndroidRuntime(274): at android.os.Handler.dispatchMessage(Handler.java:99)
12-10 20:40:42.282: E/AndroidRuntime(274): at android.os.Looper.loop(Looper.java:123)
12-10 20:40:42.282: E/AndroidRuntime(274): at android.app.ActivityThread.main(ActivityThread.java:4363)
12-10 20:40:42.282: E/AndroidRuntime(274): at java.lang.reflect.Method.invokeNative(Native Method)
12-10 20:40:42.282: E/AndroidRuntime(274): at java.lang.reflect.Method.invoke(Method.java:521)
12-10 20:40:42.282: E/AndroidRuntime(274): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
12-10 20:40:42.282: E/AndroidRuntime(274): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
12-10 20:40:42.282: E/AndroidRuntime(274): at dalvik.system.NativeStart.main(Native Method)
12-10 20:40:42.282: E/AndroidRuntime(274): Caused by: java.lang.NullPointerException
12-10 20:40:42.282: E/AndroidRuntime(274): at com.dingle.ubat.UltraBrightAndroidTorchActivity.<init>(UltraBrightAndroidTorchActivity.java:20)
12-10 20:40:42.282: E/AndroidRuntime(274): at java.lang.Class.newInstanceImpl(Native Method)
12-10 20:40:42.282: E/AndroidRuntime(274): at java.lang.Class.newInstance(Class.java:1479)
12-10 20:40:42.282: E/AndroidRuntime(274): at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
12-10 20:40:42.282: E/AndroidRuntime(274): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2409)
12-10 20:40:42.282: E/AndroidRuntime(274): ... 11 more
any help and criticism is greatly appreciated
code is being compiled for android 2.1. code is a basic, crappily coded torch app.
thanx
The code is bailing out on one of these two lines:
Display display = getWindowManager().getDefaultDisplay();
public int windowWidth = display.getWidth();
Either getWindowManager() is returning a null and that first initializer will fail, or getDefaultDisplay() is, and the second one will.
You should probably move these initializations to the onCreate handler. Having them at instance initialization sounds a bit risky - the activity's "environment" might not be completely set up yet at that point.
(And check the return values.)
Activity is full available only after it has been created. This line does not do the intended purpose:
Display display = getWindowManager().getDefaultDisplay();
Move this line and subsequent ones inside onCreate method.