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.
Related
I want to get a String from a website, and convert it to a Array. It is working in eclipse, but if I want to use it in an Android-App it wont work. I know, it must be a stupid mistake but I dont know where it is.
InternetHandler:
package de.blender4me.einkaufsliste;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.regex.Pattern;
public class internetHandler {
//Einkaufsplaner-API aufrufen
public static String[] getList(){
InputStream is = null;
try
{
URL url = new URL("https://einkaufsplaner.blender4me.de/api.php?name=Username&pass=SuperSecretPass&addValue=&removeValue=");
BufferedReader in = new BufferedReader(
new InputStreamReader(url.openStream()));
String Values = null;
for(int i = 0; i < 4; ++i){
Values = in.readLine();
}
in.close();
String[] List = Values.split( Pattern.quote( ";" ) );
return List;
}
catch ( Exception e ) {
e.printStackTrace();
}
finally {
if ( is != null )
try { is.close(); } catch ( IOException e ) { }
}
String[] error = {"error"};
return error;
}
}
Main
package de.blender4me.einkaufsliste;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
public class MainActivity extends Activity {
//Widgets
Button addItem;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//listView
populateListView();
//register Widgets
addItem =(Button) findViewById(R.id.addItem);
addItem.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
openDialog();
}
});
}
#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);
}
//Alert
public void openDialog(){
AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setTitle("Zur Einkaufsliste hinzufügen");
alert.setMessage(" ");
// Set an EditText view to get user input
final EditText input = new EditText(this);
alert.setView(input);
alert.setPositiveButton("Bestätigen", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
String value = input.getText().toString();
// Do something with value!
}
});
alert.setNegativeButton("Abbrechen", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
// Canceled.
}
});
alert.show();
}
//listView erstellen
private void populateListView() {
// Liste auslesen
String[] values = internetHandler.getList();
//Liste konvertieren
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
R.layout.listview_item, //Genutztes Layout
values); //Genutzter Array
//listView einstellen
ListView list = (ListView) findViewById(R.id.listView);
list.setAdapter(adapter);
}
}
Manifest
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="19" />
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#android:style/Theme.Holo.Light.NoActionBar.Fullscreen"
>
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
It always returns the error-array, but idk why :C
logcat:
11-07 15:18:24.425: E/Trace(642): error opening trace file: No such
file or directory (2) 11-07 15:18:24.925: W/System.err(642):
android.os.NetworkOnMainThreadException 11-07 15:18:24.925:
W/System.err(642): at
android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117)
11-07 15:18:24.935: W/System.err(642): at
java.net.InetAddress.lookupHostByName(InetAddress.java:385) 11-07
15:18:24.935: W/System.err(642): at
java.net.InetAddress.getAllByNameImpl(InetAddress.java:236) 11-07
15:18:24.935: W/System.err(642): at
java.net.InetAddress.getAllByName(InetAddress.java:214) 11-07
15:18:24.935: W/System.err(642): at
libcore.net.http.HttpConnection.(HttpConnection.java:70) 11-07
15:18:24.935: W/System.err(642): at
libcore.net.http.HttpConnection.(HttpConnection.java:50) 11-07
15:18:24.935: W/System.err(642): at
libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:341)
11-07 15:18:24.935: W/System.err(642): at
libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:87)
11-07 15:18:24.935: W/System.err(642): at
libcore.net.http.HttpConnection.connect(HttpConnection.java:128) 11-07
15:18:24.935: W/System.err(642): at
libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:315)
11-07 15:18:24.945: W/System.err(642): at
libcore.net.http.HttpsURLConnectionImpl$HttpsEngine.makeSslConnection(HttpsURLConnectionImpl.java:461)
11-07 15:18:24.945: W/System.err(642): at
libcore.net.http.HttpsURLConnectionImpl$HttpsEngine.connect(HttpsURLConnectionImpl.java:433)
11-07 15:18:24.945: W/System.err(642): at
libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:289)
11-07 15:18:24.945: W/System.err(642): at
libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:239) 11-07
15:18:24.945: W/System.err(642): at
libcore.net.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:273)
11-07 15:18:24.945: W/System.err(642): at
libcore.net.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:168)
11-07 15:18:24.945: W/System.err(642): at
libcore.net.http.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:271)
11-07 15:18:24.955: W/System.err(642): at
java.net.URL.openStream(URL.java:462) 11-07 15:18:24.955:
W/System.err(642): at
de.blender4me.einkaufsliste.internetHandler.getList(internetHandler.java:21)
11-07 15:18:24.955: W/System.err(642): at
de.blender4me.einkaufsliste.MainActivity.populateListView(MainActivity.java:100)
11-07 15:18:24.955: W/System.err(642): at
de.blender4me.einkaufsliste.MainActivity.onCreate(MainActivity.java:37)
11-07 15:18:24.955: W/System.err(642): at
android.app.Activity.performCreate(Activity.java:5008) 11-07
15:18:24.955: W/System.err(642): at
android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
11-07 15:18:24.955: W/System.err(642): at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
11-07 15:18:24.965: W/System.err(642): at
android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
11-07 15:18:24.965: W/System.err(642): at
android.app.ActivityThread.access$600(ActivityThread.java:130) 11-07
15:18:24.965: W/System.err(642): at
android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
11-07 15:18:24.965: W/System.err(642): at
android.os.Handler.dispatchMessage(Handler.java:99) 11-07
15:18:24.965: W/System.err(642): at
android.os.Looper.loop(Looper.java:137) 11-07 15:18:24.965:
W/System.err(642): at
android.app.ActivityThread.main(ActivityThread.java:4745) 11-07
15:18:24.965: W/System.err(642): at
java.lang.reflect.Method.invokeNative(Native Method) 11-07
15:18:24.965: W/System.err(642): at
java.lang.reflect.Method.invoke(Method.java:511) 11-07 15:18:24.975:
W/System.err(642): at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
11-07 15:18:24.975: W/System.err(642): at
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 11-07
15:18:24.975: W/System.err(642): at
dalvik.system.NativeStart.main(Native Method) 11-07 15:18:26.455:
D/gralloc_goldfish(642): Emulator without GPU emulation detected.
Your problem is here (in the stacktrace) :
android.os.NetworkOnMainThreadException
You're making a request in the MainThread which is forbiden with Android to avoid heavy treatments on UI (making this will block the UI).
Use an AsyncTask instead.
An example of AsyncTask to start from something :
private class LongOperation extends AsyncTask<Void, Void, String> {
#Override
protected String doInBackground(Void... params) {
InternetHandler ih = new InternetHandler();
String[] values = ih.getList();
}
#Override
protected void onPostExecute(String result) {
//Liste konvertieren
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
R.layout.listview_item, //Genutztes Layout
values); //Genutzter Array
//listView einstellen
ListView list = (ListView) findViewById(R.id.listView);
list.setAdapter(adapter);
}
#Override
protected void onPreExecute() {}
#Override
protected void onProgressUpdate(Void... values) {}
}
Call it with :
LongOperation lo = new LongOperation();
lo.execute();
The problem is you are trying to perform HTTP request on application's UI thread.
You should do it inside AsyncTask.
My application crashes whenever I launch it! whats wrong with it? I have created a database and I am trying to populate my listview from the database I have created what is the problem with the code and what is the fix for it?
02-19 22:02:07.502: D/AndroidRuntime(330): Shutting down VM
02-19 22:02:07.502: W/dalvikvm(330): threadid=1: thread exiting with uncaught exception (group=0x40015560)
02-19 22:02:07.542: E/AndroidRuntime(330): FATAL EXCEPTION: main
02-19 22:02:07.542: E/AndroidRuntime(330): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.database/com.example.database.MainActivity}: java.lang.ClassNotFoundException: com.example.database.MainActivity in loader dalvik.system.PathClassLoader[/data/app/com.example.database-1.apk]
02-19 22:02:07.542: E/AndroidRuntime(330): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1569)
02-19 22:02:07.542: E/AndroidRuntime(330): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
02-19 22:02:07.542: E/AndroidRuntime(330): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
02-19 22:02:07.542: E/AndroidRuntime(330): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
02-19 22:02:07.542: E/AndroidRuntime(330): at android.os.Handler.dispatchMessage(Handler.java:99)
02-19 22:02:07.542: E/AndroidRuntime(330): at android.os.Looper.loop(Looper.java:123)
02-19 22:02:07.542: E/AndroidRuntime(330): at android.app.ActivityThread.main(ActivityThread.java:3683)
02-19 22:02:07.542: E/AndroidRuntime(330): at java.lang.reflect.Method.invokeNative(Native Method)
02-19 22:02:07.542: E/AndroidRuntime(330): at java.lang.reflect.Method.invoke(Method.java:507)
02-19 22:02:07.542: E/AndroidRuntime(330): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
02-19 22:02:07.542: E/AndroidRuntime(330): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
02-19 22:02:07.542: E/AndroidRuntime(330): at dalvik.system.NativeStart.main(Native Method)
02-19 22:02:07.542: E/AndroidRuntime(330): Caused by: java.lang.ClassNotFoundException: com.example.database.MainActivity in loader dalvik.system.PathClassLoader[/data/app/com.example.database-1.apk]
02-19 22:02:07.542: E/AndroidRuntime(330): at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:240)
02-19 22:02:07.542: E/AndroidRuntime(330): at java.lang.ClassLoader.loadClass(ClassLoader.java:551)
02-19 22:02:07.542: E/AndroidRuntime(330): at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
02-19 22:02:07.542: E/AndroidRuntime(330): at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
02-19 22:02:07.542: E/AndroidRuntime(330): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1561)
02-19 22:02:07.542: E/AndroidRuntime(330): ... 11 more
02-19 22:10:47.354: D/AndroidRuntime(364): Shutting down VM
02-19 22:10:47.444: W/dalvikvm(364): threadid=1: thread exiting with uncaught exception (group=0x40015560)
02-19 22:10:47.482: E/AndroidRuntime(364): FATAL EXCEPTION: main
02-19 22:10:47.482: E/AndroidRuntime(364): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.database/com.example.database.MainActivity}: java.lang.ClassNotFoundException: com.example.database.MainActivity in loader dalvik.system.PathClassLoader[/data/app/com.example.database-2.apk]
02-19 22:10:47.482: E/AndroidRuntime(364): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1569)
02-19 22:10:47.482: E/AndroidRuntime(364): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
02-19 22:10:47.482: E/AndroidRuntime(364): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
02-19 22:10:47.482: E/AndroidRuntime(364): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
02-19 22:10:47.482: E/AndroidRuntime(364): at android.os.Handler.dispatchMessage(Handler.java:99)
02-19 22:10:47.482: E/AndroidRuntime(364): at android.os.Looper.loop(Looper.java:123)
02-19 22:10:47.482: E/AndroidRuntime(364): at android.app.ActivityThread.main(ActivityThread.java:3683)
02-19 22:10:47.482: E/AndroidRuntime(364): at java.lang.reflect.Method.invokeNative(Native Method)
02-19 22:10:47.482: E/AndroidRuntime(364): at java.lang.reflect.Method.invoke(Method.java:507)
02-19 22:10:47.482: E/AndroidRuntime(364): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
02-19 22:10:47.482: E/AndroidRuntime(364): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
02-19 22:10:47.482: E/AndroidRuntime(364): at dalvik.system.NativeStart.main(Native Method)
02-19 22:10:47.482: E/AndroidRuntime(364): Caused by: java.lang.ClassNotFoundException: com.example.database.MainActivity in loader dalvik.system.PathClassLoader[/data/app/com.example.database-2.apk]
02-19 22:10:47.482: E/AndroidRuntime(364): at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:240)
02-19 22:10:47.482: E/AndroidRuntime(364): at java.lang.ClassLoader.loadClass(ClassLoader.java:551)
02-19 22:10:47.482: E/AndroidRuntime(364): at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
02-19 22:10:47.482: E/AndroidRuntime(364): at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
02-19 22:10:47.482: E/AndroidRuntime(364): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1561)
02-19 22:10:47.482: E/AndroidRuntime(364): ... 11 more
02-19 22:11:10.302: I/Process(364): Sending signal. PID: 364 SIG: 9
02-19 22:11:17.712: D/AndroidRuntime(413): Shutting down VM
02-19 22:11:17.712: W/dalvikvm(413): threadid=1: thread exiting with uncaught exception (group=0x40015560)
02-19 22:11:17.852: E/AndroidRuntime(413): FATAL EXCEPTION: main
02-19 22:11:17.852: E/AndroidRuntime(413): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.database/com.example.database.MainActivity}: java.lang.ClassNotFoundException: com.example.database.MainActivity in loader dalvik.system.PathClassLoader[/data/app/com.example.database-2.apk]
02-19 22:11:17.852: E/AndroidRuntime(413): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1569)
02-19 22:11:17.852: E/AndroidRuntime(413): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
02-19 22:11:17.852: E/AndroidRuntime(413): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
02-19 22:11:17.852: E/AndroidRuntime(413): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
02-19 22:11:17.852: E/AndroidRuntime(413): at android.os.Handler.dispatchMessage(Handler.java:99)
02-19 22:11:17.852: E/AndroidRuntime(413): at android.os.Looper.loop(Looper.java:123)
02-19 22:11:17.852: E/AndroidRuntime(413): at android.app.ActivityThread.main(ActivityThread.java:3683)
02-19 22:11:17.852: E/AndroidRuntime(413): at java.lang.reflect.Method.invokeNative(Native Method)
02-19 22:11:17.852: E/AndroidRuntime(413): at java.lang.reflect.Method.invoke(Method.java:507)
02-19 22:11:17.852: E/AndroidRuntime(413): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
02-19 22:11:17.852: E/AndroidRuntime(413): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
02-19 22:11:17.852: E/AndroidRuntime(413): at dalvik.system.NativeStart.main(Native Method)
02-19 22:11:17.852: E/AndroidRuntime(413): Caused by: java.lang.ClassNotFoundException: com.example.database.MainActivity in loader dalvik.system.PathClassLoader[/data/app/com.example.database-2.apk]
02-19 22:11:17.852: E/AndroidRuntime(413): at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:240)
02-19 22:11:17.852: E/AndroidRuntime(413): at java.lang.ClassLoader.loadClass(ClassLoader.java:551)
02-19 22:11:17.852: E/AndroidRuntime(413): at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
02-19 22:11:17.852: E/AndroidRuntime(413): at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
02-19 22:11:17.852: E/AndroidRuntime(413): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1561)
02-19 22:11:17.852: E/AndroidRuntime(413): ... 11 more
02-19 22:11:19.852: I/Process(413): Sending signal. PID: 413 SIG: 9
Activity:
package com.example.database;
import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
public class AndroidSQLite extends Activity {
private DBHelper mySQLiteAdapter;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView listContent = (ListView)findViewById(R.id.contentlist);
/*
* Create/Open a SQLite database
* and fill with dummy content
* and close it
*/
mySQLiteAdapter = new DBHelper(this);
mySQLiteAdapter.openToWrite();
mySQLiteAdapter.deleteAll();
mySQLiteAdapter.insert("A for Apply");
mySQLiteAdapter.insert("B for Boy");
mySQLiteAdapter.insert("C for Cat");
mySQLiteAdapter.insert("D for Dog");
mySQLiteAdapter.insert("E for Egg");
mySQLiteAdapter.insert("F for Fish");
mySQLiteAdapter.insert("G for Girl");
mySQLiteAdapter.insert("H for Hand");
mySQLiteAdapter.insert("I for Ice-scream");
mySQLiteAdapter.insert("J for Jet");
mySQLiteAdapter.insert("K for Kite");
mySQLiteAdapter.insert("L for Lamp");
mySQLiteAdapter.insert("M for Man");
mySQLiteAdapter.insert("N for Nose");
mySQLiteAdapter.insert("O for Orange");
mySQLiteAdapter.insert("P for Pen");
mySQLiteAdapter.insert("Q for Queen");
mySQLiteAdapter.insert("R for Rain");
mySQLiteAdapter.insert("S for Sugar");
mySQLiteAdapter.insert("T for Tree");
mySQLiteAdapter.insert("U for Umbrella");
mySQLiteAdapter.insert("V for Van");
mySQLiteAdapter.insert("W for Water");
mySQLiteAdapter.insert("X for X'mas");
mySQLiteAdapter.insert("Y for Yellow");
mySQLiteAdapter.insert("Z for Zoo");
mySQLiteAdapter.close();
/*
* Open the same SQLite database
* and read all it's content.
*/
mySQLiteAdapter = new DBHelper(this);
mySQLiteAdapter.openToRead();
Cursor cursor = mySQLiteAdapter.queueAll();
startManagingCursor(cursor);
String[] from = new String[]{DBHelper.KEY_CONTENT};
int[] to = new int[]{R.id.text};
SimpleCursorAdapter cursorAdapter =
new SimpleCursorAdapter(this, R.layout.row, cursor, from, to);
listContent.setAdapter(cursorAdapter);
mySQLiteAdapter.close();
}
}
Database Class
package com.example.database;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
public class DBHelper {
public static final String MYDATABASE_NAME = "MY_DATABASE";
public static final String MYDATABASE_TABLE = "MY_TABLE";
public static final int MYDATABASE_VERSION = 1;
public static final String KEY_ID = "_id";
public static final String KEY_CONTENT = "Content";
//create table MY_DATABASE (ID integer primary key, Content text not null);
private static final String SCRIPT_CREATE_DATABASE =
"create table " + MYDATABASE_TABLE + " ("
+ KEY_ID + " integer primary key autoincrement, "
+ KEY_CONTENT + " text not null);";
private SQLiteHelper sqLiteHelper;
private SQLiteDatabase sqLiteDatabase;
private Context context;
public DBHelper(Context c){
context = c;
}
public DBHelper openToRead() throws android.database.SQLException {
sqLiteHelper = new SQLiteHelper(context, MYDATABASE_NAME, null, MYDATABASE_VERSION);
sqLiteDatabase = sqLiteHelper.getReadableDatabase();
return this;
}
public DBHelper openToWrite() throws android.database.SQLException {
sqLiteHelper = new SQLiteHelper(context, MYDATABASE_NAME, null, MYDATABASE_VERSION);
sqLiteDatabase = sqLiteHelper.getWritableDatabase();
return this;
}
public void close(){
sqLiteHelper.close();
}
public long insert(String content){
ContentValues contentValues = new ContentValues();
contentValues.put(KEY_CONTENT, content);
return sqLiteDatabase.insert(MYDATABASE_TABLE, null, contentValues);
}
public int deleteAll(){
return sqLiteDatabase.delete(MYDATABASE_TABLE, null, null);
}
public Cursor queueAll(){
String[] columns = new String[]{KEY_ID, KEY_CONTENT};
Cursor cursor = sqLiteDatabase.query(MYDATABASE_TABLE, columns,
null, null, null, null, null);
return cursor;
}
public class SQLiteHelper extends SQLiteOpenHelper {
public SQLiteHelper(Context context, String name,
CursorFactory factory, int version) {
super(context, name, factory, version);
}
#Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL(SCRIPT_CREATE_DATABASE);
}
#Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
}
}
}
you are Tryin to load MainActivity
java.lang.ClassNotFoundException: com.example.database.MainActivity
inside your Manifest.xml change MainActivity to AndroidSQLite.
And you must use an Asynctask for the Database operations, to avoid ANR Dialog.
Sorry,this is my first time to put questions in this site
My Function: I set two Buttons to restore and save the information in the Spinner and EditText with the function Sharedpreferences.
I execute the program at the first time. The program will appear the error state,if I click the Button "restore" to restore the information in Spinner. But I haven't met the problem when I restore the information in EditText.
This is the code in Spinner
private Spinner.OnItemSelectedListener getfeet = new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View v, int position,
long id) {
// TODO Auto-generated method stub
feet_out = parent.getSelectedItemPosition() + 2;
select_f = feet.getSelectedItemPosition(); //save the position you choose
Toast.makeText(MainActivity.this,
"you chose " + parent.getSelectedItem().toString(),
Toast.LENGTH_SHORT).show();
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub
}
};
private Spinner.OnItemSelectedListener getinch = new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View v, int position,
long id) {
// TODO Auto-generated method stub
inch_out = parent.getSelectedItemPosition();
select_i = inch.getSelectedItemPosition(); //save the position you choose
Toast.makeText(MainActivity.this,
"you chose " + parent.getSelectedItem().toString(),
Toast.LENGTH_SHORT).show();
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub
}
};
This is the code which executes the function of save in Sharedpreferences
private void save_() {
settings = getSharedPreferences("DATA", 0);
settings.edit().putInt("DATA_FEET", select_f) //store the position in DATA_FEET and DATA_INCH
.putInt("DATA_INCH", select_i)
.putString("DATA_WEIGHT", weight.getText().toString()).commit();
Toast.makeText(MainActivity.this, R.string.done, Toast.LENGTH_SHORT) //save done
.show();
}
This is the code which executes the function of restore in Sharedpreferences
private void restore_() {
feet.setSelection(settings.getInt("DATA_FEET", select_f)); //restore the position
inch.setSelection(settings.getInt("DATA_INCH", select_i));
weight.setText(settings.getString("DATA_WEIGHT", "EMPTY"));
}
My problem is that I can't use the function of restore at the program executing at the first time. Is there any solution to solve the problem?? Because it is normal in EditText,but it is abnormal in Spinner. Thanks :))
This is the state. :))
10-23 23:14:11.677: D/TextLayoutCache(26370): Using debug level: 0 - Debug Enabled: 0
10-23 23:14:11.747: D/libEGL(26370): loaded /system/lib/egl/libGLES_android.so
10-23 23:14:11.797: D/libEGL(26370): loaded /system/lib/egl/libEGL_mali.so
10-23 23:14:11.827: D/libEGL(26370): loaded /system/lib/egl/libGLESv1_CM_mali.so
10-23 23:14:11.827: D/libEGL(26370): loaded /system/lib/egl/libGLESv2_mali.so
10-23 23:14:11.887: D/OpenGLRenderer(26370): Enabling debug mode 0
10-23 23:14:16.762: D/AndroidRuntime(26370): Shutting down VM
10-23 23:14:16.762: W/dalvikvm(26370): threadid=1: thread exiting with uncaught exception (group=0x40aaa210)
10-23 23:14:16.802: E/AndroidRuntime(26370): FATAL EXCEPTION: main
10-23 23:14:16.802: E/AndroidRuntime(26370): java.lang.NullPointerException
10-23 23:14:16.802: E/AndroidRuntime(26370): at com.example.bmi.MainActivity.restore_(MainActivity.java:44)
10-23 23:14:16.802: E/AndroidRuntime(26370): at com.example.bmi.MainActivity.access$1(MainActivity.java:43)
10-23 23:14:16.802: E/AndroidRuntime(26370): at com.example.bmi.MainActivity$2.onClick(MainActivity.java:99)
10-23 23:14:16.802: E/AndroidRuntime(26370): at android.view.View.performClick(View.java:3574)
10-23 23:14:16.802: E/AndroidRuntime(26370): at android.view.View$PerformClick.run(View.java:14293)
10-23 23:14:16.802: E/AndroidRuntime(26370): at android.os.Handler.handleCallback(Handler.java:605)
10-23 23:14:16.802: E/AndroidRuntime(26370): at android.os.Handler.dispatchMessage(Handler.java:92)
10-23 23:14:16.802: E/AndroidRuntime(26370): at android.os.Looper.loop(Looper.java:137)
10-23 23:14:16.802: E/AndroidRuntime(26370): at android.app.ActivityThread.main(ActivityThread.java:4448)
10-23 23:14:16.802: E/AndroidRuntime(26370): at java.lang.reflect.Method.invokeNative(Native Method)
10-23 23:14:16.802: E/AndroidRuntime(26370): at java.lang.reflect.Method.invoke(Method.java:511)
10-23 23:14:16.802: E/AndroidRuntime(26370): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:823)
10-23 23:14:16.802: E/AndroidRuntime(26370): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:590)
10-23 23:14:16.802: E/AndroidRuntime(26370): at dalvik.system.NativeStart.main(Native Method)
This is the method to call the restore_()
private OnClickListener reback_1 = new OnClickListener() {
public void onClick(View v) {
restore_();
}
};
After I replaced the select_f and select_i into 0, it appeared the problem
10-24 00:01:30.957: D/TextLayoutCache(28836): Using debug level: 0 - Debug Enabled: 0
10-24 00:01:31.017: D/libEGL(28836): loaded /system/lib/egl/libGLES_android.so
10-24 00:01:31.037: D/libEGL(28836): loaded /system/lib/egl/libEGL_mali.so
10-24 00:01:31.057: D/libEGL(28836): loaded /system/lib/egl/libGLESv1_CM_mali.so
10-24 00:01:31.057: D/libEGL(28836): loaded /system/lib/egl/libGLESv2_mali.so
10-24 00:01:31.087: D/OpenGLRenderer(28836): Enabling debug mode 0
10-24 00:01:36.262: D/AndroidRuntime(28836): Shutting down VM
10-24 00:01:36.262: W/dalvikvm(28836): threadid=1: thread exiting with uncaught exception (group=0x40aaa210)
10-24 00:01:36.282: E/AndroidRuntime(28836): FATAL EXCEPTION: main
10-24 00:01:36.282: E/AndroidRuntime(28836): java.lang.NullPointerException
10-24 00:01:36.282: E/AndroidRuntime(28836): at com.example.bmi.MainActivity.restore_(MainActivity.java:44)
10-24 00:01:36.282: E/AndroidRuntime(28836): at com.example.bmi.MainActivity.access$1(MainActivity.java:43)
10-24 00:01:36.282: E/AndroidRuntime(28836): at com.example.bmi.MainActivity$2.onClick(MainActivity.java:99)
10-24 00:01:36.282: E/AndroidRuntime(28836): at android.view.View.performClick(View.java:3574)
10-24 00:01:36.282: E/AndroidRuntime(28836): at android.view.View$PerformClick.run(View.java:14293)
10-24 00:01:36.282: E/AndroidRuntime(28836): at android.os.Handler.handleCallback(Handler.java:605)
10-24 00:01:36.282: E/AndroidRuntime(28836): at android.os.Handler.dispatchMessage(Handler.java:92)
10-24 00:01:36.282: E/AndroidRuntime(28836): at android.os.Looper.loop(Looper.java:137)
10-24 00:01:36.282: E/AndroidRuntime(28836): at android.app.ActivityThread.main(ActivityThread.java:4448)
10-24 00:01:36.282: E/AndroidRuntime(28836): at java.lang.reflect.Method.invokeNative(Native Method)
10-24 00:01:36.282: E/AndroidRuntime(28836): at java.lang.reflect.Method.invoke(Method.java:511)
10-24 00:01:36.282: E/AndroidRuntime(28836): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:823)
10-24 00:01:36.282: E/AndroidRuntime(28836): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:590)
10-24 00:01:36.282: E/AndroidRuntime(28836): at dalvik.system.NativeStart.main(Native Method)
10-24 00:01:37.803: I/Process(28836): Sending signal. PID: 28836 SIG: 9
You are using the SharedPrefs wrong.
feet.setSelection(settings.getInt("DATA_FEET", select_f));
when you try to fetch the integer-value from sharedPref, you get returned null! because i think that you misunderstand how it is done:
settings.getInt("myKey", defaultValue);
returns you the value for the key "myKey". if "myKey" has no value set, then it returns you the "defaultValue". In your case it returns the current value of "select_f". And as it looks, the value of select_f is null, when you run your application for the first time.
So you have to decide whether to initialize "select_f" before you retrieve the sharedPrefs or you enter another defaultValue here.
When I try to run this line in order to hide the keyboard (I get the InputMethodManager):
this.context = context;
InputMethodManager mgr = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE); //this line crashes the app
What should I do to fix it?
(I am running it from a fragment by the way)
Crash Log:
11-03 16:20:26.700: D/AndroidRuntime(2809): Shutting down VM
11-03 16:20:26.700: W/dalvikvm(2809): threadid=1: thread exiting with uncaught exception (group=0x40a13300)
11-03 16:20:26.710: E/AndroidRuntime(2809): FATAL EXCEPTION: main
11-03 16:20:26.710: E/AndroidRuntime(2809): java.lang.NullPointerException
11-03 16:20:26.710: E/AndroidRuntime(2809): at co.emuze.tabtest1.Tab1$1.onClick(Tab1.java:32)
11-03 16:20:26.710: E/AndroidRuntime(2809): at android.view.View.performClick(View.java:4084)
11-03 16:20:26.710: E/AndroidRuntime(2809): at android.view.View$PerformClick.run(View.java:16966)
11-03 16:20:26.710: E/AndroidRuntime(2809): at android.os.Handler.handleCallback(Handler.java:615)
11-03 16:20:26.710: E/AndroidRuntime(2809): at android.os.Handler.dispatchMessage(Handler.java:92)
11-03 16:20:26.710: E/AndroidRuntime(2809): at android.os.Looper.loop(Looper.java:137)
11-03 16:20:26.710: E/AndroidRuntime(2809): at android.app.ActivityThread.main(ActivityThread.java:4745)
11-03 16:20:26.710: E/AndroidRuntime(2809): at java.lang.reflect.Method.invokeNative(Native Method)
11-03 16:20:26.710: E/AndroidRuntime(2809): at java.lang.reflect.Method.invoke(Method.java:511)
11-03 16:20:26.710: E/AndroidRuntime(2809): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
11-03 16:20:26.710: E/AndroidRuntime(2809): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
11-03 16:20:26.710: E/AndroidRuntime(2809): at dalvik.system.NativeStart.main(Native Method)
Full on click method:
public void onClick(View v) {
text1.setText(editText1.getText().toString());
InputMethodManager mgr = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
mgr.hideSoftInputFromWindow(editText1.getWindowToken(), 0);
}
It would appear context is null - you show the line this.context = context, and if the right hand side context is not a variable local to that method you're doing nothing. I think what you may be looking for is context = getApplicationContext()
Dont forget to use try catch blog because in case when your keyboard not open and if you use key key board hide code app crashed
User below code in Fragment
try {
InputMethodManager imm = (InputMethodManager)getSystemService(INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
} catch (Exception e) {
// TODO: handle exception
}
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" />