I'm very new to Android programming and I'm trying to write an Android app that would parse an OWL file (.owl file) and display the classes and subclasses. When ever the user clicks the button, the file is read from the assets folder of the Eclipse Project, the OWL file is parsed and the names of the classes are printed on to the console.
But whenever I click the button, the emulator crashes throwing the following exceptions:
02-23 16:49:36.430: E/AndroidRuntime(597): FATAL EXCEPTION: main
02-23 16:49:36.430: E/AndroidRuntime(597): java.lang.ExceptionInInitializerError
02-23 16:49:36.430: E/AndroidRuntime(597): at com.Sample.SampleApp.StartingPoint.parseOWLFile(StartingPoint.java:105)
02-23 16:49:36.430: E/AndroidRuntime(597): at com.Sample.SampleApp.StartingPoint$3.onClick(StartingPoint.java:68)
Here is my the Java code I wrote:
public class StartingPoint extends ActionBarActivity {
private static final int REQUEST_PATH = 1;
String currentFile;
EditText et1;
Button browse, ok, exit;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
et1 = (EditText)findViewById(R.id.editText);
browse = (Button) findViewById(R.id.browseButton);
browse.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v)
{
getfile(v);
}
});
exit = (Button)findViewById(R.id.ExitButton);
exit.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v)
{
Intent i = new Intent(Intent.ACTION_MAIN);
i.addCategory(Intent.CATEGORY_HOME);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(i);
}
});
ok = (Button)findViewById(R.id.OKButton);
ok.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v)
{
parseOWLFile();
}
});
}
public void getfile(View view)
{
Intent i1 = new Intent(this, FileChooser.class);
startActivityForResult(i1, REQUEST_PATH);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
if(requestCode == REQUEST_PATH)
{
if(resultCode == RESULT_OK)
{
currentFile = data.getStringExtra("GetFileName");
et1.setText(currentFile);
}
}
}
public void parseOWLFile()
{
//String file = et1.getText().toString(); //Uncomment this when using on phone/tablet
String file = "antibiotics.owl";
try
{
//System.out.println("File name is "+file);
//File f = new File(file);
InputStream fis = getAssets().open(file);
OntModel base = ModelFactory.createOntologyModel( OntModelSpec.OWL_DL_MEM );
base.read(fis, null);
String ns = base.getNsPrefixURI("Ontology");
Resource r = base.getResource(ns+" Concept");
OntClass theClass = r.as(OntClass.class);
Individual indiv = base.createIndividual(ns+"IndivTest", theClass);
for (Iterator<Resource> i = indiv.listRDFTypes(true); i.hasNext(); )
System.out.println( indiv.getURI() + " is asserted in class " + i.next() );
OntModel inf = ModelFactory.createOntologyModel( OntModelSpec.OWL_MEM_MICRO_RULE_INF, base );
Individual ind = inf.getIndividual( ns + "indivTest" );
for (Iterator<Resource> i = ind.listRDFTypes(true); i.hasNext(); )
System.out.println( ind.getURI() + " is inferred to be in class " + i.next() );
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
I have imported the AndroJena 0.5 version jars into my Eclipse Project.
Here are some more exceptions that I got:
02-23 16:49:36.430: E/AndroidRuntime(597): Caused by: java.lang.ExceptionInInitializerError
02-23 16:49:36.430: E/AndroidRuntime(597): at com.hp.hpl.jena.ontology.OntModelSpec.<clinit>(OntModelSpec.java:54)
02-23 16:49:36.430: E/AndroidRuntime(597): Caused by: java.lang.ExceptionInInitializerError
02-23 16:49:36.430: E/AndroidRuntime(597): at com.hp.hpl.jena.ontology.ProfileRegistry.<clinit>(ProfileRegistry.java:48)
02-23 16:49:36.430: E/AndroidRuntime(597): Caused by: java.lang.ExceptionInInitializerError
02-23 16:49:36.430: E/AndroidRuntime(597): at com.hp.hpl.jena.rdf.model.ModelFactory.createDefaultModel(ModelFactory.java:122)
02-23 16:49:36.430: E/AndroidRuntime(597): at com.hp.hpl.jena.rdf.model.ModelFactory.createDefaultModel(ModelFactory.java:116)
Update
Here's the updated stack trace:
02-24 21:29:24.273: E/AndroidRuntime(534): FATAL EXCEPTION: main
02-24 21:29:24.273: E/AndroidRuntime(534): java.lang.ExceptionInInitializerError
02-24 21:29:24.273: E/AndroidRuntime(534): at com.hp.hpl.jena.rdf.arp.impl.XMLHandler.<init>(XMLHandler.java:367)
02-24 21:29:24.273: E/AndroidRuntime(534): at com.hp.hpl.jena.rdf.arp.impl.RDFXMLParser.<init>(RDFXMLParser.java:52)
02-24 21:29:24.273: E/AndroidRuntime(534): at com.hp.hpl.jena.rdf.arp.impl.RDFXMLParser.create(RDFXMLParser.java:85)
02-24 21:29:24.273: E/AndroidRuntime(534): at com.hp.hpl.jena.rdf.arp.JenaReader.<init>(JenaReader.java:62)
02-24 21:29:24.273: E/AndroidRuntime(534): at java.lang.Class.newInstanceImpl(Native Method)
02-24 21:29:24.273: E/AndroidRuntime(534): at java.lang.Class.newInstance(Class.java:1319)
02-24 21:29:24.273: E/AndroidRuntime(534): at com.hp.hpl.jena.rdf.model.impl.RDFReaderFImpl.getReader(RDFReaderFImpl.java:114)
02-24 21:29:24.273: E/AndroidRuntime(534): at com.hp.hpl.jena.rdf.model.impl.ModelCom.read(ModelCom.java:220)
02-24 21:29:24.273: E/AndroidRuntime(534): at com.hp.hpl.jena.ontology.OntDocumentManager.findMetadata(OntDocumentManager.java:895)
02-24 21:29:24.273: E/AndroidRuntime(534): at com.hp.hpl.jena.ontology.OntDocumentManager.initialiseMetadata(OntDocumentManager.java:853)
02-24 21:29:24.273: E/AndroidRuntime(534): at com.hp.hpl.jena.ontology.OntDocumentManager.<init>(OntDocumentManager.java:201)
02-24 21:29:24.273: E/AndroidRuntime(534): at com.hp.hpl.jena.ontology.OntDocumentManager.<init>(OntDocumentManager.java:183)
02-24 21:29:24.273: E/AndroidRuntime(534): at com.hp.hpl.jena.ontology.OntDocumentManager.<init>(OntDocumentManager.java:167)
02-24 21:29:24.273: E/AndroidRuntime(534): at com.hp.hpl.jena.ontology.OntDocumentManager.getInstance(OntDocumentManager.java:245)
02-24 21:29:24.273: E/AndroidRuntime(534): at com.hp.hpl.jena.ontology.OntModelSpec.getDocumentManager(OntModelSpec.java:340)
02-24 21:29:24.273: E/AndroidRuntime(534): at com.hp.hpl.jena.ontology.impl.OntModelImpl.getDocumentManager(OntModelImpl.java:181)
02-24 21:29:24.273: E/AndroidRuntime(534): at com.hp.hpl.jena.ontology.impl.OntModelImpl.loadImports(OntModelImpl.java:1887)
02-24 21:29:24.273: E/AndroidRuntime(534): at com.hp.hpl.jena.ontology.impl.OntModelImpl.<init>(OntModelImpl.java:148)
02-24 21:29:24.273: E/AndroidRuntime(534): at com.hp.hpl.jena.ontology.impl.OntModelImpl.<init>(OntModelImpl.java:128)
02-24 21:29:24.273: E/AndroidRuntime(534): at com.hp.hpl.jena.rdf.model.ModelFactory.createOntologyModel(ModelFactory.java:410)
02-24 21:29:24.273: E/AndroidRuntime(534): at com.Sample.SampleApp.StartingPoint.parseOWLFile(StartingPoint.java:105)
02-24 21:29:24.273: E/AndroidRuntime(534): at com.Sample.SampleApp.StartingPoint$3.onClick(StartingPoint.java:68)
02-24 21:29:24.273: E/AndroidRuntime(534): at android.view.View.performClick(View.java:3511)
02-24 21:29:24.273: E/AndroidRuntime(534): at android.view.View$PerformClick.run(View.java:14105)
02-24 21:29:24.273: E/AndroidRuntime(534): at android.os.Handler.handleCallback(Handler.java:605)
02-24 21:29:24.273: E/AndroidRuntime(534): at android.os.Handler.dispatchMessage(Handler.java:92)
02-24 21:29:24.273: E/AndroidRuntime(534): at android.os.Looper.loop(Looper.java:137)
02-24 21:29:24.273: E/AndroidRuntime(534): at android.app.ActivityThread.main(ActivityThread.java:4424)
02-24 21:29:24.273: E/AndroidRuntime(534): at java.lang.reflect.Method.invokeNative(Native Method)
02-24 21:29:24.273: E/AndroidRuntime(534): at java.lang.reflect.Method.invoke(Method.java:511)
02-24 21:29:24.273: E/AndroidRuntime(534): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
02-24 21:29:24.273: E/AndroidRuntime(534): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
02-24 21:29:24.273: E/AndroidRuntime(534): at dalvik.system.NativeStart.main(Native Method)
02-24 21:29:24.273: E/AndroidRuntime(534): Caused by: java.lang.NoClassDefFoundError: com.hp.hpl.jena.iri.IRIFactory
02-24 21:29:24.273: E/AndroidRuntime(534): at com.hp.hpl.jena.rdf.arp.ARPOptions.<clinit>(ARPOptions.java:60)
02-24 21:29:24.273: E/AndroidRuntime(534): ... 33 more
How can I eliminate this exception and get to parse the OWL file?
The Javadoc for this exception says:
Signals that an unexpected exception has occurred in a static initializer. An ExceptionInInitializerError is thrown to indicate that an exception occurred during evaluation of a static initializer or the initializer for a static variable.
You then say:
But, I'm not using any static variable in my code.
However, your code >>is<< triggering static initialization of some class, and that static initialization is throwing an unchecked exception which is not being caught.
Normally, the stacktrace of the ExceptionInInitializerError would include the traces of the unchecked exception that caused the problem. I don't see this in the fragments of stacktrace that you have included ... but I expect that it is in the complete stacktrace.
If you want further help, provide the complete unedited stacktrace rather than random fragments of one or more stacktraces.
UPDATE
The salient part of the stacktrace is at the end:
Caused by: java.lang.NoClassDefFoundError: org.slf4j.LoggerFactory
at com.hp.hpl.jena.util.Metadata.<clinit>(Metadata.java:26)
... 21 more
The JVM has been unable to load (or possibly initialize) the org.slf4j.LoggerFactory class. The problem is most likely that the SLF4J jar file is missing.
The dependencies of that version of Jena should be explained in the documentation.
Related
I have this code witch returns the content of a file:
private ArrayList<String> readFromFile1(Context context) {
ArrayList<String> list = new ArrayList<String>();
try {
ObjectInputStream ois = new ObjectInputStream( context.openFileInput("jokesNames2.bjk"));
try {
list = (ArrayList)ois.readObject();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
ois.close();
} catch (IOException e) {
Log.e("log activity", "Can not read file: " + e.toString());
}
I'm calling it like that:
System.out.println("FILE CONTENT: " + readFromFile1(this));
and everything runs just purfect.
Now In another activity I'm using almost the same code, but it wont run.
Here it is:
#SuppressLint("NewApi")
public class FavoriteJokes extends Activity {
public static ArrayAdapter<String> adapter;
public static ListView listView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_favorite_jokes);
final GlobalsHolder globals = (GlobalsHolder)getApplication();
TextView text = (TextView) findViewById(R.id.txtJoke);
text.setText(globals.getList().get(globals.clickedPosition));
ArrayList<String> jokeNamesList = new ArrayList<String>();
jokeNamesList.addAll(readFromFile1(this));
System.out.println("FILE CONTENT: " + readFromFile1(this));
globals.setFavJokeNamesList(readFromFile1(this));
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.favorite_jokes, menu);
return true;
}
/* Read file's content */
private ArrayList<String> readFromFile1(Context context) {
ArrayList<String> list = new ArrayList<String>();
try {
ObjectInputStream ois = new ObjectInputStream( context.openFileInput("jokesNames2.bjk"));
try {
list = (ArrayList)ois.readObject();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
ois.close();
} catch (IOException e) {
Log.e("log activity", "Can not read file: " + e.toString());
}
return list;
}
}
And here is the error output:
03-03 10:50:21.055: E/AndroidRuntime(1038): FATAL EXCEPTION: main
03-03 10:50:21.055: E/AndroidRuntime(1038): Process: com.gs.britishjokes, PID: 1038
03-03 10:50:21.055: E/AndroidRuntime(1038): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.gs.britishjokes/com.gs.britishjokes.FavoriteJokes}: java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
03-03 10:50:21.055: E/AndroidRuntime(1038): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2176)
03-03 10:50:21.055: E/AndroidRuntime(1038): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2226)
03-03 10:50:21.055: E/AndroidRuntime(1038): at android.app.ActivityThread.access$700(ActivityThread.java:135)
03-03 10:50:21.055: E/AndroidRuntime(1038): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1397)
03-03 10:50:21.055: E/AndroidRuntime(1038): at android.os.Handler.dispatchMessage(Handler.java:102)
03-03 10:50:21.055: E/AndroidRuntime(1038): at android.os.Looper.loop(Looper.java:137)
03-03 10:50:21.055: E/AndroidRuntime(1038): at android.app.ActivityThread.main(ActivityThread.java:4998)
03-03 10:50:21.055: E/AndroidRuntime(1038): at java.lang.reflect.Method.invokeNative(Native Method)
03-03 10:50:21.055: E/AndroidRuntime(1038): at java.lang.reflect.Method.invoke(Method.java:515)
03-03 10:50:21.055: E/AndroidRuntime(1038): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
03-03 10:50:21.055: E/AndroidRuntime(1038): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
03-03 10:50:21.055: E/AndroidRuntime(1038): at dalvik.system.NativeStart.main(Native Method)
03-03 10:50:21.055: E/AndroidRuntime(1038): Caused by: java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
03-03 10:50:21.055: E/AndroidRuntime(1038): at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
03-03 10:50:21.055: E/AndroidRuntime(1038): at java.util.ArrayList.get(ArrayList.java:308)
03-03 10:50:21.055: E/AndroidRuntime(1038): at com.gs.britishjokes.FavoriteJokes.onCreate(FavoriteJokes.java:31)
03-03 10:50:21.055: E/AndroidRuntime(1038): at android.app.Activity.performCreate(Activity.java:5243)
03-03 10:50:21.055: E/AndroidRuntime(1038): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
03-03 10:50:21.055: E/AndroidRuntime(1038): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2140)
03-03 10:50:21.055: E/AndroidRuntime(1038): ... 11 more
I really can't understand what is wrong with my code. I'm pretty sure that I'm missing a really small part, but I'm unable to find it. Can you give me a push?
The error log (logcat) indicates an IndexOutOfBoundsException exception: an access to element 0 of an empty array.
It seems to be this line that cause the problem: text.setText(globals.getList().get(globals.clickedPosition));
Maybee you can check the size of globals.getList() ?
I try to reproduce some videos in a sequence, this is the code, and the error message. I create the sd card in eclipse with the option Run -> Debug Configurations, is a 2GB sd card. I copied some videos to the sd card. So, I appreciate your help. Thanks.
MovieG.java
package com.example.movieg;
import java.io.File;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.widget.MediaController;
import android.widget.VideoView;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
public class MovieG extends Activity {
public VideoView videoView;
public String ex_name;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_movie_g);
ex_name = getIntent().getExtras().getString("video_name");
MediaController mediaController = new MediaController(this);
mediaController.setAnchorView(videoView);
videoView.setMediaController(new MediaController(this));
handler.sendEmptyMessage(1);
}
#SuppressLint("HandlerLeak")
Handler handler = new Handler(){
public void handleMessage(Message msg){
int pos=msg.what;
if (pos == 1){
File ex_name = null;
videoView.setVideoPath(Environment.getExternalStorageDirectory()+"/"+ex_name+".mp4");
videoView.requestFocus();
videoView.start();
Log.d("Before Video Finish", "i m in before video finish");
videoView.setOnCompletionListener(new OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
finish();
}
});
}
}
};
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.movie_g, menu);
return true;
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.movieg"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme">
<activity
android:name="com.example.movieg.MovieG"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Error Message
02-24 21:42:54.916: D/AndroidRuntime(278): Shutting down VM
02-24 21:42:54.916: W/dalvikvm(278): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
02-24 21:42:54.946: E/AndroidRuntime(278): FATAL EXCEPTION: main
02-24 21:42:54.946: E/AndroidRuntime(278): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.movieg/com.example.movieg.MovieG}: java.lang.NullPointerException
02-24 21:42:54.946: E/AndroidRuntime(278): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
02-24 21:42:54.946: E/AndroidRuntime(278): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
02-24 21:42:54.946: E/AndroidRuntime(278): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
02-24 21:42:54.946: E/AndroidRuntime(278): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
02-24 21:42:54.946: E/AndroidRuntime(278): at android.os.Handler.dispatchMessage(Handler.java:99)
02-24 21:42:54.946: E/AndroidRuntime(278): at android.os.Looper.loop(Looper.java:123)
02-24 21:42:54.946: E/AndroidRuntime(278): at android.app.ActivityThread.main(ActivityThread.java:4627)
02-24 21:42:54.946: E/AndroidRuntime(278): at java.lang.reflect.Method.invokeNative(Native Method)
02-24 21:42:54.946: E/AndroidRuntime(278): at java.lang.reflect.Method.invoke(Method.java:521)
02-24 21:42:54.946: E/AndroidRuntime(278): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
02-24 21:42:54.946: E/AndroidRuntime(278): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
02-24 21:42:54.946: E/AndroidRuntime(278): at dalvik.system.NativeStart.main(Native Method)
02-24 21:42:54.946: E/AndroidRuntime(278): Caused by: java.lang.NullPointerException
02-24 21:42:54.946: E/AndroidRuntime(278): at com.example.movieg.MovieG.onCreate(MovieG.java:28)
02-24 21:42:54.946: E/AndroidRuntime(278): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
02-24 21:42:54.946: E/AndroidRuntime(278): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
02-24 21:42:54.946: E/AndroidRuntime(278): ... 11 more
You should verify that getIntent() has extras:
if (getIntent().getExtras() != null)
ex_name = getIntent().getExtras().getString("video_name");
You then need to cater for ex_name being null later in the code.
Second thing is, you never initialise videoView variable. You need to create an instance of it before it's used in onCreate().
You have not initialize the videoview:
videoView =(VideoView)findViewById(R.id.your_id);
before 28 th line
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.
Hi I'm trying to build a calculator to calculate compound interest, I'm keep getting a java.lang.RuntimeException error. I've read some topics on stackoverflow and their problems seems something wrong with their AndroidManifest.xml. I didn't edit that file at all but I'm still getting this error.
Here's the code:
package com.hychentsa.compoundinterest_v001;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import java.lang.Math;
public class MainActivity extends Activity {
private Button btn1;
private EditText edt1, edt2, edt3, edt4 ,edt5, edt6;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn1 = (Button)findViewById(R.id.btn1);
edt1 = (EditText)findViewById(R.id.edt1);
edt2 = (EditText)findViewById(R.id.edt2);
edt3 = (EditText)findViewById(R.id.edt3);
edt4 = (EditText)findViewById(R.id.edt4);
edt5 = (EditText)findViewById(R.id.edt5);
edt6 = (EditText)findViewById(R.id.edt6);
btn1.setOnClickListener(btn1listener);
}
double capital = Double.parseDouble(edt1.getText().toString());
double everyyear = Double.parseDouble(edt2.getText().toString());
double interest = Double.parseDouble(edt3.getText().toString());
double year = Double.parseDouble(edt4.getText().toString());
private Button.OnClickListener btn1listener = new Button.OnClickListener()
{
public void onClick (View v)
{
double capital = Double.parseDouble(edt1.getText().toString());
double everyyear = Double.parseDouble(edt2.getText().toString());
double interest = Double.parseDouble(edt3.getText().toString());
double year = Double.parseDouble(edt4.getText().toString());
double interestperyear = capital * Math.pow(1 + interest, year) +
everyyear * (1 - Math.pow(1 + interest, year)) / interest * -1;
edt5.setText(String.valueOf(interestperyear));
edt6.setText(String.valueOf(interestperyear / 12));
}
};
#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;
}
}
And here's the logcat:
07-15 13:45:23.640: D/AndroidRuntime(1763): Shutting down VM
07-15 13:45:23.650: W/dalvikvm(1763): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
07-15 13:45:23.660: E/AndroidRuntime(1763): FATAL EXCEPTION: main
07-15 13:45:23.660: E/AndroidRuntime(1763): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.hychentsa.compoundinterest_v001/com.hychentsa.compoundinterest_v001.MainActivity}: java.lang.NullPointerException
07-15 13:45:23.660: E/AndroidRuntime(1763): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2106)
07-15 13:45:23.660: E/AndroidRuntime(1763): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
07-15 13:45:23.660: E/AndroidRuntime(1763): at android.app.ActivityThread.access$600(ActivityThread.java:141)
07-15 13:45:23.660: E/AndroidRuntime(1763): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
07-15 13:45:23.660: E/AndroidRuntime(1763): at android.os.Handler.dispatchMessage(Handler.java:99)
07-15 13:45:23.660: E/AndroidRuntime(1763): at android.os.Looper.loop(Looper.java:137)
07-15 13:45:23.660: E/AndroidRuntime(1763): at android.app.ActivityThread.main(ActivityThread.java:5041)
07-15 13:45:23.660: E/AndroidRuntime(1763): at java.lang.reflect.Method.invokeNative(Native Method)
07-15 13:45:23.660: E/AndroidRuntime(1763): at java.lang.reflect.Method.invoke(Method.java:511)
07-15 13:45:23.660: E/AndroidRuntime(1763): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
07-15 13:45:23.660: E/AndroidRuntime(1763): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
07-15 13:45:23.660: E/AndroidRuntime(1763): at dalvik.system.NativeStart.main(Native Method)
07-15 13:45:23.660: E/AndroidRuntime(1763): Caused by: java.lang.NullPointerException
07-15 13:45:23.660: E/AndroidRuntime(1763): at com.hychentsa.compoundinterest_v001.MainActivity.<init>(MainActivity.java:30)
07-15 13:45:23.660: E/AndroidRuntime(1763): at java.lang.Class.newInstanceImpl(Native Method)
07-15 13:45:23.660: E/AndroidRuntime(1763): at java.lang.Class.newInstance(Class.java:1319)
07-15 13:45:23.660: E/AndroidRuntime(1763): at android.app.Instrumentation.newActivity(Instrumentation.java:1054)
07-15 13:45:23.660: E/AndroidRuntime(1763): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2097)
07-15 13:45:23.660: E/AndroidRuntime(1763): ... 11 more
double capital = Double.parseDouble(edt1.getText().toString());
Happens at the init of your activity, which is much before you call edt1 = (EditText)findViewById(R.id.edt1);.
You can declare your double capital ... in the implicit init of the class, but the reference to edt1 must be after onCreate is called.
Also, the actual content of edt1.getText is not relevant even at this point as it is empty, hence will also cause an Exception.
Note that declaring the variable under the onCreate method doesn't make it be called after onCreate.
I don't think you need those 4 lines al all:
double capital = Double.parseDouble(edt1.getText().toString());
double everyyear = Double.parseDouble(edt2.getText().toString());
double interest = Double.parseDouble(edt3.getText().toString());
double year = Double.parseDouble(edt4.getText().toString());
Remove the below which is before button listener. This causes NPE. Its outside of the method. Also you have the same inside button click so you can remove the ones outside the methods.
double capital = Double.parseDouble(edt1.getText().toString());
double everyyear = Double.parseDouble(edt2.getText().toString());
double interest = Double.parseDouble(edt3.getText().toString());
double year = Double.parseDouble(edt4.getText().toString());
Guys i'm having a problem making this run in android but no problems when i run it in java.
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = (TextView) findViewById(R.id.txtview);
Button bt = (Button) findViewById(R.id.button1);
am = this.getAssets();
bt.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// do something when the button is clicked
try {
parsePdf("android.resource://com.example.panalyzer_v1/raw/resume.pdf","android.resource://com.example.panalyzer_v1/raw/resume.txt");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
);
}
public void parsePdf(String pdf, String txt) throws IOException {
PdfReader reader = new PdfReader(pdf);
PrintWriter out = new PrintWriter(new FileOutputStream(txt));
for (int i = 1; i <= reader.getNumberOfPages(); i++) {
out.println(PdfTextExtractor.getTextFromPage(reader, i));
}
out.flush();
out.close();
}
This code will extract the whole text in the PDF and transfer it in a text file but I do not know if Android can do that.
I think my problem here is the filepathing: parsePdf("assets/Resume.pdf","assets/Resume.txt"); I can't make it work.
I have changed the pathing but the error is not solved. I debugged it and i still got an error:
10-22 20:16:13.850: E/AndroidRuntime(657): FATAL EXCEPTION: main
10-22 20:16:13.850: E/AndroidRuntime(657): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.panalyzerdemo/com.example.panalyzerdemo.MainActivity}: java.lang.NullPointerException
10-22 20:16:13.850: E/AndroidRuntime(657): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
10-22 20:16:13.850: E/AndroidRuntime(657): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
10-22 20:16:13.850: E/AndroidRuntime(657): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
10-22 20:16:13.850: E/AndroidRuntime(657): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
10-22 20:16:13.850: E/AndroidRuntime(657): at android.os.Handler.dispatchMessage(Handler.java:99)
10-22 20:16:13.850: E/AndroidRuntime(657): at android.os.Looper.loop(Looper.java:123)
10-22 20:16:13.850: E/AndroidRuntime(657): at android.app.ActivityThread.main(ActivityThread.java:4627)
10-22 20:16:13.850: E/AndroidRuntime(657): at java.lang.reflect.Method.invokeNative(Native Method)
10-22 20:16:13.850: E/AndroidRuntime(657): at java.lang.reflect.Method.invoke(Method.java:521)
10-22 20:16:13.850: E/AndroidRuntime(657): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
10-22 20:16:13.850: E/AndroidRuntime(657): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
10-22 20:16:13.850: E/AndroidRuntime(657): at dalvik.system.NativeStart.main(Native Method)
10-22 20:16:13.850: E/AndroidRuntime(657): Caused by: java.lang.NullPointerException
10-22 20:16:13.850: E/AndroidRuntime(657): at com.example.panalyzerdemo.MainActivity.onCreate(MainActivity.java:36)
10-22 20:16:13.850: E/AndroidRuntime(657): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
10-22 20:16:13.850: E/AndroidRuntime(657): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
10-22 20:16:13.850: E/AndroidRuntime(657): ... 11 more
10-22 20:16:16.831: I/Process(657): Sending signal. PID: 657 SIG: 9
i know the problem and it is PdfReader reader = new PdfReader(pdf);. Did i get the pathing wrong?
PS:I am a newbie when it comes to debugs. please correct me if its the wrong debug report.
Thank you.
To open an asset file in Android you should use getAssets().open("Resume.txt").
There are 3 solutions that you can try.
Instead of putting the file in assets folder put in a raw folder under res. And use the following path to refer it.
"android.resource://[your_package_name]/raw/Resume.pdf"
Asset folder path should be given like this
file:///android_asset/Resume.pdf
Instead of putting the file in assets folder put it in SD Card and SD card root path can be obtained like this
Environment.getExternalStorageDirectory();
ahh Yes! i solved it. instead of using file path i used:
reader = new PdfReader(getResources().openRawResource(R.raw.resume));