这是第二个活动OnCreate
final Button arrayBtn = (Button) findViewById(buttonQtyID[i]);
arrayBtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
ClearQtyBtnColor();
arrayBtn.getBackground().setColorFilter(0xffff9999, PorterDuff.Mode.MULTIPLY);
MainActivity cls2= new MainActivity();
String Price = cls2.CheckUnitPrice("NORMAL"); // ERROR AT HERE
TextView view = (TextView) findViewById(R.id.Harga_lblPrice);
//view.setText((Double.parseDouble(Price) * Integer.parseInt(arrayBtn.getText().toString()))+"");
}
});
这是主要活动中的编码
public Cursor DB_GetUnitPrice(){
DataBaseHelper myDbHelper = new DataBaseHelper(this.getApplicationContext());
myDbHelper = new DataBaseHelper(this);
try {
myDbHelper.createDataBase();
} catch (IOException ioe) {
throw new Error("Unable to create database");
}
try {
myDbHelper.openDataBase();
}catch(SQLException sqle){
throw sqle;
}
SQLiteDatabase db = myDbHelper.getReadableDatabase();
String Insert_Data="Select Normal_Rate,Discounted_Rate FROM Tbl_Rate WHERE Route_ID="+global.Route_ID+" AND ((From_LocationID="+global.From_LocationID+" AND To_LocationID="+global.To_LocationID+") OR (From_LocationID="+global.To_LocationID+" AND To_LocationID="+global.From_LocationID + "))";
Cursor c = db.rawQuery(Insert_Data, null);
//c.close(); // cannot will error
//db.close(); // cannot will error
//myDbHelper.close();
return c;
}
public String CheckUnitPrice(String PriceType){
if ((global.To_LocationID != 0) && (global.From_LocationID != 0)) {
Cursor c = DB_GetUnitPrice();
double Normal_Rate = 0;
double Discounted_Rate = 0;
while (c.moveToNext()) {
Normal_Rate = c.getDouble(c.getColumnIndex("Normal_Rate"));
Discounted_Rate = c.getDouble(c.getColumnIndex("Discounted_Rate"));
}
if (PriceType.equalsIgnoreCase("NORMAL")){
return String.format("%.2f",(Normal_Rate));
}else{
return String.format("%.2f",(Discounted_Rate));
}
}else{
return "0.00";
}
}
这是我在全局类
中的全局变量声明public class Global extends Application {
String Driver_Code;
String Driver_Name;
long Driver_ID;
long Vehicle_ID;
String Plat_No;
long Route_ID;
int From_LocationID;
int To_LocationID;
String From_LocationName;
String To_LocationName;
int From_Tag;
int To_Tag;
}
这是Log cat错误日志
12-02 14:13:48.718: E/AndroidRuntime(10643): FATAL EXCEPTION: main
12-02 14:13:48.718: E/AndroidRuntime(10643): java.lang.NullPointerException
12-02 14:13:48.718: E/AndroidRuntime(10643): at com.example.abc2.MainActivity.CheckUnitPrice(MainActivity.java:246)
12-02 14:13:48.718: E/AndroidRuntime(10643): at com.example.abc2.SecondActivity$2.onClick(SecondActivity.java:58)
12-02 14:13:48.718: E/AndroidRuntime(10643): at android.view.View.performClick(View.java:2485)
12-02 14:13:48.718: E/AndroidRuntime(10643): at android.view.View$PerformClick.run(View.java:9080)
12-02 14:13:48.718: E/AndroidRuntime(10643): at android.os.Handler.handleCallback(Handler.java:587)
12-02 14:13:48.718: E/AndroidRuntime(10643): at android.os.Handler.dispatchMessage(Handler.java:92)
12-02 14:13:48.718: E/AndroidRuntime(10643): at android.os.Looper.loop(Looper.java:123)
12-02 14:13:48.718: E/AndroidRuntime(10643): at android.app.ActivityThread.main(ActivityThread.java:3647)
12-02 14:13:48.718: E/AndroidRuntime(10643): at java.lang.reflect.Method.invokeNative(Native Method)
12-02 14:13:48.718: E/AndroidRuntime(10643): at java.lang.reflect.Method.invoke(Method.java:507)
12-02 14:13:48.718: E/AndroidRuntime(10643): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
12-02 14:13:48.718: E/AndroidRuntime(10643): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
12-02 14:13:48.718: E/AndroidRuntime(10643): at dalvik.system.NativeStart.main(Native Method)
在Main活动上调用CheckUnitPrice
工作正常,可以从Sqlite返回Price,但是当从另一个活动调用时,会出现问题。请帮忙
答案 0 :(得分:0)
您应该将Global类中的变量设置为public,并从ApplicationContext获取对该类的访问权。
Global state = ((Global) getApplicationContext());
int value = state.To_LocationID
此外,在application
标记下的清单文件中输入一个条目,让系统知道您的应用程序类。
android:name="Global"
注意:您应该将Global
中的成员变量设为私有,并为这些变量生成getters and setters
。避免直接访问变量。
答案 1 :(得分:0)
如果MainActivity类扩展了Activity,则无法使用此构造:
MainActivity cls2= new MainActivity();
创建简单类并在其中创建静态方法。
答案 2 :(得分:0)
试试这个......你需要给变量public static
,然后才能得到那个变量
public class Global extends Application {
public static String Driver_Code;
public static String Driver_Name;
public static long Driver_ID;
public static long Vehicle_ID;
public static String Plat_No;
public static long Route_ID;
public static int From_LocationID;
public static int To_LocationID;
public static String From_LocationName;
public static String To_LocationName;
public static int From_Tag;
public static int To_Tag;
}
您可以访问以下变量..
if ((Global.To_LocationID != 0) && (Global.From_LocationID != 0)) {