I am getting the following Logcat information when I click a button on my app:
06-19 11:24:45.308: E/AndroidRuntime(11498): FATAL EXCEPTION: main
06-19 11:24:45.308: E/AndroidRuntime(11498): java.lang.IllegalStateException: Could not execute method of the activity
06-19 11:24:45.308: E/AndroidRuntime(11498): at android.view.View$1.onClick(View.java:3674)
06-19 11:24:45.308: E/AndroidRuntime(11498): at android.view.View.performClick(View.java:4198)
06-19 11:24:45.308: E/AndroidRuntime(11498): at android.view.View$PerformClick.run(View.java:17164)
06-19 11:24:45.308: E/AndroidRuntime(11498): at android.os.Handler.handleCallback(Handler.java:615)
06-19 11:24:45.308: E/AndroidRuntime(11498): at android.os.Handler.dispatchMessage(Handler.java:92)
06-19 11:24:45.308: E/AndroidRuntime(11498): at android.os.Looper.loop(Looper.java:137)
06-19 11:24:45.308: E/AndroidRuntime(11498): at android.app.ActivityThread.main(ActivityThread.java:4918)
06-19 11:24:45.308: E/AndroidRuntime(11498): at java.lang.reflect.Method.invokeNative(Native Method)
06-19 11:24:45.308: E/AndroidRuntime(11498): at java.lang.reflect.Method.invoke(Method.java:511)
06-19 11:24:45.308: E/AndroidRuntime(11498): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1004)
06-19 11:24:45.308: E/AndroidRuntime(11498): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:771)
06-19 11:24:45.308: E/AndroidRuntime(11498): at dalvik.system.NativeStart.main(Native Method)
06-19 11:24:45.308: E/AndroidRuntime(11498): Caused by: java.lang.reflect.InvocationTargetException
06-19 11:24:45.308: E/AndroidRuntime(11498): at java.lang.reflect.Method.invokeNative(Native Method)
06-19 11:24:45.308: E/AndroidRuntime(11498): at java.lang.reflect.Method.invoke(Method.java:511)
06-19 11:24:45.308: E/AndroidRuntime(11498): at android.view.View$1.onClick(View.java:3669)
06-19 11:24:45.308: E/AndroidRuntime(11498): ... 11 more
06-19 11:24:45.308: E/AndroidRuntime(11498): Caused by: java.lang.NoClassDefFoundError: com.facebook.android.MainActivity$UpdateStatusListener
06-19 11:24:45.308: E/AndroidRuntime(11498): at com.facebook.android.MainActivity.triggerDialog(MainActivity.java:55)
06-19 11:24:45.308: E/AndroidRuntime(11498): at com.facebook.android.MainActivity.firstClicked(MainActivity.java:36)
06-19 11:24:45.308: E/AndroidRuntime(11498): ... 14 more
Here is how I have my classes set up following an example using the Facebook SDK for Android, with only the relevant methods and info displayed here:
public class MainActivity extends Activity {
//triggered when the button is clicked
public void firstClicked(View view)
{
triggerDialog();
}
public void triggerDialog()
{
//assume for the sake of this post that this string array has 4 strings
String[] offer_details = postOffer.getDetails();
Bundle params = new Bundle();
params.putString("caption", getString(R.string.app_name)); //Hackbook for Android
Utility.mFacebook.dialog(MainActivity.this, "feed", params, new UpdateStatusListener());
}
public class UpdateStatusListener extends BaseDialogListener {
#Override
public void onComplete(Bundle values) {
Log.i("wbbug","Status post complete.");
final String postId = values.getString("post_id");
if (postId != null) {
Toast toast = Toast.makeText(getApplicationContext(), "Facebook status update successful", Toast.LENGTH_LONG);
toast.show();
} else {
Toast toast = Toast.makeText(getApplicationContext(), "No wall post made",
Toast.LENGTH_LONG);
toast.show();
}
}
}
}
This is all directly following an example from the Facebook SDK, and the example is working with this basic code. Why is it that it cannot find the class UpdateStatusListener()? Thanks!
Related
Other stuck posts unfortunately couldn't help me.
When I clicked button while easy radiobutton is checked, the app stops working. I couldn't go and see another activity.
Sender Side:
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if(radiobutton_arm_triceps_easy.isChecked()) {
String dene = "my example test";
int myValue=2;
Intent intent = new Intent(getApplicationContext(), exercise_arm_triceps_execute.class);
intent.putExtra("attempt1", myValue );
startActivity(intent);
}
}
});
Receiver Side:
int receiveValue=getIntent().getIntExtra("attempt1",0);
textshow.setText(receiveValue);
LOGCAT
04-26 16:52:06.320 31527-31527/com.example.kerem.tutorial_project E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.kerem.tutorial_project, PID: 31527
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.kerem.tutorial_project/com.example.kerem.tutorial_project.exercise_arm_triceps_execute}: android.content.res.Resources$NotFoundException: String resource ID #0x2
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5001)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.content.res.Resources$NotFoundException: String resource ID #0x2
at android.content.res.Resources.getText(Resources.java:244)
at android.support.v7.widget.ResourcesWrapper.getText(ResourcesWrapper.java:52)
at android.widget.TextView.setText(TextView.java:3888)
at com.example.kerem.tutorial_project.exercise_arm_triceps_execute.onCreate(exercise_arm_triceps_execute.java:28)
at android.app.Activity.performCreate(Activity.java:5231)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5001)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
Use
textshow.setText(String.valueOf(receiveValue));
Im writing an application in which i need to list all the pdf files present in a particular forlder.
However for now im trying just to list all the files present in the raw folder and populate a ListView .
This is what i have so far:
Fields[] fields;
ListView list;
String[] sample;
fields=R.raw.class.getFields();int count=0;
for(Field f : fields)
{
sample[count]=f.getName();count++;
}
list=getListView();
list.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
setListAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_checked, sample));
However the application keeps crashing when i start it.
I cant figure where im going wrong.
Can anyone help me?
the log :
06-19 08:52:20.352: E/AndroidRuntime(4955): FATAL EXCEPTION: main
06-19 08:52:20.352: E/AndroidRuntime(4955): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.magazine/com.example.magazine.MainActivity}: java.lang.NullPointerException: storage == null
06-19 08:52:20.352: E/AndroidRuntime(4955): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
06-19 08:52:20.352: E/AndroidRuntime(4955): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
06-19 08:52:20.352: E/AndroidRuntime(4955): at android.app.ActivityThread.access$600(ActivityThread.java:141)
06-19 08:52:20.352: E/AndroidRuntime(4955): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
06-19 08:52:20.352: E/AndroidRuntime(4955): at android.os.Handler.dispatchMessage(Handler.java:99)
06-19 08:52:20.352: E/AndroidRuntime(4955): at android.os.Looper.loop(Looper.java:137)
06-19 08:52:20.352: E/AndroidRuntime(4955): at android.app.ActivityThread.main(ActivityThread.java:5041)
06-19 08:52:20.352: E/AndroidRuntime(4955): at java.lang.reflect.Method.invokeNative(Native Method)
06-19 08:52:20.352: E/AndroidRuntime(4955): at java.lang.reflect.Method.invoke(Method.java:511)
06-19 08:52:20.352: E/AndroidRuntime(4955): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
06-19 08:52:20.352: E/AndroidRuntime(4955): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
06-19 08:52:20.352: E/AndroidRuntime(4955): at dalvik.system.NativeStart.main(Native Method)
06-19 08:52:20.352: E/AndroidRuntime(4955): Caused by: java.lang.NullPointerException: storage == null
06-19 08:52:20.352: E/AndroidRuntime(4955): at java.util.Arrays$ArrayList.<init>(Arrays.java:38)
06-19 08:52:20.352: E/AndroidRuntime(4955): at java.util.Arrays.asList(Arrays.java:154)
06-19 08:52:20.352: E/AndroidRuntime(4955): at android.widget.ArrayAdapter.<init>(ArrayAdapter.java:128)
06-19 08:52:20.352: E/AndroidRuntime(4955): at com.example.magazine.MainActivity.onCreate(MainActivity.java:44)
06-19 08:52:20.352: E/AndroidRuntime(4955): at android.app.Activity.performCreate(Activity.java:5104)
06-19 08:52:20.352: E/AndroidRuntime(4955): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
06-19 08:52:20.352: E/AndroidRuntime(4955): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
06-19 08:52:20.352: E/AndroidRuntime(4955): ... 11 more
You should initialize array of Strings before:
Field[] fields = R.raw.class.getFields();
ListView list;
String sample[] = new String[R.raw.class.getFields().length];
int count=0;
for(Field f : fields)
{
sample[count]=f.getName();
count++;
}
list=getListView();
list.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
setListAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_checked, sample));
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 am getting a NPE, but when I check the logcat there are no points at which it is using my code.
The program crashes after attempting to show an AlertDialog containing a RadioButtonGroup. It previously worked and the only changes I remember are after the dialog.
Now I recieve the following:
EDIT:
06-19 17:15:19.430: E/AndroidRuntime(898): java.lang.NullPointerException
06-19 17:15:19.430: E/AndroidRuntime(898): at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:394)
06-19 17:15:19.430: E/AndroidRuntime(898): at android.widget.ArrayAdapter.getView(ArrayAdapter.java:362)
06-19 17:15:19.430: E/AndroidRuntime(898): at android.widget.AbsListView.obtainView(AbsListView.java:2143)
06-19 17:15:19.430: E/AndroidRuntime(898): at android.widget.ListView.measureHeightOfChildren(ListView.java:1246)
06-19 17:15:19.430: E/AndroidRuntime(898): at android.widget.ListView.onMeasure(ListView.java:1158)
06-19 17:15:19.430: E/AndroidRuntime(898): at android.view.View.measure(View.java:15518)
06-19 17:15:19.430: E/AndroidRuntime(898): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
06-19 17:15:19.430: E/AndroidRuntime(898): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
06-19 17:15:19.430: E/AndroidRuntime(898): at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
06-19 17:15:19.430: E/AndroidRuntime(898): at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
06-19 17:15:19.430: E/AndroidRuntime(898): at android.view.View.measure(View.java:15518)
06-19 17:15:19.430: E/AndroidRuntime(898): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
06-19 17:15:19.430: E/AndroidRuntime(898): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
06-19 17:15:19.430: E/AndroidRuntime(898): at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
06-19 17:15:19.430: E/AndroidRuntime(898): at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
06-19 17:15:19.430: E/AndroidRuntime(898): at android.view.View.measure(View.java:15518)
06-19 17:15:19.430: E/AndroidRuntime(898): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
06-19 17:15:19.430: E/AndroidRuntime(898): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
06-19 17:15:19.430: E/AndroidRuntime(898): at android.view.View.measure(View.java:15518)
06-19 17:15:19.430: E/AndroidRuntime(898): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
06-19 17:15:19.430: E/AndroidRuntime(898): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
06-19 17:15:19.430: E/AndroidRuntime(898): at android.view.View.measure(View.java:15518)
06-19 17:15:19.430: E/AndroidRuntime(898): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
06-19 17:15:19.430: E/AndroidRuntime(898): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
06-19 17:15:19.430: E/AndroidRuntime(898): at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2176)
06-19 17:15:19.430: E/AndroidRuntime(898): at android.view.View.measure(View.java:15518)
06-19 17:15:19.430: E/AndroidRuntime(898): at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1874)
06-19 17:15:19.430: E/AndroidRuntime(898): at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1089)
06-19 17:15:19.430: E/AndroidRuntime(898): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1265)
06-19 17:15:19.430: E/AndroidRuntime(898): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:989)
06-19 17:15:19.430: E/AndroidRuntime(898): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4351)
06-19 17:15:19.430: E/AndroidRuntime(898): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
06-19 17:15:19.430: E/AndroidRuntime(898): at android.view.Choreographer.doCallbacks(Choreographer.java:562)
06-19 17:15:19.430: E/AndroidRuntime(898): at android.view.Choreographer.doFrame(Choreographer.java:532)
06-19 17:15:19.430: E/AndroidRuntime(898): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
06-19 17:15:19.430: E/AndroidRuntime(898): at android.os.Handler.handleCallback(Handler.java:725)
06-19 17:15:19.430: E/AndroidRuntime(898): at android.os.Handler.dispatchMessage(Handler.java:92)
06-19 17:15:19.430: E/AndroidRuntime(898): at android.os.Looper.loop(Looper.java:137)
06-19 17:15:19.430: E/AndroidRuntime(898): at android.app.ActivityThread.main(ActivityThread.java:5041)
06-19 17:15:19.430: E/AndroidRuntime(898): at java.lang.reflect.Method.invokeNative(Native Method)
06-19 17:15:19.430: E/AndroidRuntime(898): at java.lang.reflect.Method.invoke(Method.java:511)
06-19 17:15:19.430: E/AndroidRuntime(898): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
06-19 17:15:19.430: E/AndroidRuntime(898): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
06-19 17:15:19.430: E/AndroidRuntime(898): at dalvik.system.NativeStart.main(Native Method)
The last code block that is run, when I step through, is:
public void addTeam (View view) {
//open select race dialog
AlertDialog alertDialog;
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
String[] items = new String[races.size()];
int i = 0;
for (Race race : races) {
//add radio button for each race
items[i] = race.raceName;
}
builder.setTitle(getString(R.string.select_race));
builder.setSingleChoiceItems(items, 0, null);
builder.setPositiveButton(R.string.done, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialogInterface, int whichButton) {
dialogInterface.dismiss();
Team team = new Team();
team.raceIndex = ((AlertDialog) dialogInterface).getListView().getCheckedItemPosition();
team.raceName = races.get(team.raceIndex).raceName;
setContentView(R.layout.edit_team);
((TextView) findViewById(R.id.raceTV)).setText(team.raceName);
}
});
alertDialog = builder.create();
alertDialog.show();
}
Without a line reference to my code, I do not even know where to start. Help is appreciated.
Here is an error in your code:
int i = 0;
for (Race race : races) {
//add radio button for each race
items[i] = race.raceName;
}
You don't increment i in the for loop.
May be this causes the crash.
You have two dialog instances. 1) AlertDialog dialog; and 2) DialogInterface dialog. So most probably AlertDialog dialog; throwing NPE as it was never initilized so defaluts to null
Replace this
((TextView) findViewById(R.id.raceTV)).setText(team.raceName);
with
((TextView) dialog.findViewById(R.id.raceTV)).setText(team.raceName);
i am using the services of parse.com, android guide:
https://www.parse.com/docs/android_guide
and it didnt work for me (i get a message: app must be closed...)
here is my code:
gameScore = new ParseObject("GameScore");
gameScore.put("score", 1337);
gameScore.put("playerName", "Sean Plott");
gameScore.put("cheatMode", false);
try {
gameScore.save();
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
gameScore.saveInBackground(new SaveCallback() {
public void done(ParseException e) {
// Access the object id here
objectId= gameScore.getObjectId();
}
});
ParseQuery query = new ParseQuery("GameScore");
query.getInBackground(objectId, new GetCallback() {
#Override
public void done(ParseObject parseObject, ParseException arg1) {
// TODO Auto-generated method stub
int score= parseObject.getInt("score");
Integer i =new Integer(score);
t1.setText(i.toString());
}
});
and here is the Logcat:
02-28 09:59:10.552: E/AndroidRuntime(773): FATAL EXCEPTION: main
02-28 09:59:10.552: E/AndroidRuntime(773): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.salebook/com.example.salebook.MainActivity}: java.lang.NullPointerException
02-28 09:59:10.552: E/AndroidRuntime(773): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
02-28 09:59:10.552: E/AndroidRuntime(773): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
02-28 09:59:10.552: E/AndroidRuntime(773): at android.app.ActivityThread.access$600(ActivityThread.java:141)
02-28 09:59:10.552: E/AndroidRuntime(773): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
02-28 09:59:10.552: E/AndroidRuntime(773): at android.os.Handler.dispatchMessage(Handler.java:99)
02-28 09:59:10.552: E/AndroidRuntime(773): at android.os.Looper.loop(Looper.java:137)
02-28 09:59:10.552: E/AndroidRuntime(773): at android.app.ActivityThread.main(ActivityThread.java:5041)
02-28 09:59:10.552: E/AndroidRuntime(773): at java.lang.reflect.Method.invokeNative(Native Method)
02-28 09:59:10.552: E/AndroidRuntime(773): at java.lang.reflect.Method.invoke(Method.java:511)
02-28 09:59:10.552: E/AndroidRuntime(773): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
02-28 09:59:10.552: E/AndroidRuntime(773): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
02-28 09:59:10.552: E/AndroidRuntime(773): at dalvik.system.NativeStart.main(Native Method)
02-28 09:59:10.552: E/AndroidRuntime(773): Caused by: java.lang.NullPointerException
02-28 09:59:10.552: E/AndroidRuntime(773): at com.example.salebook.MainActivity.onCreate(MainActivity.java:69)
02-28 09:59:10.552: E/AndroidRuntime(773): at android.app.Activity.performCreate(Activity.java:5104)
02-28 09:59:10.552: E/AndroidRuntime(773): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
02-28 09:59:10.552: E/AndroidRuntime(773): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
02-28 09:59:10.552: E/AndroidRuntime(773): ... 11 more
Also, when i am on debugging mode, i see that the objectId is null ,although i can see in the server that the object uploded succesfully with problem and it has a valid objectId.
Also, when i look in the gameScore object in eclipse i can see that it has the same objectId as in the server , however again it return me null in the objectId
thanks
So what is happening is you are executing saveInBackground which starts on a new thread. You will then immediately call query.getInBackground with a null objectId. You need to wait and call query.getInBackground AFTER the done call in gameScore.saveInBackground.
I would recommend moving the second call to the done function call in the gamerScore.saveInBackground execution above. Also you must consider that the save may fail so you will want to catch the exception as follows:
public void done(List<ParseObject> items, ParseException e) {
if (e == null) {
//execute query here
} else {
//handle exception here
}
}