Hi I am writing a simple app that interacts with androids camera API directly, I'm using this tutorial http://www.tutorialspoint.com/android/android_camera.htm
Could anyone show me where I am going wrong?
The log output:
01-09 23:42:56.901: D/ATRecorder(886): com.htc.autotest.dlib.RecordEngine in loader dalvik.system.DexClassLoader#40528bb8
01-09 23:42:57.161: D/AndroidRuntime(886): Shutting down VM
01-09 23:42:57.161: W/dalvikvm(886): threadid=1: thread exiting with uncaught exception (group=0x400205a0)
01-09 23:42:57.171: E/AndroidRuntime(886): FATAL EXCEPTION: main
01-09 23:42:57.171: E/AndroidRuntime(886): java.lang.RuntimeException: startPreview failed
01-09 23:42:57.171: E/AndroidRuntime(886): at android.hardware.Camera.startPreview(Native Method)
01-09 23:42:57.171: E/AndroidRuntime(886): at com.example.camera1.ShowCamera.surfaceCreated(ShowCamera.java:30)
01-09 23:42:57.171: E/AndroidRuntime(886): at android.view.SurfaceView.updateWindow(SurfaceView.java:551)
01-09 23:42:57.171: E/AndroidRuntime(886): at android.view.SurfaceView.dispatchDraw(SurfaceView.java:348)
01-09 23:42:57.171: E/AndroidRuntime(886): at android.view.ViewGroup.drawChild(ViewGroup.java:1730)
01-09 23:42:57.171: E/AndroidRuntime(886): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1459)
01-09 23:42:57.171: E/AndroidRuntime(886): at android.view.ViewGroup.drawChild(ViewGroup.java:1730)
01-09 23:42:57.171: E/AndroidRuntime(886): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1459)
01-09 23:42:57.171: E/AndroidRuntime(886): at android.view.ViewGroup.drawChild(ViewGroup.java:1730)
01-09 23:42:57.171: E/AndroidRuntime(886): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1459)
01-09 23:42:57.171: E/AndroidRuntime(886): at android.view.ViewGroup.drawChild(ViewGroup.java:1730)
01-09 23:42:57.171: E/AndroidRuntime(886): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1459)
01-09 23:42:57.171: E/AndroidRuntime(886): at android.view.View.draw(View.java:6973)
01-09 23:42:57.171: E/AndroidRuntime(886): at android.widget.FrameLayout.draw(FrameLayout.java:357)
01-09 23:42:57.171: E/AndroidRuntime(886): at android.view.ViewGroup.drawChild(ViewGroup.java:1732)
01-09 23:42:57.171: E/AndroidRuntime(886): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1459)
01-09 23:42:57.171: E/AndroidRuntime(886): at android.view.ViewGroup.drawChild(ViewGroup.java:1730)
01-09 23:42:57.171: E/AndroidRuntime(886): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1459)
01-09 23:42:57.171: E/AndroidRuntime(886): at android.view.View.draw(View.java:6973)
01-09 23:42:57.171: E/AndroidRuntime(886): at android.widget.FrameLayout.draw(FrameLayout.java:357)
01-09 23:42:57.171: E/AndroidRuntime(886): at com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:1997)
01-09 23:42:57.171: E/AndroidRuntime(886): at android.view.ViewRoot.draw(ViewRoot.java:1600)
01-09 23:42:57.171: E/AndroidRuntime(886): at android.view.ViewRoot.performTraversals(ViewRoot.java:1321)
01-09 23:42:57.171: E/AndroidRuntime(886): at android.view.ViewRoot.handleMessage(ViewRoot.java:1957)
01-09 23:42:57.171: E/AndroidRuntime(886): at android.os.Handler.dispatchMessage(Handler.java:99)
01-09 23:42:57.171: E/AndroidRuntime(886): at android.os.Looper.loop(Looper.java:150)
01-09 23:42:57.171: E/AndroidRuntime(886): at android.app.ActivityThread.main(ActivityThread.java:4277)
01-09 23:42:57.171: E/AndroidRuntime(886): at java.lang.reflect.Method.invokeNative(Native Method)
01-09 23:42:57.171: E/AndroidRuntime(886): at java.lang.reflect.Method.invoke(Method.java:507)
01-09 23:42:57.171: E/AndroidRuntime(886): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
01-09 23:42:57.171: E/AndroidRuntime(886): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
01-09 23:42:57.171: E/AndroidRuntime(886): at dalvik.system.NativeStart.main(Native Method)
I've followed it perfectly but I still get and uncaught exception in log-cat when I try to run it, here is the code in MainActivity:
package com.example.camera1;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.hardware.Camera;
import android.hardware.Camera.PictureCallback;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.Toast;
public class MainActivity extends Activity {
private Camera cameraObject;
private ShowCamera showCamera;
private ImageView pic;
public static Camera isCameraAvailiable(){
Camera object = null;
try {
object = Camera.open();
}
catch (Exception e){
}
return object;
}
private PictureCallback capturedIt = new PictureCallback() {
#Override
public void onPictureTaken(byte[] data, Camera camera) {
Bitmap bitmap = BitmapFactory.decodeByteArray(data , 0, data .length);
if(bitmap==null){
Toast.makeText(getApplicationContext(), "not taken", Toast.LENGTH_SHORT).show();
}
else
{
Toast.makeText(getApplicationContext(), "taken", Toast.LENGTH_SHORT).show();
}
cameraObject.release();
}
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
pic = (ImageView)findViewById(R.id.imageView1);
cameraObject = isCameraAvailiable();
showCamera = new ShowCamera(this, cameraObject);
FrameLayout preview = (FrameLayout) findViewById(R.id.camera_preview);
preview.addView(showCamera);
}
public void snapIt(View view){
cameraObject.takePicture(null, null, capturedIt);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
Here is the ShowCamera class:
package com.example.camera1;
import java.io.IOException;
import android.content.Context;
import android.hardware.Camera;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
public class ShowCamera extends SurfaceView implements SurfaceHolder.Callback {
private SurfaceHolder holdMe;
private Camera theCamera;
public ShowCamera(Context context,Camera camera) {
super(context);
theCamera = camera;
holdMe = getHolder();
holdMe.addCallback(this);
}
#Override
public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {
}
#Override
public void surfaceCreated(SurfaceHolder holder) {
try {
theCamera.setPreviewDisplay(holder);
theCamera.startPreview();
} catch (IOException e) {
}
}
#Override
public void surfaceDestroyed(SurfaceHolder arg0) {
}
}
There may be different reasons for Camera.startPreview() to throw RuntimeException. For example, it could be by set of preview size not to one of supported preview sizes.
Try to catch a top level Exception in the surfaceCreated method (instead of IOException) and call e.getMessage() in the exception block to get more information on the type of error.
Related
I was trying to execute Mr Nom(from the book Begining Android Games by Mario Zechner). but it doesn't work. My first atemp was create 2 different projects, one called framework where are com.badlogic.androidgames.framework and com.badlogic.androidgames.framework files and other project called mrnom (com.badlogic.androidgames.mrnom)with the rest of files. the framework project was added mrnom project/java build path/project/add and the result was this:
logcat
11-07 15:21:55.573: W/dalvikvm(641): Unable to resolve superclass of Lcom/badlogic/androidgames/mrnom/MrNom; (445)
11-07 15:21:55.573: W/dalvikvm(641): Link of class 'Lcom/badlogic/androidgames/mrnom/MrNom;' failed
11-07 15:21:55.573: D/AndroidRuntime(641): Shutting down VM
11-07 15:21:55.573: W/dalvikvm(641): threadid=1: thread exiting with uncaught exception (group=0x40a13300)
11-07 15:21:55.583: E/AndroidRuntime(641): FATAL EXCEPTION: main
11-07 15:21:55.583: E/AndroidRuntime(641): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.badlogic.androidgames.mrnom/com.badlogic.androidgames.mrnom.MrNom}: java.lang.ClassNotFoundException: com.badlogic.androidgames.mrnom.MrNom
11-07 15:21:55.583: E/AndroidRuntime(641): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1983)
11-07 15:21:55.583: E/AndroidRuntime(641): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
11-07 15:21:55.583: E/AndroidRuntime(641): at android.app.ActivityThread.access$600(ActivityThread.java:130)
11-07 15:21:55.583: E/AndroidRuntime(641): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
11-07 15:21:55.583: E/AndroidRuntime(641): at android.os.Handler.dispatchMessage(Handler.java:99)
11-07 15:21:55.583: E/AndroidRuntime(641): at android.os.Looper.loop(Looper.java:137)
11-07 15:21:55.583: E/AndroidRuntime(641): at android.app.ActivityThread.main(ActivityThread.java:4745)
11-07 15:21:55.583: E/AndroidRuntime(641): at java.lang.reflect.Method.invokeNative(Native Method)
11-07 15:21:55.583: E/AndroidRuntime(641): at java.lang.reflect.Method.invoke(Method.java:511)
11-07 15:21:55.583: E/AndroidRuntime(641): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
11-07 15:21:55.583: E/AndroidRuntime(641): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
11-07 15:21:55.583: E/AndroidRuntime(641): at dalvik.system.NativeStart.main(Native Method)
11-07 15:21:55.583: E/AndroidRuntime(641): Caused by: java.lang.ClassNotFoundException: com.badlogic.androidgames.mrnom.MrNom
11-07 15:21:55.583: E/AndroidRuntime(641): at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:61)
11-07 15:21:55.583: E/AndroidRuntime(641): at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
11-07 15:21:55.583: E/AndroidRuntime(641): at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
11-07 15:21:55.583: E/AndroidRuntime(641): at android.app.Instrumentation.newActivity(Instrumentation.java:1053)
11-07 15:21:55.583: E/AndroidRuntime(641): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1974)
11-07 15:21:55.583: E/AndroidRuntime(641): ... 11 more
11-07 15:24:06.602: E/Trace(662): error opening trace file: No such file or directory (2)
11-07 15:24:06.902: W/dalvikvm(662): Unable to resolve superclass of Lcom/badlogic/androidgames/mrnom/MrNom; (445)
11-07 15:24:06.902: W/dalvikvm(662): Link of class 'Lcom/badlogic/androidgames/mrnom/MrNom;' failed
11-07 15:24:06.902: D/AndroidRuntime(662): Shutting down VM
11-07 15:24:06.932: W/dalvikvm(662): threadid=1: thread exiting with uncaught exception (group=0x40a13300)
11-07 15:24:06.952: E/AndroidRuntime(662): FATAL EXCEPTION: main
11-07 15:24:06.952: E/AndroidRuntime(662): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.badlogic.androidgames.mrnom/com.badlogic.androidgames.mrnom.MrNom}: java.lang.ClassNotFoundException: com.badlogic.androidgames.mrnom.MrNom
11-07 15:24:06.952: E/AndroidRuntime(662): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1983)
11-07 15:24:06.952: E/AndroidRuntime(662): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
11-07 15:24:06.952: E/AndroidRuntime(662): at android.app.ActivityThread.access$600(ActivityThread.java:130)
11-07 15:24:06.952: E/AndroidRuntime(662): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
11-07 15:24:06.952: E/AndroidRuntime(662): at android.os.Handler.dispatchMessage(Handler.java:99)
11-07 15:24:06.952: E/AndroidRuntime(662): at android.os.Looper.loop(Looper.java:137)
11-07 15:24:06.952: E/AndroidRuntime(662): at android.app.ActivityThread.main(ActivityThread.java:4745)
11-07 15:24:06.952: E/AndroidRuntime(662): at java.lang.reflect.Method.invokeNative(Native Method)
11-07 15:24:06.952: E/AndroidRuntime(662): at java.lang.reflect.Method.invoke(Method.java:511)
11-07 15:24:06.952: E/AndroidRuntime(662): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
11-07 15:24:06.952: E/AndroidRuntime(662): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
11-07 15:24:06.952: E/AndroidRuntime(662): at dalvik.system.NativeStart.main(Native Method)
11-07 15:24:06.952: E/AndroidRuntime(662): Caused by: java.lang.ClassNotFoundException: com.badlogic.androidgames.mrnom.MrNom
11-07 15:24:06.952: E/AndroidRuntime(662): at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:61)
11-07 15:24:06.952: E/AndroidRuntime(662): at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
11-07 15:24:06.952: E/AndroidRuntime(662): at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
11-07 15:24:06.952: E/AndroidRuntime(662): at android.app.Instrumentation.newActivity(Instrumentation.java:1053)
11-07 15:24:06.952: E/AndroidRuntime(662): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1974)
11-07 15:24:06.952: E/AndroidRuntime(662): ... 11 more
later I removed the framework from mrnom and added it using a jar file, the result was the same
Now, I copy the com.badlogic.androidgames.framework and com.badlogic.androidgames.framework files into mr project then I have com.badlogic.androidgames.mrnom, com.badlogic.androidgames.framework and com.badlogic.androidgames.framework in the same project but now the error is
logcat
12-05 15:42:43.510: D/dalvikvm(582): GC_EXTERNAL_ALLOC freed 57K, 53% free 2569K/5379K, external 1925K/2137K, paused 239ms
12-05 15:42:43.860: D/dalvikvm(582): GC_EXTERNAL_ALLOC freed 3K, 53% free 2569K/5379K, external 2515K/2701K, paused 36ms
12-05 15:42:44.100: W/dalvikvm(582): threadid=11: thread exiting with uncaught exception (group=0x40015560)
12-05 15:42:44.100: E/AndroidRuntime(582): FATAL EXCEPTION: Thread-12
12-05 15:42:44.100: E/AndroidRuntime(582): java.lang.NullPointerException
12-05 15:42:44.100: E/AndroidRuntime(582): at com.badlogic.androidgames.mrnom.Settings.load(Settings.java:18)
12-05 15:42:44.100: E/AndroidRuntime(582): at com.badlogic.androidgames.mrnom.LoadingScreen.update(LoadingScreen.java:41)
12-05 15:42:44.100: E/AndroidRuntime(582): at com.badlogic.androidgames.framework.impl.AndroidFastRenderView.run(AndroidFastRenderView.java:36)
12-05 15:42:44.100: E/AndroidRuntime(582): at java.lang.Thread.run(Thread.java:1019)
Settings
package com.badlogic.androidgames.mrnom;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import com.balogic.androidgames.framework.FileIO;
public class Settings {
public static boolean soundEnable=true;
public static int[] highscores=new int[]{100,80,50,30,10};
public static void load(FileIO files){
BufferedReader in=null;
try{
in=new BufferedReader(new InputStreamReader(files.readFile(".mrnom")));// line 18 error here
soundEnable=Boolean.parseBoolean(in.readLine());
for(int i=0; i<5;i++){
highscores[i]=Integer.parseInt(in.readLine());
}
}catch(IOException e){
// :( vale,trabajamos con valores predeterminados
}catch(NumberFormatException e){
// :/ vale, estos valores nos han salvado el día
}finally{
try{
if(in!=null)
in.close();
}catch(IOException e){
//nada
}
}
}
public static void save(FileIO files){
BufferedWriter out=null;
try{
out=new BufferedWriter(new OutputStreamWriter(files.writeFile(".mrnom")));
out.write(Boolean.toString(soundEnable));
for(int i=0; i<5; i++){
out.write(Integer.toString(highscores[i]));
}
}catch(IOException e){
//nada
}finally{
try{
if(out!=null)
out.close();
}catch(IOException e){
//nada
}
}
}
public static void addScore(int score){
for(int i=0;i<5;i++){
if(highscores[i]<score){
for(int j=4;j>i;j--)
highscores[j]=highscores[j-1];
highscores[i]=score;
break;
}
}
}
}
Loadingsceen
package com.badlogic.androidgames.mrnom;
import com.badlogic.androidgames.framework.Game;
import com.badlogic.androidgames.framework.Graphics;
import com.badlogic.androidgames.framework.Graphics.PixmapFormat;
import com.badlogic.androidgames.framework.Screen;
public class LoadingScreen extends Screen{
public LoadingScreen(Game game){
super(game);
}
#Override
public void update(float deltaTime){
Graphics g=game.getGraphics();
Assets.background = g.newPixmap("background.png", PixmapFormat.RGB565);
Assets.logo = g.newPixmap("logo.png", PixmapFormat.ARGB4444);
Assets.mainMenu = g.newPixmap("mainmenu.png", PixmapFormat.ARGB4444);
Assets.buttons = g.newPixmap("buttons.png", PixmapFormat.ARGB4444);
Assets.help1 = g.newPixmap("help1.png", PixmapFormat.ARGB4444);
Assets.help2 = g.newPixmap("help2.png", PixmapFormat.ARGB4444);
Assets.help3 = g.newPixmap("help3.png", PixmapFormat.ARGB4444);
Assets.numbers = g.newPixmap("numbers.png", PixmapFormat.ARGB4444);
Assets.ready = g.newPixmap("ready.png", PixmapFormat.ARGB4444);
Assets.pause = g.newPixmap("pausemenu.png", PixmapFormat.ARGB4444);
Assets.gameOver = g.newPixmap("gameover.png", PixmapFormat.ARGB4444);
Assets.headUp = g.newPixmap("headup.png", PixmapFormat.ARGB4444);
Assets.headLeft = g.newPixmap("headleft.png", PixmapFormat.ARGB4444);
Assets.headDown = g.newPixmap("headdown.png", PixmapFormat.ARGB4444);
Assets.headRight = g.newPixmap("headright.png", PixmapFormat.ARGB4444);
Assets.tail = g.newPixmap("tail.png", PixmapFormat.ARGB4444);
Assets.stain1 = g.newPixmap("stain.png", PixmapFormat.ARGB4444);
Assets.stain2 = g.newPixmap("stain2.png", PixmapFormat.ARGB4444);
Assets.stain3 = g.newPixmap("stain3.png", PixmapFormat.ARGB4444);
Assets.click = game.getAudio().newSound("click.ogg");
Assets.eat = game.getAudio().newSound("eat.ogg");
Assets.bitten = game.getAudio().newSound("bitten.ogg");
Settings.load(game.getFileIO()); //line 41 error here
game.setScreen(new MainMenuScreen(game));
}
#Override
public void present(float deltaTime) {
// TODO Auto-generated method stub
}
#Override
public void pause() {
// TODO Auto-generated method stub
}
#Override
public void resume() {
// TODO Auto-generated method stub
}
#Override
public void dispose() {
// TODO Auto-generated method stub
}
}
AndroidFastRenderView
package com.badlogic.androidgames.framework.impl;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
public class AndroidFastRenderView extends SurfaceView implements Runnable{
AndroidGame game;
Bitmap framebuffer;
Thread renderThread=null;
SurfaceHolder holder;
volatile boolean running=false;
public AndroidFastRenderView(AndroidGame game,Bitmap framebuffer){
super(game);
this.game=game;
this.framebuffer=framebuffer;
this.holder=getHolder();
}
public void resume(){
running=true;
renderThread=new Thread(this);
renderThread.start();
}
public void run(){
Rect dstRect=new Rect();
long startTime=System.nanoTime();
while(running){
if(!holder.getSurface().isValid())
continue;
float deltaTime=(System.nanoTime()-startTime)/1000000000.0f;
startTime=System.nanoTime();
game.getCurrentScreen().update(deltaTime);//line 36 error here
game.getCurrentScreen().present(deltaTime);
Canvas canvas=holder.lockCanvas();
canvas.getClipBounds(dstRect);
canvas.drawBitmap(framebuffer, null, dstRect,null);
holder.unlockCanvasAndPost(canvas);
}
}
public void pause(){
running=false;
while(true){
try{
renderThread.join();
break;
}catch(InterruptedException e){
//vuelve a intentarlo
}
}
}
}
Eclipse doesn't showme any mistake on code but on the emulator just say "Sorry! the application mrnom (process com.badlogic.androidgames.mrnom) has stopped unexpectedly. Please try again"
Can you help me with this? No idea what's happening :( Thank you so much.
This question already has answers here:
How can I fix 'android.os.NetworkOnMainThreadException'?
(66 answers)
Closed 8 years ago.
I had tried many ways to make an http request in android. Logcat keeps giving me errors no matter what i do. I need some help here. I added
uses-permission android:name="android.permission.INTERNET">
to manifest file
I had checked my connection and it says it is connected...
code :
package com.example.gpschat;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.StatusLine;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import android.support.v7.app.ActionBarActivity;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.os.Message;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TextView;
public class MainActivity extends ActionBarActivity {
String[] messages;
LinearLayout.LayoutParams lp;
TextView tv;
#Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String m = "";
try {
m = excutePost();
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Log.d("gilad", m);
messages = m.split(",");
LinearLayout ll = (LinearLayout) findViewById(R.id.chat);
for (String message : messages)
{
lp = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
tv = new TextView(this);
tv.setId(1);
tv.setTextSize(15);
tv.setText(message);
tv.setLayoutParams(lp);
(ll).addView(tv);
}
}
#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
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);
}
public static String excutePost() throws IOException
{
HttpClient httpclient = new DefaultHttpClient();
HttpResponse response = httpclient.execute(new HttpGet("http://laurawharton.com/gps/getM.php"));
StatusLine statusLine = response.getStatusLine();
if(statusLine.getStatusCode() == HttpStatus.SC_OK){
ByteArrayOutputStream out = new ByteArrayOutputStream();
response.getEntity().writeTo(out);
out.close();
String responseString = out.toString();
//..more logic
return responseString;
} else{
//Closes the connection.
response.getEntity().getContent().close();
throw new IOException(statusLine.getReasonPhrase());
}
}
}
error:
09-02 11:43:10.873: W/dalvikvm(1234): threadid=1: thread exiting with uncaught exception (group=0xb3ac8ba8)
09-02 11:43:10.893: E/AndroidRuntime(1234): FATAL EXCEPTION: main
09-02 11:43:10.893: E/AndroidRuntime(1234): Process: com.example.gpschat, PID: 1234
09-02 11:43:10.893: E/AndroidRuntime(1234): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.gpschat/com.example.gpschat.MainActivity}: android.os.NetworkOnMainThreadException
09-02 11:43:10.893: E/AndroidRuntime(1234): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
09-02 11:43:10.893: E/AndroidRuntime(1234): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
09-02 11:43:10.893: E/AndroidRuntime(1234): at android.app.ActivityThread.access$800(ActivityThread.java:135)
09-02 11:43:10.893: E/AndroidRuntime(1234): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
09-02 11:43:10.893: E/AndroidRuntime(1234): at android.os.Handler.dispatchMessage(Handler.java:102)
09-02 11:43:10.893: E/AndroidRuntime(1234): at android.os.Looper.loop(Looper.java:136)
09-02 11:43:10.893: E/AndroidRuntime(1234): at android.app.ActivityThread.main(ActivityThread.java:5017)
09-02 11:43:10.893: E/AndroidRuntime(1234): at java.lang.reflect.Method.invokeNative(Native Method)
09-02 11:43:10.893: E/AndroidRuntime(1234): at java.lang.reflect.Method.invoke(Method.java:515)
09-02 11:43:10.893: E/AndroidRuntime(1234): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
09-02 11:43:10.893: E/AndroidRuntime(1234): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
09-02 11:43:10.893: E/AndroidRuntime(1234): at dalvik.system.NativeStart.main(Native Method)
09-02 11:43:10.893: E/AndroidRuntime(1234): Caused by: android.os.NetworkOnMainThreadException
09-02 11:43:10.893: E/AndroidRuntime(1234): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1145)
09-02 11:43:10.893: E/AndroidRuntime(1234): at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
09-02 11:43:10.893: E/AndroidRuntime(1234): at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
09-02 11:43:10.893: E/AndroidRuntime(1234): at java.net.InetAddress.getAllByName(InetAddress.java:214)
09-02 11:43:10.893: E/AndroidRuntime(1234): at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:137)
09-02 11:43:10.893: E/AndroidRuntime(1234): at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
09-02 11:43:10.893: E/AndroidRuntime(1234): at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
09-02 11:43:10.893: E/AndroidRuntime(1234): at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
09-02 11:43:10.893: E/AndroidRuntime(1234): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
09-02 11:43:10.893: E/AndroidRuntime(1234): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
09-02 11:43:10.893: E/AndroidRuntime(1234): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
09-02 11:43:10.893: E/AndroidRuntime(1234): at com.example.gpschat.MainActivity.excutePost(MainActivity.java:89)
09-02 11:43:10.893: E/AndroidRuntime(1234): at com.example.gpschat.MainActivity.onCreate(MainActivity.java:44)
09-02 11:43:10.893: E/AndroidRuntime(1234): at android.app.Activity.performCreate(Activity.java:5231)
09-02 11:43:10.893: E/AndroidRuntime(1234): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
09-02 11:43:10.893: E/AndroidRuntime(1234): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
09-02 11:43:10.893: E/AndroidRuntime(1234): ... 11 more
09-02 11:43:16.833: I/Process(1234): Sending signal. PID: 1234 SIG: 9
try with AsyncTask
I was just wanted write short comment, but I'm not able to write a comment. I'm sorry.
AsyncTask will help you.
If you need a tutorial, take a look.
link : AsyncTask Android example
I would like to create applications forming charts based on data from parse.com. I have read some examples and tutorials but still have problem with displaying charts. Below is my code:
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.util.Log;
import com.parse.GetCallback;
import com.parse.ParseException;
import com.parse.ParseObject;
import com.parse.ParseQuery;
import org.achartengine.ChartFactory;
import org.achartengine.GraphicalView;
import org.achartengine.chart.PointStyle;
import org.achartengine.model.XYMultipleSeriesDataset;
import org.achartengine.model.XYSeries;
import org.achartengine.renderer.XYMultipleSeriesRenderer;
import org.achartengine.renderer.XYSeriesRenderer;
import java.util.ArrayList;
public class LineGraph {
public ArrayList<Integer> dataArray;
XYMultipleSeriesDataset dataset;
XYMultipleSeriesRenderer renderer;
public static boolean ClickEnabled = true;
public Intent getIntent(Context context) {
ArrayList<Integer> y = this.dataArray;
XYSeries seriesY = new XYSeries("Y");
for (int i = 0; i < y.size(); i++) {
seriesY.add(i, y.get(i));
}
dataset = new XYMultipleSeriesDataset();
dataset.addSeries(seriesY);
renderer.setPanEnabled(true, false);
renderer.setClickEnabled(ClickEnabled);
renderer.setBackgroundColor(Color.WHITE);
renderer.setApplyBackgroundColor(true);
renderer.setChartTitle("Simple data");
renderer.setAxesColor(Color.BLACK);
XYMultipleSeriesRenderer mRenderer = new XYMultipleSeriesRenderer();
XYSeriesRenderer renderer = new XYSeriesRenderer();
renderer.setColor(Color.RED);
renderer.setPointStyle(PointStyle.DIAMOND);
mRenderer.addSeriesRenderer(renderer);
Intent intent = ChartFactory.getLineChartIntent(context, dataset, mRenderer, "Line Graph Title");
return intent;
}
public void getData() {
ParseQuery<ParseObject> query = ParseQuery.getQuery("Counters_data");
query.getInBackground("lxFzCTeOcl", new GetCallback<ParseObject>() {
public void done(ParseObject parseObject, ParseException e) {
if (e == null) {
String object = parseObject.getString("value");
Integer objectValue = Integer.parseInt(object);
if (dataArray == null) {
dataArray = new ArrayList<Integer>();
dataArray.add(objectValue);
}
} else {
Log.d("score", "Error: " + e.getMessage());
}
}
});
}
}
And there is how I invoke charts:
public void lineGraphHandler(View view) {
LineGraph line = new LineGraph();
line.getData();
Intent lineIntent = line.getIntent(this);
startActivity(lineIntent);
}
And XML part:
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/counters"
android:onClick="lineGraphHandler"
android:text="Charts"
android:id="#+id/charts"/>
There is my logcat:
03-26 08:42:13.096 1229-1229/com.example.tst D/dalvikvm﹕ Late-enabling
CheckJNI 03-26 08:42:13.487 1229-1229/com.example.tst D/libEGL﹕ loaded
/system/lib/egl/libEGL_genymotion.so 03-26 08:42:13.491
1229-1229/com.example.tst D/﹕ HostConnection::get() New Host
Connection established 0xb94f4270, tid 1229 03-26 08:42:13.551
1229-1229/com.example.tst D/libEGL﹕ loaded
/system/lib/egl/libGLESv1_CM_genymotion.so 03-26 08:42:13.551
1229-1229/com.example.tst D/libEGL﹕ loaded
/system/lib/egl/libGLESv2_genymotion.so 03-26 08:42:14.035
1229-1229/com.example.tst W/EGL_genymotion﹕ eglSurfaceAttrib not
implemented 03-26 08:42:14.039 1229-1229/com.example.tst
E/OpenGLRenderer﹕ Getting MAX_TEXTURE_SIZE from GradienCache 03-26
08:42:14.043 1229-1229/com.example.tst E/OpenGLRenderer﹕
MAX_TEXTURE_SIZE: 4096 03-26 08:42:14.055 1229-1229/com.example.tst
E/OpenGLRenderer﹕ Getting MAX_TEXTURE_SIZE from
Caches::initConstraints() 03-26 08:42:14.063 1229-1229/com.example.tst
E/OpenGLRenderer﹕ MAX_TEXTURE_SIZE: 4096 03-26 08:42:14.063
1229-1229/com.example.tst D/OpenGLRenderer﹕ Enabling debug mode 0
03-26 08:42:50.327 1229-1229/com.example.tst D/dalvikvm﹕ GC_FOR_ALLOC
freed 200K, 8% free 2975K/3228K, paused 10ms, total 13ms 03-26
08:42:51.675 1229-1229/com.example.tst D/dalvikvm﹕ GC_FOR_ALLOC freed
431K, 14% free 3056K/3540K, paused 22ms, total 28ms 03-26 08:42:52.043
1229-1229/com.example.tst W/EGL_genymotion﹕ eglSurfaceAttrib not
implemented 03-26 08:42:53.543 1229-1229/com.example.tst
I/Choreographer﹕ Skipped 89 frames! The application may be doing too
much work on its main thread. 03-26 08:43:01.747
1229-1229/com.example.tst D/AndroidRuntime﹕ Shutting down VM 03-26
08:43:01.747 1229-1229/com.example.tst W/dalvikvm﹕ threadid=1: thread
exiting with uncaught exception (group=0xa4d8fb20) 03-26 08:43:01.767
1229-1229/com.example.tst E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.tst, PID: 1229 java.lang.IllegalStateException:
Could not execute method of the activity at
android.view.View$1.onClick(View.java:3823) at
android.view.View.performClick(View.java:4438) at
android.view.View$PerformClick.run(View.java:18422) at
android.os.Handler.handleCallback(Handler.java:733) at
android.os.Handler.dispatchMessage(Handler.java:95) at
android.os.Looper.loop(Looper.java:136) at
android.app.ActivityThread.main(ActivityThread.java:5017) 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:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) at
dalvik.system.NativeStart.main(Native Method) Caused by:
java.lang.reflect.InvocationTargetException at
java.lang.reflect.Method.invokeNative(Native Method) at
java.lang.reflect.Method.invoke(Method.java:515) at
android.view.View$1.onClick(View.java:3818) at
android.view.View.performClick(View.java:4438) at
android.view.View$PerformClick.run(View.java:18422) at
android.os.Handler.handleCallback(Handler.java:733) at
android.os.Handler.dispatchMessage(Handler.java:95) at
android.os.Looper.loop(Looper.java:136) at
android.app.ActivityThread.main(ActivityThread.java:5017)
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:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method) Caused by:
java.lang.NullPointerException at
com.example.tst.LineGraph.getIntent(LineGraph.java:36) at
com.example.tst.MainActivity.lineGraphHandler(MainActivity.java:44)
at java.lang.reflect.Method.invokeNative(Native Method) at
java.lang.reflect.Method.invoke(Method.java:515) at
android.view.View$1.onClick(View.java:3818) at
android.view.View.performClick(View.java:4438) at
android.view.View$PerformClick.run(View.java:18422) at
android.os.Handler.handleCallback(Handler.java:733) at
android.os.Handler.dispatchMessage(Handler.java:95) at
android.os.Looper.loop(Looper.java:136) at
android.app.ActivityThread.main(ActivityThread.java:5017)
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:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method) 03-26 08:43:04.507
1229-1229/com.example.tst I/Process﹕ Sending signal. PID: 1229 SIG: 9
I don't understand where the problem is. My app starts but crashes immediately when I push "chart" button. Is it data type of problem or because I misunderstand something?
Thank you in advance.
I tried like this but still got crash:
public void done(ParseObject parseObject, ParseException e) {
if (e == null) {
String object = parseObject.getString("value");
Integer objectValue = Integer.parseInt(object);
if (dataArray == null) {
dataArray = new ArrayList<Integer>();
dataArray.add(objectValue);
ArrayList<Integer> y = dataArray;
XYSeries seriesY = new XYSeries("Y");
for (int i = 0; i < y.size(); i++) {
seriesY.add(i, y.get(i));
dataset = new XYMultipleSeriesDataset();
dataset.addSeries(seriesY);
}
}
Your getData() retrieves the data asynchronously. dataArray won't be initialized immediately when you call getIntent().
Wait for the async operation to complete before using the data there. For example, call the code requiring that data from the done() callback.
Ok so I am following mybringback's tutorial and where I am setting a background image with bitmap I get an error so I looked here and I used the code someone gave saw that I had no errors so I run the app and when I select an image it crashes
Here is my activity:
package com.odysseus.thebasics;
import java.io.IOException;
import java.io.InputStream;
import android.app.Activity;
import android.app.WallpaperManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;
public class TutorialThree extends Activity implements OnClickListener {
MediaPlayer buttonSound;
ImageView display;
int toPhone;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.wallpaper);
final MediaPlayer buttonSound = MediaPlayer.create(TutorialThree.this, R.raw.button_sound);
toPhone = R.drawable.image1;
display = (ImageView) findViewById(R.id.IVdisplay);
ImageView image1 = (ImageView) findViewById(R.id.IVimage1);
ImageView image2 = (ImageView) findViewById(R.id.IVimage2);
ImageView image3 = (ImageView) findViewById(R.id.IVimage3);
ImageView image4 = (ImageView) findViewById(R.id.IVimage4);
Button setWall = (Button) findViewById(R.id.setBackround);
image1.setOnClickListener(this);
image2.setOnClickListener(this);
image3.setOnClickListener(this);
image4.setOnClickListener(this);
setWall.setOnClickListener(this);
}
#Override
public void onClick(View v) {
switch (v.getId()){
case R.id.IVimage1:
display.setImageResource(R.drawable.image5);
toPhone = R.drawable.image1;
break;
case R.id.IVimage2:
display.setImageResource(R.drawable.image2);
toPhone = R.drawable.image2;
break;
case R.id.IVimage3:
display.setImageResource(R.drawable.image3);
toPhone = R.drawable.image3;
break;
case R.id.IVimage4:
display.setImageResource(R.drawable.image4);
toPhone = R.drawable.image4;
break;
case R.id.setBackround:
InputStream decode = getResources().openRawResource(toPhone);
Bitmap background = BitmapFactory.decodeStream(decode); //this before was InputStream ....
WallpaperManagermyWallpaperManager=WallpaperManager.getInstance(getApplicationContext()); //error used to be here before i had getApplicationContext().setWallpaper(background);
try{
myWallpaperManager.setBitmap(background);
}catch(IOException e){
e.printStackTrace();
Toast error = Toast.makeText(TutorialThree.this, "Oops, couldn't set image", Toast.LENGTH_LONG);
error.show();
}
break;
}
buttonSound.start();
}
}
This is my xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageView
android:id="#+id/IVdisplay"
android:layout_width="200dp"
android:layout_height="159dp"
android:layout_gravity="center"
android:src="#drawable/image5" />
<Button
android:id="#+id/setBackround"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Set Image To Background" />
<HorizontalScrollView
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_gravity="center" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="25dp"
android:orientation="horizontal" >
<ImageView
android:id="#+id/IVimage1"
android:layout_width="125dp"
android:layout_height="200dp"
android:padding="15dp"
android:src="#drawable/image5" />
<ImageView
android:id="#+id/IVimage2"
android:layout_width="125dp"
android:layout_height="200dp"
android:padding="15dp"
android:src="#drawable/image2" />
<ImageView
android:id="#+id/IVimage3"
android:layout_width="125dp"
android:layout_height="200dp"
android:padding="15dp"
android:src="#drawable/image3" />
<ImageView
android:id="#+id/IVimage4"
android:layout_width="125dp"
android:layout_height="200dp"
android:padding="15dp"
android:src="#drawable/image4" />
</LinearLayout>
</HorizontalScrollView>
</LinearLayout>
My logcat:
01-09 20:03:03.960: V/MediaPlayer(29999): getDuration
01-09 20:03:03.960: V/MediaPlayer(29999): getCurrentPosition
01-09 20:03:03.970: D/MediaPlayer(29999): start() mUri is null
01-09 20:03:03.980: I/MediaPlayer(29999): mContext.toString() = com.odysseus.thebasics.menu#422c3b50
01-09 20:03:03.980: I/MediaPlayer(29999): setLGSoundNormalizerOnOff(1)
01-09 20:03:03.980: V/MediaPlayer(29999): start
01-09 20:03:04.020: V/MediaPlayer(29999): message received msg=4, ext1=0, ext2=0
01-09 20:03:04.020: V/MediaPlayer(29999): Received seek complete
01-09 20:03:04.020: V/MediaPlayer(29999): All seeks complete - return to regularly scheduled program
01-09 20:03:04.020: V/MediaPlayer(29999): callback application
01-09 20:03:04.020: V/MediaPlayer(29999): back from callback
01-09 20:03:04.040: V/MediaPlayer(29999): getDuration
01-09 20:03:04.040: V/MediaPlayer(29999): getCurrentPosition
01-09 20:03:04.040: V/MediaPlayer(29999): isPlaying: 1
01-09 20:03:04.590: V/MediaPlayer(29999): message received msg=2, ext1=0, ext2=0
01-09 20:03:04.590: V/MediaPlayer(29999): playback complete
01-09 20:03:04.590: V/MediaPlayer(29999): callback application
01-09 20:03:04.590: V/MediaPlayer(29999): back from callback
01-09 20:03:04.590: E/MediaPlayer(29999): ##### MEDIA_PLAYBACK_COMPLETE
01-09 20:03:04.590: V/MediaPlayer(29999): getDuration
01-09 20:03:04.590: V/MediaPlayer(29999): getCurrentPosition
01-09 20:03:04.590: D/MediaPlayer(29999): start() mUri is null
01-09 20:03:06.650: D/dalvikvm(29999): GC_CONCURRENT freed 76K, 5% free 6944K/7303K, paused 20ms+4ms, total 86ms
01-09 20:03:06.650: D/dalvikvm(29999): WAIT_FOR_CONCURRENT_GC blocked 45ms
01-09 20:03:06.750: D/dalvikvm(29999): GC_FOR_ALLOC freed 679K, 14% free 6646K/7687K, paused 23ms, total 23ms
01-09 20:03:06.820: D/dalvikvm(29999): GC_CONCURRENT freed 196K, 10% free 6931K/7687K, paused 21ms+2ms, total 58ms
01-09 20:03:06.820: D/dalvikvm(29999): WAIT_FOR_CONCURRENT_GC blocked 40ms
01-09 20:03:06.820: V/MediaPlayer(29999): constructor
01-09 20:03:06.820: V/MediaPlayer(29999): setListener
01-09 20:03:06.820: V/MediaPlayer(29999): setDataSource(51, 3706, 13890)
01-09 20:03:06.840: V/MediaPlayer(29999): setVideoSurfaceTexture
01-09 20:03:06.840: V/MediaPlayer(29999): prepare
01-09 20:03:06.840: V/MediaPlayer(29999): message received msg=200, ext1=1, ext2=9120
01-09 20:03:06.840: W/MediaPlayer(29999): info/warning (1, 9120)
01-09 20:03:06.840: V/MediaPlayer(29999): callback application
01-09 20:03:06.840: V/MediaPlayer(29999): back from callback
01-09 20:03:06.890: V/MediaPlayer(29999): message received msg=5, ext1=0, ext2=0
01-09 20:03:06.890: V/MediaPlayer(29999): New video size 0 x 0
01-09 20:03:06.890: V/MediaPlayer(29999): callback application
01-09 20:03:06.890: V/MediaPlayer(29999): back from callback
01-09 20:03:06.890: V/MediaPlayer(29999): message received msg=1, ext1=0, ext2=0
01-09 20:03:06.890: V/MediaPlayer(29999): prepared
01-09 20:03:06.890: V/MediaPlayer(29999): signal application thread
01-09 20:03:06.890: V/MediaPlayer(29999): prepare complete - status=0
01-09 20:03:06.900: V/MediaPlayer(29999): callback application
01-09 20:03:06.900: V/MediaPlayer(29999): back from callback
01-09 20:03:06.910: I/MediaPlayer(29999): Info (1,9120)
01-09 20:03:10.300: W/dalvikvm(29999): threadid=1: thread exiting with uncaught exception (group=0x41c2e438)
01-09 20:03:10.330: E/AndroidRuntime(29999): FATAL EXCEPTION: main
01-09 20:03:10.330: E/AndroidRuntime(29999): java.lang.NullPointerException
01-09 20:03:10.330: E/AndroidRuntime(29999): at com.odysseus.thebasics.TutorialThree.onClick(TutorialThree.java:79)
01-09 20:03:10.330: E/AndroidRuntime(29999): at android.view.View.performClick(View.java:4101)
01-09 20:03:10.330: E/AndroidRuntime(29999): at android.view.View$PerformClick.run(View.java:17082)
01-09 20:03:10.330: E/AndroidRuntime(29999): at android.os.Handler.handleCallback(Handler.java:615)
01-09 20:03:10.330: E/AndroidRuntime(29999): at android.os.Handler.dispatchMessage(Handler.java:92)
01-09 20:03:10.330: E/AndroidRuntime(29999): at android.os.Looper.loop(Looper.java:137)
01-09 20:03:10.330: E/AndroidRuntime(29999): at android.app.ActivityThread.main(ActivityThread.java:4940)
01-09 20:03:10.330: E/AndroidRuntime(29999): at java.lang.reflect.Method.invokeNative(Native Method)
01-09 20:03:10.330: E/AndroidRuntime(29999): at java.lang.reflect.Method.invoke(Method.java:511)
01-09 20:03:10.330: E/AndroidRuntime(29999): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:798)
01-09 20:03:10.330: E/AndroidRuntime(29999): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:565)
01-09 20:03:10.330: E/AndroidRuntime(29999): at dalvik.system.NativeStart.main(Native Method)
You can run it yourself to see also I am new to android so please understand! Thanks in advance
MediaPlayer buttonSound; // already declared but not initialized
ImageView display;
int toPhone;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.wallpaper);
final MediaPlayer buttonSound = MediaPlayer.create(TutorialThree.this, R.raw.button_sound);
// again declared and initialized. local to onCreate.
Change to
buttonSound = MediaPlayer.create(TutorialThree.this, R.raw.button_sound);
This
buttonSound.start();
is probably line 79 TutorialThree.java which causes NUllPointerException.
I am developing an application in android and I have a requirement to read and write from an excel file stored in sdcard.I am using jxl library for this purpose. But I am getting some errors with file operation.
ext = Environment.getExternalStorageDirectory().getAbsolutePath()+"/hai/dynamic.xls";
setOutputFile(ext);
try {
write();
} catch (WriteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void setOutputFile(String inputFile) {
this.inputfile = inputFile;
}
public void write() throws IOException, WriteException {
File file = new File(inputfile);
WorkbookSettings wbSettings = new WorkbookSettings();
wbSettings.setLocale(new Locale("en", "EN"));
WritableWorkbook workbook = Workbook.createWorkbook(file, wbSettings);
workbook.createSheet("Report", 0);
WritableSheet excelSheet = workbook.getSheet(0);
createLabel(excelSheet);
createContent(excelSheet);
workbook.write();
workbook.close();
}
Then I am getting this following error in logcat
01-09 12:43:12.824: W/System.err(12321): java.io.FileNotFoundException:/mnt/sdcard/hai/dynamic.xls (Permission denied)
01-09 12:43:12.884: W/System.err(12321): at org.apache.harmony.luni.platform.OSFileSystem.open(Native Method)
01-09 12:43:12.884: W/System.err(12321): at dalvik.system.BlockGuard$WrappedFileSystem.open(BlockGuard.java:232)
01-09 12:43:12.904: W/System.err(12321): at java.io.FileOutputStream.<init>(FileOutputStream.java:94)
01-09 12:43:12.904: W/System.err(12321): at java.io.FileOutputStream.<init>(FileOutputStream.java:66)
01-09 12:43:12.904: W/System.err(12321): at jxl.Workbook.createWorkbook(Workbook.java:301)
01-09 12:43:12.904: W/System.err(12321): at com.fis.excel.WriteAcivity.write(WriteAcivity.java:59)
01-09 12:43:12.904: W/System.err(12321): at com.fis.excel.WriteAcivity.onCreate(WriteAcivity.java:40)
01-09 12:43:12.904: W/System.err(12321): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
01-09 12:43:12.904: W/System.err(12321): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
01-09 12:43:12.916: W/System.err(12321): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
01-09 12:43:12.916: W/System.err(12321): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
01-09 12:43:12.924: W/System.err(12321): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
01-09 12:43:12.924: W/System.err(12321): at android.os.Handler.dispatchMessage(Handler.java:99)
01-09 12:43:12.924: W/System.err(12321): at android.os.Looper.loop(Looper.java:123)
01-09 12:43:12.924: W/System.err(12321): at android.app.ActivityThread.main(ActivityThread.java:3683)
01-09 12:43:12.924: W/System.err(12321): at java.lang.reflect.Method.invokeNative(Native Method)
01-09 12:43:12.944: W/System.err(12321): at java.lang.reflect.Method.invoke(Method.java:507)
01-09 12:43:12.944: W/System.err(12321): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
01-09 12:43:12.944: W/System.err(12321): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
01-09 12:43:12.944: W/System.err(12321): at dalvik.system.NativeStart.main(Native Method)
//add use permission in your manifest
WRITE_EXTERNAL_STORAGE
Allows an application to write to external storage
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />