I am creating an app in which the players enter their names into edit texts. This information is retrieved and sent to a second activity through extras. Whenever I run this, I get a Resources$NotFoundException. I am nut sure if this is from my code or from my resources folder.
Here is my code (UPDATED WITHOUT TOAST STATEMENTS)
private void getNames(int number){
Intent intent = new Intent(Setup.this, Test.class);
intent.putExtra("numberofplayers", number);
try {
if(!player1.getText().toString().contentEquals(""))
p1 = player1.getText().toString();
else
p1= "Player 1";
if(!player2.getText().toString().contentEquals(""))
p2 = player2.getText().toString();
else
p2= "Player 2";
if(!player3.getText().toString().contentEquals(""))
p3 = player3.getText().toString();
else
p3= "Player 3";
if(!player4.getText().toString().contentEquals(""))
p4 = player4.getText().toString();
else
p5= "Player 5";
if(!player6.getText().toString().contentEquals(""))
p6 = player6.getText().toString();
else
p6= "Player 6";
if(!player7.getText().toString().contentEquals(""))
p7 = player7.getText().toString();
else
p7= "Player 7";
if(!player8.getText().toString().contentEquals(""))
p8 = player8.getText().toString();
else
p8= "Player 8";
switch (number) {
case 2:
String[] a = {p1,p2};
names=a;
break;
case 3:
String[] b = {p1,p2,p3};
names=b;
break;
case 4:
String[] c = {p1,p2,p3,p4};
names=c;
break;
case 5:
String[] d= {p1,p2,p3,p4,p5};
names=d;
break;
case 6:
String[] e = {p1,p2,p3,p4,p5,p6};
names=e;
break;
case 7:
String[] f = {p1,p2,p3,p4,p5,p6,p7};
names=f;
break;
case 8:
String[] g = {p1,p2,p3,p4,p5,p6,p7,p8};
names=g;
break;
}
} catch (Exception e) {
Log.e("Setup.class Error:", e.getMessage());
}
//Toast.makeText(this, number + "", Toast.LENGTH_SHORT).show();
//for(int q =0;q<names.length;q++)
// Toast.makeText(this, names[q], Toast.LENGTH_SHORT).show();
intent.putExtra("namearray", names);
startActivity(intent);
}
Here is the code for the receiving activity:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle ext = getIntent().getExtras();
int i= ext.getInt("numberofplayers");
String[] names = ext.getStringArray("namearray");
Toast.makeText(this, i, Toast.LENGTH_SHORT).show();
for(int q =0;q<names.length;q++)
Toast.makeText(this, names[q], Toast.LENGTH_SHORT).show();
}
And here is the logcat error (UPDATED LOGCAT REPORT):
06-06 21:21:07.267: E/AndroidRuntime(2538): FATAL EXCEPTION: main
06-06 21:21:07.267: E/AndroidRuntime(2538): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.brightdesign.truthordare/com.brightdesign.truthordare.Test}: android.content.res.Resources$NotFoundException: String resource ID #0x2
06-06 21:21:07.267: E/AndroidRuntime(2538): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
06-06 21:21:07.267: E/AndroidRuntime(2538): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
06-06 21:21:07.267: E/AndroidRuntime(2538): at android.app.ActivityThread.access$600(ActivityThread.java:123)
06-06 21:21:07.267: E/AndroidRuntime(2538): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
06-06 21:21:07.267: E/AndroidRuntime(2538): at android.os.Handler.dispatchMessage(Handler.java:99)
06-06 21:21:07.267: E/AndroidRuntime(2538): at android.os.Looper.loop(Looper.java:137)
06-06 21:21:07.267: E/AndroidRuntime(2538): at android.app.ActivityThread.main(ActivityThread.java:4424)
06-06 21:21:07.267: E/AndroidRuntime(2538): at java.lang.reflect.Method.invokeNative(Native Method)
06-06 21:21:07.267: E/AndroidRuntime(2538): at java.lang.reflect.Method.invoke(Method.java:511)
06-06 21:21:07.267: E/AndroidRuntime(2538): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
06-06 21:21:07.267: E/AndroidRuntime(2538): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
06-06 21:21:07.267: E/AndroidRuntime(2538): at dalvik.system.NativeStart.main(Native Method)
06-06 21:21:07.267: E/AndroidRuntime(2538): Caused by: android.content.res.Resources$NotFoundException: String resource ID #0x2
06-06 21:21:07.267: E/AndroidRuntime(2538): at android.content.res.Resources.getText(Resources.java:247)
06-06 21:21:07.267: E/AndroidRuntime(2538): at android.widget.Toast.makeText(Toast.java:260)
06-06 21:21:07.267: E/AndroidRuntime(2538): at com.brightdesign.truthordare.Test.onCreate(Test.java:16)
06-06 21:21:07.267: E/AndroidRuntime(2538): at android.app.Activity.performCreate(Activity.java:4465)
06-06 21:21:07.267: E/AndroidRuntime(2538): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
06-06 21:21:07.267: E/AndroidRuntime(2538): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
06-06 21:21:07.267: E/AndroidRuntime(2538): ... 11 more
This is your first Toast call:
Toast.makeText(this, number, Toast.LENGTH_SHORT).show();
Here, the second argument is an integer (number), so it thinks that is a string ID. Try replacing number with number+"".
Related
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 writing an Android application in Eclipse that uses RitaWordNet to find synonyms for a word. It compiles but on running it crashes. Is it possible that the app crashes and I receive the message "Unfortunately RitaWord has stopped working". Is it because the jar files I have added to the build path to make the app work are so large (>10 MB)? The program is:
package com.example.ritaword;
import java.util.Arrays;
import android.*
import rita.wordnet.*;
public class MainActivity extends Activity {
TextView A, B;
Button ok;
String[] synonyms;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ok = (Button)findViewById(android.R.id.button1);
A = (TextView)findViewById(R.id.textView1);
B = (TextView)findViewById(R.id.textView2);
B.setText("");
RiWordnet wordnet = new RiWordnet();
String word = wordnet.getRandomWord("a");
synonyms = wordnet.getAllSynonyms(word, "a", 1);
}
public void onClick(View view) {
switch (view.getId()) {
case (R.id.button1):
if (synonyms != null) {
Arrays.sort(synonyms);
B.setText(synonyms[0]);
}
else
B.setText("No synyonyms!");
}
}
#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;
}
}
If it helps, the last few error Logs I receive are:
>04-20 22:25:08.239: E/ActivityThread(730): at com.android.exchange.ExchangeService.getDeviceId(ExchangeService.java:1249)
04-20 22:25:08.239: E/ActivityThread(730): at com.android.exchange.ExchangeService$7.run(ExchangeService.java:1856)
04-20 22:25:08.239: E/ActivityThread(730): at com.android.emailcommon.utility.Utility$2.doInBackground(Utility.java:551)
04-20 22:25:08.239: E/ActivityThread(730): at com.android.emailcommon.utility.Utility$2.doInBackground(Utility.java:549)
04-20 22:25:08.239: E/ActivityThread(730): at android.os.AsyncTask$2.call(AsyncTask.java:287)
04-20 22:25:08.239: E/ActivityThread(730): at java.util.concurrent.FutureTask.run(FutureTask.java:234)
04-20 22:25:08.239: E/ActivityThread(730): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
04-20 22:25:08.239: E/ActivityThread(730): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
04-20 22:25:08.239: E/ActivityThread(730): at java.lang.Thread.run(Thread.java:856)
04-20 22:25:08.319: E/StrictMode(730): null
04-20 22:25:08.319: E/StrictMode(730): android.app.ServiceConnectionLeaked: Service com.android.exchange.ExchangeService has leaked ServiceConnection com.android.emailcommon.service.ServiceProxy$ProxyConnection#40cea428 that was originally bound here
04-20 22:25:08.319: E/StrictMode(730): at android.app.LoadedApk$ServiceDispatcher.<init>(LoadedApk.java:969)
04-20 22:25:08.319: E/StrictMode(730): at android.app.LoadedApk.getServiceDispatcher(LoadedApk.java:863)
04-20 22:25:08.319: E/StrictMode(730): at android.app.ContextImpl.bindService(ContextImpl.java:1418)
04-20 22:25:08.319: E/StrictMode(730): at android.app.ContextImpl.bindService(ContextImpl.java:1407)
04-20 22:25:08.319: E/StrictMode(730): at android.content.ContextWrapper.bindService(ContextWrapper.java:473)
04-20 22:25:08.319: E/StrictMode(730): at com.android.emailcommon.service.ServiceProxy.setTask(ServiceProxy.java:157)
04-20 22:25:08.319: E/StrictMode(730): at com.android.emailcommon.service.ServiceProxy.setTask(ServiceProxy.java:145)
04-20 22:25:08.319: E/StrictMode(730): at com.android.emailcommon.service.AccountServiceProxy.getDeviceId(AccountServiceProxy.java:116)
04-20 22:25:08.319: E/StrictMode(730): at com.android.exchange.ExchangeService.getDeviceId(ExchangeService.java:1249)
04-20 22:25:08.319: E/StrictMode(730): at com.android.exchange.ExchangeService$7.run(ExchangeService.java:1856)
04-20 22:25:08.319: E/StrictMode(730): at com.android.emailcommon.utility.Utility$2.doInBackground(Utility.java:551)
04-20 22:25:08.319: E/StrictMode(730): at com.android.emailcommon.utility.Utility$2.doInBackground(Utility.java:549)
04-20 22:25:08.319: E/StrictMode(730): at android.os.AsyncTask$2.call(AsyncTask.java:287)
04-20 22:25:08.319: E/StrictMode(730): at java.util.concurrent.FutureTask.run(FutureTask.java:234)
04-20 22:25:08.319: E/StrictMode(730): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
04-20 22:25:08.319: E/StrictMode(730): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
04-20 22:25:08.319: E/StrictMode(730): at java.lang.Thread.run(Thread.java:856)
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
}
}
Hi i have developed one android application.
The app purpose is retrieve data from mysql database and display in android device.
This is my android code:
public class RetailerActivity extends Activity {
private final String NAMESPACE = "http://ws.testprops.com";
private final String URL = "http://krish.jelastic.servint.net/Retrieve/services/Fetch?wsdl";
private final String SOAP_ACTION = "http://ws.testprops.com/customerData";
private final String METHOD_NAME = "customerData";
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.setOutputSoapObject(request);
HttpTransportSE ht = new HttpTransportSE(URL);
try {
ht.call(SOAP_ACTION, envelope);
SoapPrimitive response = (SoapPrimitive)envelope.getResponse();
SoapPrimitive s = response;
String str = s.toString();
String resultArr[] = str.split("&");//Result string will split & store in an array
TextView tv = new TextView(this);
for(int i = 0; i<resultArr.length;i++){
tv.append(resultArr[i]+"\n\n");
}
setContentView(tv);
} catch (Exception e) {
e.printStackTrace();
}
}}
If i have to run the app means blank screen only displayed.it is taking too long time nearly 1 hour
also am getting following error on my android console window:
11-05 10:38:27.868: W/System.err(837): java.lang.ClassCastException: org.ksoap2.serialization.SoapObject
11-05 10:38:27.878: W/System.err(837): at com.retailer.client.RetailerActivity.onCreate(RetailerActivity.java:29)
11-05 10:38:27.878: W/System.err(837): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
11-05 10:38:27.878: W/System.err(837): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
11-05 10:38:27.878: W/System.err(837): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
11-05 10:38:27.878: W/System.err(837): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
11-05 10:38:27.878: W/System.err(837): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
11-05 10:38:27.878: W/System.err(837): at android.os.Handler.dispatchMessage(Handler.java:99)
11-05 10:38:27.878: W/System.err(837): at android.os.Looper.loop(Looper.java:123)
11-05 10:38:27.878: W/System.err(837): at android.app.ActivityThread.main(ActivityThread.java:4627)
11-05 10:38:27.878: W/System.err(837): at java.lang.reflect.Method.invokeNative(Native Method)
11-05 10:38:27.878: W/System.err(837): at java.lang.reflect.Method.invoke(Method.java:521)
11-05 10:38:27.878: W/System.err(837): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
11-05 10:38:27.878: W/System.err(837): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
11-05 10:38:27.888: W/System.err(837): at dalvik.system.NativeStart.main(Native Method)
please help me.how can i resolve this error.
Change
SoapPrimitive response = (SoapPrimitive)envelope.getResponse();
to
SoapObject response = (SoapObject)envelope.getResponse();
I'm trying to start a service, but always getting NPE. What could I be doing wrong?
Even the onServiceConnected() method does not get called, why ever?
#Override
protected void onCreate(Bundle savedInstanceState) {
mServiceIntent = new Intent(this, MyService.class);
bindService(mServiceIntent, mConnection, Context.BIND_AUTO_CREATE);
//other stuff
}
usage:
private okButton(View v) {
startService(mServiceIntent);
}
result: NullPointerException!
private ServiceConnection mConnection = new ServiceConnection() {
#Override
public void onServiceConnected(ComponentName className, IBinder service) {
Log.v("service", "gets never executed!");
mDownloadService = ((LocalBinder<MyService>) service).getService();
}
#Override
public void onServiceDisconnected(ComponentName className) {
}
};
}
public class LocalBinder<S> extends Binder {
private String TAG = "LocalBinder";
private WeakReference<S> mService;
public LocalBinder(S service){
mService = new WeakReference<S>(service);
}
public S getService() {
return mService.get();
}
}
Of course I also have the service in manifest:
<service android:enabled="true" android:name=".service.MyService" />
Logcat:
06-06 23:48:20.411: W/dalvikvm(592): threadid=1: thread exiting with uncaught exception (group=0x409c01f8)
06-06 23:48:20.461: E/AndroidRuntime(592): FATAL EXCEPTION: main
06-06 23:48:20.461: E/AndroidRuntime(592): java.lang.RuntimeException: Unable to start activity ComponentInfo{MyActivity}: java.lang.NullPointerException
06-06 23:48:20.461: E/AndroidRuntime(592): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
06-06 23:48:20.461: E/AndroidRuntime(592): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
06-06 23:48:20.461: E/AndroidRuntime(592): at android.app.ActivityThread.access$600(ActivityThread.java:123)
06-06 23:48:20.461: E/AndroidRuntime(592): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
06-06 23:48:20.461: E/AndroidRuntime(592): at android.os.Handler.dispatchMessage(Handler.java:99)
06-06 23:48:20.461: E/AndroidRuntime(592): at android.os.Looper.loop(Looper.java:137)
06-06 23:48:20.461: E/AndroidRuntime(592): at android.app.ActivityThread.main(ActivityThread.java:4424)
06-06 23:48:20.461: E/AndroidRuntime(592): at java.lang.reflect.Method.invokeNative(Native Method)
06-06 23:48:20.461: E/AndroidRuntime(592): at java.lang.reflect.Method.invoke(Method.java:511)
06-06 23:48:20.461: E/AndroidRuntime(592): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
06-06 23:48:20.461: E/AndroidRuntime(592): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
06-06 23:48:20.461: E/AndroidRuntime(592): at dalvik.system.NativeStart.main(Native Method)
06-06 23:48:20.461: E/AndroidRuntime(592): Caused by: java.lang.NullPointerException
06-06 23:48:20.461: E/AndroidRuntime(592): at MyActivity.okButton(MyActivity.java:217)
06-06 23:48:20.461: E/AndroidRuntime(592): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1939)
06-06 23:48:20.461: E/AndroidRuntime(592): ... 11 more
The intent seems to be ok:
Log.v("service", String.valueOf(mServiceIntent == null)); //false
Your stack trace clearly shows the NullPointerException happening in okButton(), and according to the code you posted, the only line in that function is
startService(mServiceIntent);
So clearly mServiceIntent is null at this point, despite being set in onCreate().
Do you have any other lines where you set mServiceIntent?
Or do you have a constructor in which you call okButton()? A constructor is the only thing I can think of that would run before onCreate(). That would also explain why onServiceConnected() is never run despite the call to bindService() in onCreate().
This code is not proper Java syntax, as the method definition has no return type :
private okButton(View v) {
startService(mServiceIntent);
}
You need something like:
private void okButton(View v) {
startService(mServiceIntent);
}
It looks to me from the stack trace that the okButton() method is being called during the creation of the MyActivity instance before the onCreate() method is called and of course at that time mService will be null.
Post more code or correct the code you've posted so that it actually matches what you are using.