So I have 3 Activities (LauncherActivity>MenuAvtivity>SelectionActivity)
When I press the back button to go back from the SelectionActivity to the MenuActivity my app chrashes.
Code: Intent intent = new Intent(MenuActivity.this,SelectionActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
Error Code:
12-21 20:37:52.179: E/AndroidRuntime(14823): FATAL EXCEPTION: main
12-21 20:37:52.179: E/AndroidRuntime(14823): Process: com.example.myapp, PID: 14823
12-21 20:37:52.179: E/AndroidRuntime(14823): java.lang.IllegalThreadStateException: Thread already started
12-21 20:37:52.179: E/AndroidRuntime(14823): at java.lang.Thread.checkNotStarted(Thread.java:871)
12-21 20:37:52.179: E/AndroidRuntime(14823): at java.lang.Thread.start(Thread.java:1025)
12-21 20:37:52.179: E/AndroidRuntime(14823): at com.example.myapp.MenuView$1.surfaceCreated(MenuView.java:51)
12-21 20:37:52.179: E/AndroidRuntime(14823): at android.view.SurfaceView.updateWindow(SurfaceView.java:662)
12-21 20:37:52.179: E/AndroidRuntime(14823): at android.view.SurfaceView.onWindowVisibilityChanged(SurfaceView.java:256)
12-21 20:37:52.179: E/AndroidRuntime(14823): at android.view.View.dispatchWindowVisibilityChanged(View.java:8096)
12-21 20:37:52.179: E/AndroidRuntime(14823): at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:1110)
12-21 20:37:52.179: E/AndroidRuntime(14823): at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:1110)
12-21 20:37:52.179: E/AndroidRuntime(14823): at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:1110)
12-21 20:37:52.179: E/AndroidRuntime(14823): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1448)
12-21 20:37:52.179: E/AndroidRuntime(14823): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1192)
12-21 20:37:52.179: E/AndroidRuntime(14823): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6231)
12-21 20:37:52.179: E/AndroidRuntime(14823): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:788)
12-21 20:37:52.179: E/AndroidRuntime(14823): at android.view.Choreographer.doCallbacks(Choreographer.java:591)
12-21 20:37:52.179: E/AndroidRuntime(14823): at android.view.Choreographer.doFrame(Choreographer.java:560)
12-21 20:37:52.179: E/AndroidRuntime(14823): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:774)
12-21 20:37:52.179: E/AndroidRuntime(14823): at android.os.Handler.handleCallback(Handler.java:808)
12-21 20:37:52.179: E/AndroidRuntime(14823): at android.os.Handler.dispatchMessage(Handler.java:103)
12-21 20:37:52.179: E/AndroidRuntime(14823): at android.os.Looper.loop(Looper.java:193)
12-21 20:37:52.179: E/AndroidRuntime(14823): at android.app.ActivityThread.main(ActivityThread.java:5292)
12-21 20:37:52.179: E/AndroidRuntime(14823): at java.lang.reflect.Method.invokeNative(Native Method)
12-21 20:37:52.179: E/AndroidRuntime(14823): at java.lang.reflect.Method.invoke(Method.java:515)
12-21 20:37:52.179: E/AndroidRuntime(14823): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:824)
12-21 20:37:52.179: E/AndroidRuntime(14823): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:640)
12-21 20:37:52.179: E/AndroidRuntime(14823): at dalvik.system.NativeStart.main(Native Method)
The LauncherActivity is using a SurfaceView and the MenuActivity a SurfaceView with a Thread drawing on a Canvas.
Thread Class:
#Override
public void run() {
while (running) {
Canvas c = null;
try {
c = menuView.getHolder().lockCanvas();
synchronized (menuView.getHolder()) {
menuView.doDraw(c);
}
} finally {
if (c != null) {
menuView.getHolder().unlockCanvasAndPost(c);
}
}
}
}
.IllegalThreadStateException: Thread already started
So in your surfaceCreated you're calling thread.start. However that thread was already started previously. So either don't start it again, or create a new Thread. My guess is the first one is the right answer, but without code I can't tell for sure.
Related
firstly i am sorry for my broken english. I wanna read txt file and into 2d array. this code is succesfully running on java. but android emulator gives warning message: "Sorry! the application has stopped unexpectedly. please try again"
how can I fix that? thanks
here is my code:
package com.example.hocam;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class Konular extends ActionBarActivity {
private ListView konuListe;
private List<KonuBilgileri> konuBilgileri;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_konular);
Bundle dersadi = getIntent().getExtras();
String dersinadi= dersadi.getString("Ders");
switch(dersinadi) {
case "Turkce":
KonuGetir turkce=new KonuGetir("turkce.txt");
String[][] turkcekonular=turkce.getKonular();
System.out.println(turkcekonular[0][0]); // it is the problem and give error
konuListe = (ListView) findViewById(R.id.konu_listesi);
konuBilgileri = new ArrayList<KonuBilgileri>();
konuBilgileri.add(new KonuBilgileri(turkcekonular[0][0],R.drawable.turkce, turkcekonular[0][1])); // array is problem
MyAdapter adapter = new MyAdapter(Konular.this, R.layout.custom_list_item, konuBilgileri);
konuListe.setAdapter(adapter);
break;
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.konular, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class KonuGetir {
final int maxLines = 10100;
String[][] resultArray = new String[maxLines][];
public KonuGetir(String Ders){
File file = new File(Ders);
Scanner scanner;
try {
scanner = new Scanner(file,"utf-8");
int linesCounter = 0;
while (scanner.hasNextLine() && linesCounter < maxLines) {
resultArray[linesCounter] = scanner.nextLine().split("#");
linesCounter++;
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public String[][] getKonular() {
return resultArray;
}
}
logfile:
> 09-04 16:18:22.262: E/AndroidRuntime(1164): FATAL EXCEPTION: main
09-04 16:18:22.262: E/AndroidRuntime(1164): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.hocam/com.example.hocam.Konular}: java.lang.NullPointerException
09-04 16:18:22.262: E/AndroidRuntime(1164): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
09-04 16:18:22.262: E/AndroidRuntime(1164): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
09-04 16:18:22.262: E/AndroidRuntime(1164): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
09-04 16:18:22.262: E/AndroidRuntime(1164): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
09-04 16:18:22.262: E/AndroidRuntime(1164): at android.os.Handler.dispatchMessage(Handler.java:99)
09-04 16:18:22.262: E/AndroidRuntime(1164): at android.os.Looper.loop(Looper.java:123)
09-04 16:18:22.262: E/AndroidRuntime(1164): at android.app.ActivityThread.main(ActivityThread.java:4627)
09-04 16:18:22.262: E/AndroidRuntime(1164): at java.lang.reflect.Method.invokeNative(Native Method)
09-04 16:18:22.262: E/AndroidRuntime(1164): at java.lang.reflect.Method.invoke(Method.java:521)
09-04 16:18:22.262: E/AndroidRuntime(1164): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
09-04 16:18:22.262: E/AndroidRuntime(1164): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
09-04 16:18:22.262: E/AndroidRuntime(1164): at dalvik.system.NativeStart.main(Native Method)
09-04 16:18:22.262: E/AndroidRuntime(1164): Caused by: java.lang.NullPointerException
09-04 16:18:22.262: E/AndroidRuntime(1164): at com.example.hocam.Konular.onCreate(Konular.java:66)
09-04 16:18:22.262: E/AndroidRuntime(1164): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
09-04 16:18:22.262: E/AndroidRuntime(1164): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
09-04 16:18:22.262: E/AndroidRuntime(1164): ... 11 more
09-04 16:42:10.422: E/AndroidRuntime(1178): FATAL EXCEPTION: main
09-04 16:42:10.422: E/AndroidRuntime(1178): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.hocam/com.example.hocam.Konular}: java.lang.NullPointerException
09-04 16:42:10.422: E/AndroidRuntime(1178): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
09-04 16:42:10.422: E/AndroidRuntime(1178): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
09-04 16:42:10.422: E/AndroidRuntime(1178): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
09-04 16:42:10.422: E/AndroidRuntime(1178): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
09-04 16:42:10.422: E/AndroidRuntime(1178): at android.os.Handler.dispatchMessage(Handler.java:99)
09-04 16:42:10.422: E/AndroidRuntime(1178): at android.os.Looper.loop(Looper.java:123)
09-04 16:42:10.422: E/AndroidRuntime(1178): at android.app.ActivityThread.main(ActivityThread.java:4627)
09-04 16:42:10.422: E/AndroidRuntime(1178): at java.lang.reflect.Method.invokeNative(Native Method)
09-04 16:42:10.422: E/AndroidRuntime(1178): at java.lang.reflect.Method.invoke(Method.java:521)
09-04 16:42:10.422: E/AndroidRuntime(1178): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
09-04 16:42:10.422: E/AndroidRuntime(1178): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
09-04 16:42:10.422: E/AndroidRuntime(1178): at dalvik.system.NativeStart.main(Native Method)
09-04 16:42:10.422: E/AndroidRuntime(1178): Caused by: java.lang.NullPointerException
09-04 16:42:10.422: E/AndroidRuntime(1178): at com.example.hocam.Konular.onCreate(Konular.java:66)
09-04 16:42:10.422: E/AndroidRuntime(1178): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
09-04 16:42:10.422: E/AndroidRuntime(1178): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
09-04 16:42:10.422: E/AndroidRuntime(1178): ... 11 more
09-04 16:43:28.802: E/AndroidRuntime(1207): FATAL EXCEPTION: main
09-04 16:43:28.802: E/AndroidRuntime(1207): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.hocam/com.example.hocam.Konular}: java.lang.NullPointerException
09-04 16:43:28.802: E/AndroidRuntime(1207): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
09-04 16:43:28.802: E/AndroidRuntime(1207): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
09-04 16:43:28.802: E/AndroidRuntime(1207): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
09-04 16:43:28.802: E/AndroidRuntime(1207): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
09-04 16:43:28.802: E/AndroidRuntime(1207): at android.os.Handler.dispatchMessage(Handler.java:99)
09-04 16:43:28.802: E/AndroidRuntime(1207): at android.os.Looper.loop(Looper.java:123)
09-04 16:43:28.802: E/AndroidRuntime(1207): at android.app.ActivityThread.main(ActivityThread.java:4627)
09-04 16:43:28.802: E/AndroidRuntime(1207): at java.lang.reflect.Method.invokeNative(Native Method)
09-04 16:43:28.802: E/AndroidRuntime(1207): at java.lang.reflect.Method.invoke(Method.java:521)
09-04 16:43:28.802: E/AndroidRuntime(1207): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
09-04 16:43:28.802: E/AndroidRuntime(1207): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
09-04 16:43:28.802: E/AndroidRuntime(1207): at dalvik.system.NativeStart.main(Native Method)
09-04 16:43:28.802: E/AndroidRuntime(1207): Caused by: java.lang.NullPointerException
09-04 16:43:28.802: E/AndroidRuntime(1207): at com.example.hocam.Konular.onCreate(Konular.java:66)
09-04 16:43:28.802: E/AndroidRuntime(1207): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
09-04 16:43:28.802: E/AndroidRuntime(1207): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
09-04 16:43:28.802: E/AndroidRuntime(1207): ... 11 more
android manifest file:
<uses-permission android:name="android.permission.INTERNET" />
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="21" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".YGS"
android:label="#string/title_activity_ygs" >
</activity>
<activity
android:name=".Konular"
android:label="#string/title_activity_konular" >
</activity>
<activity
android:name=".Video"
android:screenOrientation="landscape"
android:label="#string/title_activity_video" >
</activity>
</application>
You are using the following code:
String[][] turkcekonular=turkce.getKonular();
System.out.println(turkcekonular[0][0]);
But get java.lang.NullPointerException.
The reason for this because getKonular() returns an array initalised to size 10100, however scanner does not contain any lines so scanner.hasNextLine() is false.
This means that turkcekonular[0] is null, as it was never set.
Which means that turkcekonular[0][0] is trying to access a null object, so throws a NullPointerException
You should check if the object is null before trying to access it..
if(turkcekonular[0] != null)
System.out.println(turkcekonular[0][0]);
and else where that this issue occurs.
(and you should figure out why your file is not read. Is it named correctly and exists in the specified path?)
I have been working on an app to use a phone's/tablet's camera flash as a flashlight. Everything seemed to be working fine but when I tested it on my Droid Bionic running Android 4.1.2, the app failed to turn on the flash even though it said it did. Here is the java code I used:
package com.example.flash;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.hardware.Camera;
import android.hardware.Camera.Parameters;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends Activity {
private boolean isFlashOn = false;
private Camera camera;
private Button button;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.buttonFlashlight);
Context context = this;
PackageManager pm = context.getPackageManager();
if(!pm.hasSystemFeature(PackageManager.FEATURE_CAMERA)) {
Log.e("err", "Device has no camera!");
Toast.makeText(getApplicationContext(),
"Your device doesn't have camera!",Toast.LENGTH_SHORT).show();
return;
}
camera = Camera.open();
final Parameters p = camera.getParameters();
button.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
if (isFlashOn) {
Log.i("info", "torch is turned off!");
p.setFlashMode(Parameters.FLASH_MODE_OFF);
camera.setParameters(p);
isFlashOn = false;
button.setText("Tap to turn flashlight on.");
}
else {
Log.i("info", "torch is turned on!");
p.setFlashMode(Parameters.FLASH_MODE_TORCH);
camera.setParameters(p);
isFlashOn = true;
button.setText("Tap to turn flashlight off.");
}
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
protected void onStop() {
super.onStop();
if (camera != null) {
camera.release();
}
}}
Is this code correct or did I miss something?
Logcat:
07-03 18:48:29.064: E/Trace(773): error opening trace file: No such file or directory (2)
07-03 18:48:30.535: D/Camera(773): app passed NULL surface
07-03 18:48:31.023: D/libEGL(773): loaded /system/lib/egl/libEGL_emulation.so
07-03 18:48:31.073: D/(773): HostConnection::get() New Host Connection established 0x2a13c3c0, tid 773
07-03 18:48:31.123: D/libEGL(773): loaded /system/lib/egl/libGLESv1_CM_emulation.so
07-03 18:48:31.173: D/libEGL(773): loaded /system/lib/egl/libGLESv2_emulation.so
07-03 18:48:31.406: W/EGL_emulation(773): eglSurfaceAttrib not implemented
07-03 18:48:31.433: D/OpenGLRenderer(773): Enabling debug mode 0
07-03 18:48:31.723: I/Choreographer(773): Skipped 58 frames! The application may be doing too much work on its main thread.
07-03 18:49:05.923: D/dalvikvm(773): GC_CONCURRENT freed 202K, 12% free 2623K/2956K, paused 74ms+25ms, total 234ms
07-03 18:49:06.216: W/EGL_emulation(773): eglSurfaceAttrib not implemented
07-03 18:49:09.584: D/Camera(773): app passed NULL surface
07-03 18:49:09.853: W/EGL_emulation(773): eglSurfaceAttrib not implemented
07-03 18:49:11.813: I/info(773): torch is turned on!
07-03 18:49:13.467: I/info(773): torch is turned off!
07-03 18:49:16.263: W/EGL_emulation(773): eglSurfaceAttrib not implemented
07-03 18:49:16.713: D/AndroidRuntime(773): Shutting down VM
07-03 18:49:16.713: W/dalvikvm(773): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
07-03 18:49:16.936: E/AndroidRuntime(773): FATAL EXCEPTION: main
07-03 18:49:16.936: E/AndroidRuntime(773): java.lang.RuntimeException: Method called after release()
07-03 18:49:16.936: E/AndroidRuntime(773): at android.hardware.Camera._stopPreview(Native Method)
07-03 18:49:16.936: E/AndroidRuntime(773): at android.hardware.Camera.stopPreview(Camera.java:543)
07-03 18:49:16.936: E/AndroidRuntime(773): at com.example.flash.MainActivity.surfaceDestroyed(MainActivity.java:140)
07-03 18:49:16.936: E/AndroidRuntime(773): at android.view.SurfaceView.updateWindow(SurfaceView.java:553)
07-03 18:49:16.936: E/AndroidRuntime(773): at android.view.SurfaceView.onWindowVisibilityChanged(SurfaceView.java:231)
07-03 18:49:16.936: E/AndroidRuntime(773): at android.view.View.dispatchWindowVisibilityChanged(View.java:7544)
07-03 18:49:16.936: E/AndroidRuntime(773): at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:1039)
07-03 18:49:16.936: E/AndroidRuntime(773): at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:1039)
07-03 18:49:16.936: E/AndroidRuntime(773): at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:1039)
07-03 18:49:16.936: E/AndroidRuntime(773): at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:1039)
07-03 18:49:16.936: E/AndroidRuntime(773): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1211)
07-03 18:49:16.936: E/AndroidRuntime(773): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:989)
07-03 18:49:16.936: E/AndroidRuntime(773): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4351)
07-03 18:49:16.936: E/AndroidRuntime(773): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
07-03 18:49:16.936: E/AndroidRuntime(773): at android.view.Choreographer.doCallbacks(Choreographer.java:562)
07-03 18:49:16.936: E/AndroidRuntime(773): at android.view.Choreographer.doFrame(Choreographer.java:532)
07-03 18:49:16.936: E/AndroidRuntime(773): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
07-03 18:49:16.936: E/AndroidRuntime(773): at android.os.Handler.handleCallback(Handler.java:725)
07-03 18:49:16.936: E/AndroidRuntime(773): at android.os.Handler.dispatchMessage(Handler.java:92)
07-03 18:49:16.936: E/AndroidRuntime(773): at android.os.Looper.loop(Looper.java:137)
07-03 18:49:16.936: E/AndroidRuntime(773): at android.app.ActivityThread.main(ActivityThread.java:5041)
07-03 18:49:16.936: E/AndroidRuntime(773): at java.lang.reflect.Method.invokeNative(Native Method)
07-03 18:49:16.936: E/AndroidRuntime(773): at java.lang.reflect.Method.invoke(Method.java:511)
07-03 18:49:16.936: E/AndroidRuntime(773): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
07-03 18:49:16.936: E/AndroidRuntime(773): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
07-03 18:49:16.936: E/AndroidRuntime(773): at dalvik.system.NativeStart.main(Native Method)
07-03 18:49:24.854: E/Trace(811): error opening trace file: No such file or directory (2)
07-03 18:49:25.413: D/libEGL(811): loaded /system/lib/egl/libEGL_emulation.so
07-03 18:49:25.567: D/(811): HostConnection::get() New Host Connection established 0x2a15f570, tid 811
07-03 18:49:25.643: D/libEGL(811): loaded /system/lib/egl/libGLESv1_CM_emulation.so
07-03 18:49:25.663: D/libEGL(811): loaded /system/lib/egl/libGLESv2_emulation.so
07-03 18:49:25.934: W/EGL_emulation(811): eglSurfaceAttrib not implemented
07-03 18:49:25.963: D/OpenGLRenderer(811): Enabling debug mode 0
07-03 18:53:12.298: D/Camera(811): app passed NULL surface
07-03 18:53:12.723: D/dalvikvm(811): GC_CONCURRENT freed 172K, 11% free 2600K/2904K, paused 9ms+165ms, total 421ms
07-03 18:53:12.934: E/EGL_emulation(811): rcCreateWindowSurface returned 0
07-03 18:53:12.934: E/EGL_emulation(811): tid 811: eglCreateWindowSurface(631): error 0x3003 (EGL_BAD_ALLOC)
07-03 18:53:12.943: D/AndroidRuntime(811): Shutting down VM
07-03 18:53:12.943: W/dalvikvm(811): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
07-03 18:53:13.033: E/AndroidRuntime(811): FATAL EXCEPTION: main
07-03 18:53:13.033: E/AndroidRuntime(811): java.lang.RuntimeException: createWindowSurface failed EGL_BAD_ALLOC
07-03 18:53:13.033: E/AndroidRuntime(811): at android.view.HardwareRenderer$GlRenderer.createSurface(HardwareRenderer.java:1064)
07-03 18:53:13.033: E/AndroidRuntime(811): at android.view.HardwareRenderer$GlRenderer.createEglSurface(HardwareRenderer.java:961)
07-03 18:53:13.033: E/AndroidRuntime(811): at android.view.HardwareRenderer$GlRenderer.initialize(HardwareRenderer.java:787)
07-03 18:53:13.033: E/AndroidRuntime(811): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1502)
07-03 18:53:13.033: E/AndroidRuntime(811): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:989)
07-03 18:53:13.033: E/AndroidRuntime(811): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4351)
07-03 18:53:13.033: E/AndroidRuntime(811): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
07-03 18:53:13.033: E/AndroidRuntime(811): at android.view.Choreographer.doCallbacks(Choreographer.java:562)
07-03 18:53:13.033: E/AndroidRuntime(811): at android.view.Choreographer.doFrame(Choreographer.java:532)
07-03 18:53:13.033: E/AndroidRuntime(811): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
07-03 18:53:13.033: E/AndroidRuntime(811): at android.os.Handler.handleCallback(Handler.java:725)
07-03 18:53:13.033: E/AndroidRuntime(811): at android.os.Handler.dispatchMessage(Handler.java:92)
07-03 18:53:13.033: E/AndroidRuntime(811): at android.os.Looper.loop(Looper.java:137)
07-03 18:53:13.033: E/AndroidRuntime(811): at android.app.ActivityThread.main(ActivityThread.java:5041)
07-03 18:53:13.033: E/AndroidRuntime(811): at java.lang.reflect.Method.invokeNative(Native Method)
07-03 18:53:13.033: E/AndroidRuntime(811): at java.lang.reflect.Method.invoke(Method.java:511)
07-03 18:53:13.033: E/AndroidRuntime(811): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
07-03 18:53:13.033: E/AndroidRuntime(811): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
07-03 18:53:13.033: E/AndroidRuntime(811): at dalvik.system.NativeStart.main(Native Method)
UPDATE
I think the key is you are running Android 4.1.2. Since Android 4.0, if you want to use the Camera Device, even if you only want to use the flash, you are forced to use a SurfaceView.
In the previous answer (below), I gave you a link to a Torch app which uses SurfaceView. Try it or adapt it to your code.
PREVIOUS ANSWER:
As stated in many other cases (like this one), you may be facing a Device-Specific issue that is quite common in the Android world.
Although getSupportedFlashModes() may return FLASH_MODE_TORCH on nearly every device, many of them don't actually support it.
Anyway, you could try these:
Use camera.startPreview(); after camera = Camera.open();
Try setting FLASH_MODE_OFF initially (before camera.startPreview();).
Check if this Torch app works in your device. In case it does, you have the source code to compare it to yours.
Download a Torch app from the Play Store to test if it's a device issue or not.
Post the issue in a Droid Bionic support forum.
UPDATE: I would say the final keyword is a problem in your code. Try changing it to:
//camera = Camera.open();
//final Parameters p = camera.getParameters();
button.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
if (isFlashOn) {
Log.i("info", "torch is turned off!");
cam.stopPreview();
cam.release();
isFlashOn = false;
button.setText("Tap to turn flashlight on.");
}
else {
Log.i("info", "torch is turned on!");
camera = Camera.open();
Parameters p = camera.getParameters();
p.setFlashMode(Parameters.FLASH_MODE_OFF);
camera.setParameters(p);
camera.startPreview();
p.setFlashMode(Parameters.FLASH_MODE_TORCH);
camera.setParameters(p);
isFlashOn = true;
button.setText("Tap to turn flashlight off.");
}
}
});
user the permission "android.permission.FLASHLIGHT" in the manifest
<uses-permission android:name="android.permission.FLASHLIGHT" />
<uses-permission android:name="android.permission.CAMERA" />
I've been following the Android tutorials and created MyFirstApp (see http://developer.android.com/training/basics/firstapp/index.html) and I can launch the app ok on the emulator, but upon entering a message and hitting "send" I get an IllegalStateException caused by a NullPointerException at this line in the below code:
String message = editText.getText().toString();
Upon debugging, it is clear that editText is null at this point, but I cannot see why, or anything I've missed in the tutorial.
My MainActivity class:
package com.example.myfirstapp;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
/**
* <p>Main Activity class for the App</p>
*
* #author - thebloodguy
*/
public class MainActivity extends Activity {
//~ ----------------------------------------------------------------------------------------------------------------
//~ Static fields/initializers
//~ ----------------------------------------------------------------------------------------------------------------
/** Key to the extra message data in the sendMessage intent */
public static final String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";
//~ ----------------------------------------------------------------------------------------------------------------
//~ Methods
//~ ----------------------------------------------------------------------------------------------------------------
/**
* {#inheritDoc}
*/
#Override
public boolean onCreateOptionsMenu(Menu menu) {
/* Inflate the menu; this adds items to the action bar if it is present */
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
/**
* <p>Send a message using the data in the {#link View}</p>
*
* #param view - the {#link View} object representing the state of the view when the message is sent
*/
public void sendMessage(View view) {
Intent intent = new Intent(this, DisplayMessageActivity.class);
EditText editText = (EditText) view.findViewById(R.id.edit_message);
String message = editText.getText().toString(); // This is the guilty line
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
}
/**
* {#inheritDoc}
*/
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
My activity_main.xml:
<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=".MainActivity"
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>
and the LogCat output:
12-21 10:02:32.210: E/Trace(621): error opening trace file: No such file or directory (2)
12-21 10:02:32.250: W/ActivityThread(621): Application com.example.myfirstapp is waiting for the debugger on port 8100...
12-21 10:02:32.270: I/System.out(621): Sending WAIT chunk
12-21 10:02:32.280: I/dalvikvm(621): Debugger is active
12-21 10:02:32.480: I/System.out(621): Debugger has connected
12-21 10:02:32.480: I/System.out(621): waiting for debugger to settle...
12-21 10:02:32.680: I/System.out(621): waiting for debugger to settle...
12-21 10:02:32.889: I/System.out(621): waiting for debugger to settle...
12-21 10:02:33.091: I/System.out(621): waiting for debugger to settle...
12-21 10:02:33.290: I/System.out(621): waiting for debugger to settle...
12-21 10:02:33.534: I/System.out(621): waiting for debugger to settle...
12-21 10:02:33.796: I/System.out(621): waiting for debugger to settle...
12-21 10:02:33.990: I/System.out(621): waiting for debugger to settle...
12-21 10:02:34.204: I/System.out(621): debugger has settled (1353)
12-21 10:02:35.429: D/gralloc_goldfish(621): Emulator without GPU emulation detected.
12-21 10:05:11.510: I/Choreographer(621): Skipped 94 frames! The application may be doing too much work on its main thread.
12-21 10:05:13.850: I/Choreographer(621): Skipped 35 frames! The application may be doing too much work on its main thread.
12-21 10:05:15.571: I/Choreographer(621): Skipped 38 frames! The application may be doing too much work on its main thread.
12-21 10:06:53.724: D/AndroidRuntime(621): Shutting down VM
12-21 10:06:53.724: W/dalvikvm(621): threadid=1: thread exiting with uncaught exception (group=0x40a13300)
12-21 10:06:53.840: E/AndroidRuntime(621): FATAL EXCEPTION: main
12-21 10:06:53.840: E/AndroidRuntime(621): java.lang.IllegalStateException: Could not execute method of the activity
12-21 10:06:53.840: E/AndroidRuntime(621): at android.view.View$1.onClick(View.java:3591)
12-21 10:06:53.840: E/AndroidRuntime(621): at android.view.View.performClick(View.java:4084)
12-21 10:06:53.840: E/AndroidRuntime(621): at android.view.View$PerformClick.run(View.java:16966)
12-21 10:06:53.840: E/AndroidRuntime(621): at android.os.Handler.handleCallback(Handler.java:615)
12-21 10:06:53.840: E/AndroidRuntime(621): at android.os.Handler.dispatchMessage(Handler.java:92)
12-21 10:06:53.840: E/AndroidRuntime(621): at android.os.Looper.loop(Looper.java:137)
12-21 10:06:53.840: E/AndroidRuntime(621): at android.app.ActivityThread.main(ActivityThread.java:4745)
12-21 10:06:53.840: E/AndroidRuntime(621): at java.lang.reflect.Method.invokeNative(Native Method)
12-21 10:06:53.840: E/AndroidRuntime(621): at java.lang.reflect.Method.invoke(Method.java:511)
12-21 10:06:53.840: E/AndroidRuntime(621): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
12-21 10:06:53.840: E/AndroidRuntime(621): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
12-21 10:06:53.840: E/AndroidRuntime(621): at dalvik.system.NativeStart.main(Native Method)
12-21 10:06:53.840: E/AndroidRuntime(621): Caused by: java.lang.reflect.InvocationTargetException
12-21 10:06:53.840: E/AndroidRuntime(621): at java.lang.reflect.Method.invokeNative(Native Method)
12-21 10:06:53.840: E/AndroidRuntime(621): at java.lang.reflect.Method.invoke(Method.java:511)
12-21 10:06:53.840: E/AndroidRuntime(621): at android.view.View$1.onClick(View.java:3586)
12-21 10:06:53.840: E/AndroidRuntime(621): ... 11 more
12-21 10:06:53.840: E/AndroidRuntime(621): Caused by: java.lang.NullPointerException
12-21 10:06:53.840: E/AndroidRuntime(621): at com.example.myfirstapp.MainActivity.sendMessage(MainActivity.java:61)
12-21 10:06:53.840: E/AndroidRuntime(621): ... 14 more
Change this line
EditText editText = (EditText) view.findViewById(R.id.edit_message);
to
EditText editText = (EditText)findViewById(R.id.edit_message);
here in sendMessage method, view parameter is the Button being clicked, and EditText is not child of this view, so you need to call findViewById from the activity content view, which you call by
EditText editText = (EditText) findViewById(R.id.edit_message);
public class ListSceneAdapter extends OAListSceneAdapter {
public ListSceneAdapter(Context context) {
super(context, R.layout.listitem); // pass the custom UI layout for list items
}
#Override
protected void setupListItemView(View listItemView, OAScene scene) {
// fill the custom UI with the given scene data for the list item
((TextView)listItemView.findViewById(R.id.listItemName)).setText(scene.name);
// add listener for the button
final OAScene _scene = scene;
((Button)listItemView.findViewById(R.id.listItemButton)).setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getContext(), _scene.description, Toast.LENGTH_SHORT).show();
}
});
((Button)listItemView.findViewById(R.id.listItemMap)).setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
double lat = 1.559034;
double longi =103.641486;
Intent intent = new Intent(
android.content.Intent.ACTION_VIEW,
Uri.parse("http://maps.google.com/maps?saddr="+ lat + "," + longi+ "&daddr="+_scene.getLatitude()+","+_scene.getLongitude()+""));
intent.setClassName("com.google.android.apps.maps", "com.google.android.maps.MapsActivity");
startActivity(intent);
//Toast.makeText(getContext(), _scene.description, Toast.LENGTH_SHORT).show();
}
});
}
}
heres the code. when i try to onClick and open new activity which give navigation from Google Map api.. it giver this error..
The method startActivity(Intent) is undefined for the type new View.OnClickListener(){}
heres the LOGCAT
12-21 11:30:10.867: D/SensorManager(4782): ====>>>>>Num Sensor: 1
12-21 11:30:10.867: D/SensorManager(4782): ====>>>>>Num Sensor: 2
12-21 11:30:10.875: D/SensorManager(4782): ====>>>>>Num Sensor: 3
12-21 11:30:10.875: D/SensorManager(4782): ====>>>>>Num Sensor: 4
12-21 11:30:10.875: D/SensorManager(4782): ====>>>>>Num Sensor: 5
12-21 11:30:10.875: D/SensorManager(4782): ====>>>>>Num Sensor: 6
12-21 11:30:10.875: D/SensorManager(4782): ====>>>>>Num Sensor: 0
12-21 11:30:10.898: I/inertialManager(4782): Rotation is: 0 or 180
12-21 11:30:10.898: I/intertialManager(4782): Natural Orientation is landscape
12-21 11:30:11.000: W/System.err(4782): java.lang.NumberFormatException: unable to parse 'main' as integer
12-21 11:30:11.015: W/System.err(4782): at java.lang.Integer.parse(Integer.java:383)
12-21 11:30:11.015: W/System.err(4782): at java.lang.Integer.parseInt(Integer.java:372)
12-21 11:30:11.015: W/System.err(4782): at java.lang.Integer.parseInt(Integer.java:332)
12-21 11:30:11.015: W/System.err(4782): at java.lang.Integer.valueOf(Integer.java:506)
12-21 11:30:11.015: W/System.err(4782): at com.hitlabnz.androidar.utils.SceneXMLReader.characters(SceneXMLReader.java:427)
12-21 11:30:11.015: W/System.err(4782): at org.apache.harmony.xml.ExpatParser.text(ExpatParser.java:165)
12-21 11:30:11.015: W/System.err(4782): at org.apache.harmony.xml.ExpatParser.appendBytes(Native Method)
12-21 11:30:11.015: W/System.err(4782): at org.apache.harmony.xml.ExpatParser.parseFragment(ExpatParser.java:518)
12-21 11:30:11.015: W/System.err(4782): at org.apache.harmony.xml.ExpatParser.parseDocument(ExpatParser.java:479)
12-21 11:30:11.015: W/System.err(4782): at org.apache.harmony.xml.ExpatReader.parse(ExpatReader.java:318)
12-21 11:30:11.015: W/System.err(4782): at org.apache.harmony.xml.ExpatReader.parse(ExpatReader.java:275)
12-21 11:30:11.015: W/System.err(4782): at com.hitlabnz.androidar.utils.SceneXMLReader.readManifest(SceneXMLReader.java:549)
12-21 11:30:11.015: W/System.err(4782): at com.hitlabnz.androidar.data.management.DataLoaderLocalXML.getSceneData(DataLoaderLocalXML.java:72)
12-21 11:30:11.015: W/System.err(4782): at com.hitlabnz.androidar.data.management.DataLoaderLocalXML.getSceneData(DataLoaderLocalXML.java:1)
12-21 11:30:11.015: W/System.err(4782): at com.hitlabnz.outdoorar.data.OADataManagerAssets.loadScenes(OADataManagerAssets.java:200)
12-21 11:30:11.015: W/System.err(4782): at com.hitlabnz.outdoorar.data.OADataManager.startLoading(OADataManager.java:266)
12-21 11:30:11.015: W/System.err(4782): at com.hitlabnz.outdoorar.api.OAListComponentBase.setupScenes(OAListComponentBase.java:87)
12-21 11:30:11.015: W/System.err(4782): at com.hitlabnz.outdoorar.api.OAListComponentBase.onCreate(OAListComponentBase.java:167)
12-21 11:30:11.015: W/System.err(4782): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
12-21 11:30:11.015: W/System.err(4782): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)
12-21 11:30:11.015: W/System.err(4782): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
12-21 11:30:11.015: W/System.err(4782): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
12-21 11:30:11.023: W/System.err(4782): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
12-21 11:30:11.023: W/System.err(4782): at android.os.Handler.dispatchMessage(Handler.java:99)
12-21 11:30:11.023: W/System.err(4782): at android.os.Looper.loop(Looper.java:130)
12-21 11:30:11.023: W/System.err(4782): at android.app.ActivityThread.main(ActivityThread.java:3687)
12-21 11:30:11.023: W/System.err(4782): at java.lang.reflect.Method.invokeNative(Native Method)
12-21 11:30:11.023: W/System.err(4782): at java.lang.reflect.Method.invoke(Method.java:507)
12-21 11:30:11.023: W/System.err(4782): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
12-21 11:30:11.023: W/System.err(4782): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
12-21 11:30:11.023: W/System.err(4782): at dalvik.system.NativeStart.main(Native Method)
12-21 11:30:11.054: W/System.err(4782): java.lang.NumberFormatException: unable to parse 'main' as integer
12-21 11:30:11.062: W/System.err(4782): at java.lang.Integer.parse(Integer.java:383)
12-21 11:30:11.062: W/System.err(4782): at java.lang.Integer.parseInt(Integer.java:372)
12-21 11:30:11.062: W/System.err(4782): at java.lang.Integer.parseInt(Integer.java:332)
12-21 11:30:11.070: W/System.err(4782): at java.lang.Integer.valueOf(Integer.java:506)
12-21 11:30:11.070: W/System.err(4782): at com.hitlabnz.androidar.utils.SceneXMLReader.characters(SceneXMLReader.java:427)
12-21 11:30:11.070: W/System.err(4782): at org.apache.harmony.xml.ExpatParser.text(ExpatParser.java:165)
12-21 11:30:11.070: W/System.err(4782): at org.apache.harmony.xml.ExpatParser.appendBytes(Native Method)
12-21 11:30:11.070: W/System.err(4782): at org.apache.harmony.xml.ExpatParser.parseFragment(ExpatParser.java:518)
12-21 11:30:11.070: W/System.err(4782): at org.apache.harmony.xml.ExpatParser.parseDocument(ExpatParser.java:479)
12-21 11:30:11.070: W/System.err(4782): at org.apache.harmony.xml.ExpatReader.parse(ExpatReader.java:318)
12-21 11:30:11.070: W/System.err(4782): at org.apache.harmony.xml.ExpatReader.parse(ExpatReader.java:275)
12-21 11:30:11.070: W/System.err(4782): at com.hitlabnz.androidar.utils.SceneXMLReader.readManifest(SceneXMLReader.java:549)
12-21 11:30:11.070: W/System.err(4782): at com.hitlabnz.androidar.data.management.DataLoaderLocalXML.getSceneData(DataLoaderLocalXML.java:72)
12-21 11:30:11.070: W/System.err(4782): at com.hitlabnz.androidar.data.management.DataLoaderLocalXML.getSceneData(DataLoaderLocalXML.java:1)
12-21 11:30:11.070: W/System.err(4782): at com.hitlabnz.outdoorar.data.OADataManagerAssets.loadScenes(OADataManagerAssets.java:200)
12-21 11:30:11.070: W/System.err(4782): at com.hitlabnz.outdoorar.data.OADataManager.startLoading(OADataManager.java:266)
12-21 11:30:11.070: W/System.err(4782): at com.hitlabnz.outdoorar.api.OAListComponentBase.setupScenes(OAListComponentBase.java:87)
12-21 11:30:11.070: W/System.err(4782): at com.hitlabnz.outdoorar.api.OAListComponentBase.onCreate(OAListComponentBase.java:167)
12-21 11:30:11.070: W/System.err(4782): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
12-21 11:30:11.070: W/System.err(4782): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)
12-21 11:30:11.070: W/System.err(4782): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
12-21 11:30:11.070: W/System.err(4782): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
12-21 11:30:11.070: W/System.err(4782): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
12-21 11:30:11.070: W/System.err(4782): at android.os.Handler.dispatchMessage(Handler.java:99)
12-21 11:30:11.070: W/System.err(4782): at android.os.Looper.loop(Looper.java:130)
12-21 11:30:11.070: W/System.err(4782): at android.app.ActivityThread.main(ActivityThread.java:3687)
12-21 11:30:11.070: W/System.err(4782): at java.lang.reflect.Method.invokeNative(Native Method)
12-21 11:30:11.070: W/System.err(4782): at java.lang.reflect.Method.invoke(Method.java:507)
12-21 11:30:11.070: W/System.err(4782): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
12-21 11:30:11.070: W/System.err(4782): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
12-21 11:30:11.070: W/System.err(4782): at dalvik.system.NativeStart.main(Native Method)
12-21 11:30:11.242: W/System.err(4782): java.lang.NumberFormatException: unable to parse 'main' as integer
12-21 11:30:11.242: W/System.err(4782): at java.lang.Integer.parse(Integer.java:383)
12-21 11:30:11.242: W/System.err(4782): at java.lang.Integer.parseInt(Integer.java:372)
12-21 11:30:11.242: W/System.err(4782): at java.lang.Integer.parseInt(Integer.java:332)
12-21 11:30:11.242: W/System.err(4782): at java.lang.Integer.valueOf(Integer.java:506)
12-21 11:30:11.242: W/System.err(4782): at com.hitlabnz.androidar.utils.SceneXMLReader.characters(SceneXMLReader.java:427)
12-21 11:30:11.242: W/System.err(4782): at org.apache.harmony.xml.ExpatParser.text(ExpatParser.java:165)
12-21 11:30:11.242: W/System.err(4782): at org.apache.harmony.xml.ExpatParser.appendBytes(Native Method)
12-21 11:30:11.242: W/System.err(4782): at org.apache.harmony.xml.ExpatParser.parseFragment(ExpatParser.java:518)
12-21 11:30:11.242: W/System.err(4782): at org.apache.harmony.xml.ExpatParser.parseDocument(ExpatParser.java:479)
12-21 11:30:11.242: W/System.err(4782): at org.apache.harmony.xml.ExpatReader.parse(ExpatReader.java:318)
12-21 11:30:11.242: W/System.err(4782): at org.apache.harmony.xml.ExpatReader.parse(ExpatReader.java:275)
12-21 11:30:11.242: W/System.err(4782): at com.hitlabnz.androidar.utils.SceneXMLReader.readManifest(SceneXMLReader.java:549)
12-21 11:30:11.242: W/System.err(4782): at com.hitlabnz.androidar.data.management.DataLoaderLocalXML.getSceneData(DataLoaderLocalXML.java:72)
12-21 11:30:11.242: W/System.err(4782): at com.hitlabnz.androidar.data.management.DataLoaderLocalXML.getSceneData(DataLoaderLocalXML.java:1)
12-21 11:30:11.242: W/System.err(4782): at com.hitlabnz.outdoorar.data.OADataManagerAssets.loadScenes(OADataManagerAssets.java:200)
12-21 11:30:11.250: W/System.err(4782): at com.hitlabnz.outdoorar.data.OADataManager.startLoading(OADataManager.java:266)
12-21 11:30:11.250: W/System.err(4782): at com.hitlabnz.outdoorar.api.OAListComponentBase.setupScenes(OAListComponentBase.java:87)
12-21 11:30:11.250: W/System.err(4782): at com.hitlabnz.outdoorar.api.OAListComponentBase.onCreate(OAListComponentBase.java:167)
12-21 11:30:11.250: W/System.err(4782): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
12-21 11:30:11.250: W/System.err(4782): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)
12-21 11:30:11.250: W/System.err(4782): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
12-21 11:30:11.250: W/System.err(4782): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
12-21 11:30:11.250: W/System.err(4782): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
12-21 11:30:11.250: W/System.err(4782): at android.os.Handler.dispatchMessage(Handler.java:99)
12-21 11:30:11.250: W/System.err(4782): at android.os.Looper.loop(Looper.java:130)
12-21 11:30:11.250: W/System.err(4782): at android.app.ActivityThread.main(ActivityThread.java:3687)
12-21 11:30:11.250: W/System.err(4782): at java.lang.reflect.Method.invokeNative(Native Method)
12-21 11:30:11.250: W/System.err(4782): at java.lang.reflect.Method.invoke(Method.java:507)
12-21 11:30:11.250: W/System.err(4782): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
12-21 11:30:11.250: W/System.err(4782): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
12-21 11:30:11.250: W/System.err(4782): at dalvik.system.NativeStart.main(Native Method)
12-21 11:30:11.398: D/dalvikvm(4782): GC_CONCURRENT freed 265K, 47% free 3001K/5639K, external 499K/517K, paused 3ms+10ms
12-21 11:30:11.398: I/GPSManager(4782): Network Location Provider available
12-21 11:30:11.468: E/SensorManager(4782): registerListener :: handle = 1600615030 name= Gravity Sensor delay= 20000 Listener= com.hitlabnz.androidar.sensors.inertial.InertialManager$3#4055af88
12-21 11:30:11.468: E/SensorManager(4782): =======>>>Sensor Thread RUNNING <<<========
12-21 11:30:11.476: E/SensorManager(4782): registerListener :: handle = 1 name= MMC328X delay= 20000 Listener= com.hitlabnz.androidar.sensors.inertial.InertialManager$2#4055ac88
12-21 11:30:11.476: E/SensorManager(4782): reg :: handle = 1
12-21 11:30:12.687: E/SensorManager(4782): unregisterListener:: all sensors, listener = com.hitlabnz.androidar.sensors.inertial.InertialManager$2#4055ac88
12-21 11:30:12.695: E/SensorManager(4782): unregisterListener:: all sensors, listener = com.hitlabnz.androidar.sensors.inertial.InertialManager$3#4055af88
12-21 11:30:12.804: I/GPSManager(4782): Network Location Provider available
12-21 11:30:12.867: E/SensorManager(4782): registerListener :: handle = 1600615030 name= Gravity Sensor delay= 20000 Listener= com.hitlabnz.androidar.sensors.inertial.InertialManager$3#4055af88
12-21 11:30:12.867: E/SensorManager(4782): registerListener :: handle = 1 name= MMC328X delay= 20000 Listener= com.hitlabnz.androidar.sensors.inertial.InertialManager$2#4055ac88
12-21 11:30:12.875: E/SensorManager(4782): reg :: handle = 1
12-21 11:30:12.976: D/AndroidRuntime(4782): Shutting down VM
12-21 11:30:12.976: W/dalvikvm(4782): threadid=1: thread exiting with uncaught exception (group=0x40018578)
12-21 11:30:13.039: E/AndroidRuntime(4782): FATAL EXCEPTION: main
12-21 11:30:13.039: E/AndroidRuntime(4782): java.lang.NullPointerException
12-21 11:30:13.039: E/AndroidRuntime(4782): at com.hitlabnz.tutorialbasic.ListSceneAdapter$2.onClick(ListSceneAdapter.java:53)
12-21 11:30:13.039: E/AndroidRuntime(4782): at android.view.View.performClick(View.java:2485)
12-21 11:30:13.039: E/AndroidRuntime(4782): at android.view.View$PerformClick.run(View.java:9080)
12-21 11:30:13.039: E/AndroidRuntime(4782): at android.os.Handler.handleCallback(Handler.java:587)
12-21 11:30:13.039: E/AndroidRuntime(4782): at android.os.Handler.dispatchMessage(Handler.java:92)
12-21 11:30:13.039: E/AndroidRuntime(4782): at android.os.Looper.loop(Looper.java:130)
12-21 11:30:13.039: E/AndroidRuntime(4782): at android.app.ActivityThread.main(ActivityThread.java:3687)
12-21 11:30:13.039: E/AndroidRuntime(4782): at java.lang.reflect.Method.invokeNative(Native Method)
12-21 11:30:13.039: E/AndroidRuntime(4782): at java.lang.reflect.Method.invoke(Method.java:507)
12-21 11:30:13.039: E/AndroidRuntime(4782): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
12-21 11:30:13.039: E/AndroidRuntime(4782): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
12-21 11:30:13.039: E/AndroidRuntime(4782): at dalvik.system.NativeStart.main(Native Method)
OAListSceneAdapter class
/*
* Copyright 2011 the Human Interface Technology Laboratory New Zealand, University of Canterbury.
* http://www.hitlabnz.org
*
* This software is provided under the license terms described in LICENSE.TXT file distributed with this software package.
*/
package com.hitlabnz.outdoorar.api;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import com.hitlabnz.androidar.data.SceneData;
import com.hitlabnz.outdoorar.R;
/**
* Basic adapter class for making custom layout for list items in list components.
* #author Gun Lee
*/
public class OAListSceneAdapter extends ArrayAdapter<SceneData> {
protected LayoutInflater layoutInflater;
private int listItemLayoutId;
private Context c;
/**
* Instantiate with the context to access the layout resource for list items.
* Subclasses should override this method and call super(context, layoutResId) with a customized layout resource.
* #param context context for accessing resources
*/
public OAListSceneAdapter(Context context) {
this(context, R.layout.oa_list_item);
c =context;
}
/**
* Instantiate with the context to access the layout resource for list items.
* Subclasses should NOT override this method.
* #param context context for accessing resources
* #param layoutResId resource id of the list item layout
*/
protected OAListSceneAdapter(Context context, int layoutResId) {
super(context, R.layout.list_item);
this.layoutInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
listItemLayoutId = layoutResId;
}
/**
* Called back for setting up the list item view for a given scene.
* Subclasses should override this method for setting up the list item view for the scene.
* Subclasses need NOT to call the method in the super class.
* #param listItemView view for the list item, create with the layout resource id given at instan
* #param scene
*/
protected void setupListItemView(View listItemView, OAScene scene) {
TextView nameText = (TextView) listItemView.findViewById(R.id.listItemName);
TextView categoryText = (TextView) listItemView.findViewById(R.id.listItemCategory);
nameText.setText(scene.name);
if(scene.category == null)
categoryText.setText("");
else
categoryText.setText("- " + scene.category);
}
/**
* Called back for a view for the list item.
* Subclasses should NOT override this method, but override the setupListItemView() method for customization.
*/
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null)
convertView = layoutInflater.inflate(listItemLayoutId, parent, false);
OAScene scene = (OAScene)this.getItem(position);
setupListItemView(convertView, scene);
return convertView;
}
}
That's because Listeners don't start Activities.
I'm assuming that the Context passed to your class is available to the whole class via a getter method or something similar. If not, you can store a global reference to this Context when it is obtained via the constructor. You may need to make it final or the Listener may complain.
Anyways, what you have to do, is call
context.startActivity(intent)
instead of
startActivity(intent);
because Contexts can start Activities.
You can pass the object activity class to the adapter, from which you are calling the adapter. Then use that object to call startActivity.
((Button)listItemView.findViewById(R.id.listItemMap)).setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(A.alias,B.class);
A.alias.startActivity(intent);
}
});
I have created new activity that my MainActivity Should lunch, some why the application is crashing on the start of the new activity (called GamePlayActivity).
Here is the java code:
Intent startGameDrill = new Intent(MainActivity.this, GamePlayActivity.class);
startActivity(startGameDrill);
Here is the startGameDrill:
package com.simplemathgame;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
public class GamePlayActivity extends MainActivity {
int addDrills;
int subDrils;
int mulDrills;
int divDrills;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game_play);
try {
numberOfAddDrills = (TextView) findViewById(R.id.add_drills_number);
numberOfSubDrills = (TextView) findViewById(R.id.sub_drills_number);
numberOfMulDrills = (TextView) findViewById(R.id.mul_drills_number);
numberOfDivDrills = (TextView) findViewById(R.id.div_drills_number);
minBoundText = (TextView) findViewById(R.id.min_text);
maxBoundText = (TextView) findViewById(R.id.max_text);
} catch (Exception e1) {
// TODO Auto-generated catch block
Log.w("game","error");
}
try {
addDrills = Integer.parseInt((String) numberOfAddDrills.getText());
subDrils = Integer.parseInt((String) numberOfSubDrills.getText());
mulDrills = Integer.parseInt((String) numberOfMulDrills.getText());
divDrills = Integer.parseInt((String) numberOfDivDrills.getText());
} catch (NumberFormatException e) {
Log.w("GameDrills","string to int");
}
Log.w("add", "" + addDrills);
Log.w("add", "" + subDrils);
Log.w("add", "" + mulDrills);
Log.w("add", "" + divDrills);
}
}
Here is the manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.simplemathgame"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="16" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:theme="#style/AppTheme" >
<activity
android:name="com.simplemathgame.Splash"
android:theme="#android:style/Theme.Black.NoTitleBar.Fullscreen" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.simplemathgame.MainActivity"
android:theme="#android:style/Theme.Black.NoTitleBar.Fullscreen" >
<intent-filter>
<action android:name="com.simplemathgame.MainActivity" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name="com.simplemathgame.GamePlayActivity"
android:theme="#android:style/Theme.Black.NoTitleBar.Fullscreen" >
</activity>
</application>
</manifest>
here is the logCat:
12-21 22:05:53.949: D/dalvikvm(610): GC_EXTERNAL_ALLOC freed 42K, 53% free 2546K/5379K, external 1917K/2137K, paused 41ms
12-21 22:06:32.809: D/AndroidRuntime(610): Shutting down VM
12-21 22:06:32.809: W/dalvikvm(610): threadid=1: thread exiting with uncaught exception (group=0x40015560)
12-21 22:06:32.818: E/AndroidRuntime(610): FATAL EXCEPTION: main
12-21 22:06:32.818: E/AndroidRuntime(610): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.simplemathgame/com.simplemathgame.GamePlayActivity}: java.lang.NullPointerException
12-21 22:06:32.818: E/AndroidRuntime(610): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
12-21 22:06:32.818: E/AndroidRuntime(610): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
12-21 22:06:32.818: E/AndroidRuntime(610): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
12-21 22:06:32.818: E/AndroidRuntime(610): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
12-21 22:06:32.818: E/AndroidRuntime(610): at android.os.Handler.dispatchMessage(Handler.java:99)
12-21 22:06:32.818: E/AndroidRuntime(610): at android.os.Looper.loop(Looper.java:123)
12-21 22:06:32.818: E/AndroidRuntime(610): at android.app.ActivityThread.main(ActivityThread.java:3683)
12-21 22:06:32.818: E/AndroidRuntime(610): at java.lang.reflect.Method.invokeNative(Native Method)
12-21 22:06:32.818: E/AndroidRuntime(610): at java.lang.reflect.Method.invoke(Method.java:507)
12-21 22:06:32.818: E/AndroidRuntime(610): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
12-21 22:06:32.818: E/AndroidRuntime(610): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
12-21 22:06:32.818: E/AndroidRuntime(610): at dalvik.system.NativeStart.main(Native Method)
12-21 22:06:32.818: E/AndroidRuntime(610): Caused by: java.lang.NullPointerException
12-21 22:06:32.818: E/AndroidRuntime(610): at com.simplemathgame.GamePlayActivity.onCreate(GamePlayActivity.java:31)
12-21 22:06:32.818: E/AndroidRuntime(610): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
12-21 22:06:32.818: E/AndroidRuntime(610): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
12-21 22:06:32.818: E/AndroidRuntime(610): ... 11 more
Here is the layout of the GamePlayActivity:
<?xml version="1.0" encoding="utf-8"?>
<TableLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:background="#000044">
<TableRow
android:layout_marginTop="10dp"
android:gravity="center_vertical" >
<TextView
android:textColor="#FFFFFFFF"
android:textSize="16sp"
android:textStyle="bold"
android:text="Addition Drills:"/>
</TableRow>
</TableLayout>
Why does it crashes?
Have a look here:
12-21 22:06:32.818: E/AndroidRuntime(610): Caused by: java.lang.NullPointerException
12-21 22:06:32.818: E/AndroidRuntime(610): at com.simplemathgame.GamePlayActivity.onCreate(GamePlayActivity.java:31)
Line 31 of GamePlayActivity.java is this line:
addDrills = Integer.parseInt((String) numberOfAddDrills.getText());
Since the only thing on this line that you reference a member or method of is numberOfAddDrills, then it must be null.
Consider that you are checking for exceptions when using findViewById; however, if the view is not found it will just return null, not throw an exception.
Have a look at the activity_game_play.xml you posted; there is no TextView with android:id="#+id/add_drills_number". You need to create it, and the same deal for the other five TextViews.
Oh, and a hint: Don't just catch generic exceptions with } catch (Exception e1) {, especially if you have no intent of reading the logs. I can't tell you how many times people have missed errors from doing this.
If you want to pass values from another layout, you have two options: keep a reference to the View objects (this is a bit silly, don't do this); or pass the data you need from them to your new Activity:
startGameDrill.putExtra("myIntExtra", 5); // For example
Then, in GamePlayActivity.onCreate(), you can fetch it using getIntent()'s extras:
Bundle extras = getIntent().getExtras();
int myIntExtra = extras.getInt("myIntExtra");
Then use that value instead of relying on the view in the previous layout. There is a fuller example in this answer.
Follow the stacktrace until you find some references to your classes:
It shows:
12-21 22:06:32.818: E/AndroidRuntime(610): Caused by: java.lang.NullPointerException
12-21 22:06:32.818: E/AndroidRuntime(610): at com.simplemathgame.GamePlayActivity.onCreate(GamePlayActivity.java:31)
So you are creating a NullPointerException at line 31 in GamePlayActivity.java
Line 31:
addDrills = Integer.parseInt((String) numberOfAddDrills.getText());
so maybe numberOfAddDrills is null?