Hey, so I've been searching around, and thinking for past two hours, but seems like I am just too retarded for this, anyway:
Here is a class that is supposed to generate random values, assign a currency sign from sharedpreferences (selected on different activity/class), then stored in variables.
Seems fine. But..
[code]
package com.example.droid;
import java.text.DecimalFormat;
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
public class ObtainCurrentMon extends Activity{
boolean loggedIn;
double inc;
double exp;
DecimalFormat curency = new DecimalFormat("###.##");
public static final String PREFS_NAME = "Sets";
//Temporary values:
public String GetCurrentExpense(){
this.exp = Math.random()*100;
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
return settings.getString("SelectedCur", "€")+curency.format(exp);
}
public String GetCurrentIncome(){
this.inc = Math.random()*100;
if( exp > inc){
this.inc = Math.random()*100;
}
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
return settings.getString("SelectedCur", "€").toString()+curency.format(inc);
}
}
[/code]
In different class, it's supposed to take those variables and assign them to label/textview.
It has exception throwing ERROR #2 if something goes wrong.
[code]
try{
SetTexter(ocm.GetCurrentExpense(), R.id.CurMonth_Exp);
SetTexter(ocm.GetCurrentIncome(), R.id.CurMonth_Inc);
}
catch(Exception e){
SetTexter("ERROR #2", R.id.CurMonth_Exp);
SetTexter("ERROR #2", R.id.CurMonth_Inc);
}
[/code]
So when I use: return settings.getString("SelectedCur", "€")+curency.format(exp);
It throws ERROR #2
but when I use just:
"€"+curency.format(exp); it works fine, generates values and then also outputs them no problem.
As far as I am aware, if it doesn't get sharedpref it should return € right? but it doesn't I think.
Help?
Anyone pls? :L
Check the exception thrown for more info?
[QUOTE=NovembrDobby;39710284]Check the exception thrown for more info?[/QUOTE]
All it gives me is:
"
02-25 14:13:52.169: E/AndroidRuntime(30194): FATAL EXCEPTION: main
02-25 14:13:52.169: E/AndroidRuntime(30194): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.droid/com.example.droid.MainActivity}: java.lang.NullPointerException
02-25 14:13:52.169: E/AndroidRuntime(30194): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1573)
02-25 14:13:52.169: E/AndroidRuntime(30194): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
02-25 14:13:52.169: E/AndroidRuntime(30194): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
02-25 14:13:52.169: E/AndroidRuntime(30194): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
02-25 14:13:52.169: E/AndroidRuntime(30194): at android.os.Handler.dispatchMessage(Handler.java:99)
02-25 14:13:52.169: E/AndroidRuntime(30194): at android.os.Looper.loop(Looper.java:130)
02-25 14:13:52.169: E/AndroidRuntime(30194): at android.app.ActivityThread.main(ActivityThread.java:3687)
02-25 14:13:52.169: E/AndroidRuntime(30194): at java.lang.reflect.Method.invokeNative(Native Method)
02-25 14:13:52.169: E/AndroidRuntime(30194): at java.lang.reflect.Method.invoke(Method.java:507)
02-25 14:13:52.169: E/AndroidRuntime(30194): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
02-25 14:13:52.169: E/AndroidRuntime(30194): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
02-25 14:13:52.169: E/AndroidRuntime(30194): at dalvik.system.NativeStart.main(Native Method)
02-25 14:13:52.169: E/AndroidRuntime(30194): Caused by: java.lang.NullPointerException
02-25 14:13:52.169: E/AndroidRuntime(30194): at android.content.ContextWrapper.getSharedPreferences(ContextWrapper.java:146)
02-25 14:13:52.169: E/AndroidRuntime(30194): at com.example.droid.ObtainCurrentMon.<init>(ObtainCurrentMon.java:15)
02-25 14:13:52.169: E/AndroidRuntime(30194): at com.example.droid.MainActivity.<init>(MainActivity.java:24)
02-25 14:13:52.169: E/AndroidRuntime(30194): at java.lang.Class.newInstanceImpl(Native Method)
02-25 14:13:52.169: E/AndroidRuntime(30194): at java.lang.Class.newInstance(Class.java:1409)
02-25 14:13:52.169: E/AndroidRuntime(30194): at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
02-25 14:13:52.169: E/AndroidRuntime(30194): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1565)
02-25 14:13:52.169: E/AndroidRuntime(30194): ... 11 more
"
[editline]25th February 2013[/editline]
For some reason I feel it has something to do with:
public class ObtainCurrentMon extends Activity{
and
SharedPreferences.
I tried also declaring this class as Activity in manifest but that didn't change anything.
You have a NullPointerException at line 15 in ObtainCurrentMon, which seems to be empty (or you didn't copy your code exact) But why are you extending Activity? I think that'll just cause errors. When I think about it, the exception complains that the error is inside <init> (which would be your constructor, ObtainCurrentMon()), which you don't have. I'd try getting rid of the "extends Activity" and see if that fixes it.
[QUOTE=Gulen;39711395]You have a NullPointerException at line 15 in ObtainCurrentMon, which seems to be empty (or you didn't copy your code exact) But why are you extending Activity? I think that'll just cause errors. When I think about it, the exception complains that the error is inside <init> (which would be your constructor, ObtainCurrentMon()), which you don't have. I'd try getting rid of the "extends Activity" and see if that fixes it.[/QUOTE]
The only reason I extended Activity is because without it sharedprefs doesn't work. (not that it works that way either anyway)
Have you tried passing a Context to this class and calling getSharedPreferences off that?
[QUOTE=Gulen;39711460]Have you tried passing a Context to this class and calling getSharedPreferences off that?[/QUOTE]
How? any help or tutorials on that?
i was using this [url]https://github.com/makepanic/rndm-todo/blob/master/src/de/rndm/todo/model/AppPreferences.java[/url] worked fine for me
damn... I don't get it...
[cpp]package com.example.droid;
import java.text.DecimalFormat;
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
public class ObtainCurrentMon{
boolean loggedIn;
MainActivity parent;
double inc;
double exp;
DecimalFormat curency = new DecimalFormat("###.##");
public static final String PREFS_NAME = "Sets";
public ObtainCurrentMon(MainActivity parent);
//Temporary values:
public String GetCurrentExpense(){
this.exp = Math.random()*100;
SharedPreferences settings = parent.getSharedPreferences(PREFS_NAME, 0);
return settings.getString("SelectedCur", "€")+curency.format(exp);
}
public String GetCurrentIncome(){
this.inc = Math.random()*100;
if( exp > inc){
this.inc = Math.random()*100;
}
SharedPreferences settings = parent.getSharedPreferences(PREFS_NAME, 0);
return settings.getString("SelectedCur", "€").toString()+curency.format(inc);
}
}[/cpp]
There you go.
[QUOTE=Gulen;39713030][cpp]package com.example.droid;
import java.text.DecimalFormat;
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
public class ObtainCurrentMon{
boolean loggedIn;
MainActivity parent;
double inc;
double exp;
DecimalFormat curency = new DecimalFormat("###.##");
public static final String PREFS_NAME = "Sets";
public ObtainCurrentMon(MainActivity parent);
//Temporary values:
public String GetCurrentExpense(){
this.exp = Math.random()*100;
SharedPreferences settings = parent.getSharedPreferences(PREFS_NAME, 0);
return settings.getString("SelectedCur", "€")+curency.format(exp);
}
public String GetCurrentIncome(){
this.inc = Math.random()*100;
if( exp > inc){
this.inc = Math.random()*100;
}
SharedPreferences settings = parent.getSharedPreferences(PREFS_NAME, 0);
return settings.getString("SelectedCur", "€").toString()+curency.format(inc);
}
}[/cpp]
There you go.[/QUOTE]
Thank you sir.
Sorry, you need to Log In to post a reply to this thread.