0

这是我主要活动的来源,我尝试在 Android 2.3.7 和 Android 4.1 及更高版本中启动它,它在 Android 2.3.7 中成功运行,但在 Android 4.1 中崩溃。

public class CalculatorActivity extends Activity implements LocationListener 
{
protected static final String TAG = "123";
public static Handler h;




@Override
protected void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_calculator);
    (LocationManager)getSystemService(Context.LOCATION_SERVICE);


    final SharedPreferences unitpreference = PreferenceManager.getDefaultSharedPreferences(this);
    final String unitstring = unitpreference.getString("unit_type", "");
    final int unit = Integer.parseInt(unitstring);

      changecityname();
      changeusername();



    if (unit==1)
    {

    TextView kms1 = (TextView) findViewById(R.id.textView7);
    kms1.setText("kms.");

    TextView kml = (TextView) findViewById(R.id.textView8);
    kml.setText("km/lt");

    TextView kms2 = (TextView) findViewById(R.id.textView9);
    kms2.setText("kms.");

    TextView rs = (TextView) findViewById(R.id.textView10);
    rs.setText("Rs.");
    }

    else
    {
        TextView kms1 = (TextView) findViewById(R.id.textView7);
        kms1.setText("mi.");

        TextView kml = (TextView) findViewById(R.id.textView8);
        kml.setText("mi/ga");

        TextView kms2 = (TextView) findViewById(R.id.textView9);
        kms2.setText("mi.");

        TextView rs = (TextView) findViewById(R.id.textView10);
        rs.setText("$");
    }


    clickbutton();

};


private void changeusername() 
{
final SharedPreferences currentusername = getSharedPreferences("PREFS_NAME",0);
final String loggedInUser = currentusername.getString("username", "");
TextView loggedInUsername = (TextView) findViewById(R.id.textView11);
loggedInUsername.setText("Logged in as "+loggedInUser);

}


private void changecityname() 
{
final SharedPreferences citypreference = PreferenceManager.getDefaultSharedPreferences(this);
final String citystring = citypreference.getString("example_list", "");
int city = Integer.parseInt(citystring);
TextView cityname = (TextView) findViewById(R.id.textView12);

if (city==1)
{
    cityname.setText("Panchkula   ");
}
else if (city==2)
{
    cityname.setText("Chandigarh   ");
}
else if (city==3)
{
    cityname.setText("Mohali   ");
}
else
{
    cityname.setText("Your City   ");
}


}



@Override
public void onResume() {
    super.onResume();

    final SharedPreferences unitpreference = PreferenceManager.getDefaultSharedPreferences(this);
    final String unitstring = unitpreference.getString("unit_type", "");
    final int unit = Integer.parseInt(unitstring);


    final SharedPreferences citypreference = PreferenceManager.getDefaultSharedPreferences(this);
    final String citystring = citypreference.getString("example_list", "");
    int city = Integer.parseInt(citystring);


    final SharedPreferences fuelpreference = PreferenceManager.getDefaultSharedPreferences(this);
    final String fuelstring = fuelpreference.getString("fuel_type", "");
    int fuel = Integer.parseInt(fuelstring);




    if (unit==1)
    {

    TextView kms1 = (TextView) findViewById(R.id.textView7);
    kms1.setText("kms.");

    TextView kml = (TextView) findViewById(R.id.textView8);
    kml.setText("km/lt");

    TextView kms2 = (TextView) findViewById(R.id.textView9);
    kms2.setText("kms.");

    TextView rs = (TextView) findViewById(R.id.textView10);
    rs.setText("Rs.");
    }

    else
    {
        TextView kms1 = (TextView) findViewById(R.id.textView7);
        kms1.setText("mi.");

        TextView kml = (TextView) findViewById(R.id.textView8);
        kml.setText("mi/ga");

        TextView kms2 = (TextView) findViewById(R.id.textView9);
        kms2.setText("mi.");

        TextView rs = (TextView) findViewById(R.id.textView10);
        rs.setText("$");
    }
    clickbutton();
    changecityname();
    changeusername();
    h = new Handler()
    {
        public void handleMessage(Message msg) 
        {
            super.handleMessage(msg);
            switch (msg.what)
            {
            case 0:
                finish();
                break;
            }

        }

    };

}

@Override
protected void onPause() {
    super.onPause();
    final SharedPreferences unitpreference = getSharedPreferences("unit_type",MODE_PRIVATE);
    SharedPreferences.Editor uniteditor = unitpreference.edit();
    uniteditor.commit();


}

public void clickbutton() 
{   
    final SharedPreferences unitpreference = PreferenceManager.getDefaultSharedPreferences(this);
    final String unitstring = unitpreference.getString("unit_type", "");
    final int unit = Integer.parseInt(unitstring);

    final SharedPreferences citypreference = PreferenceManager.getDefaultSharedPreferences(this);
    final String citystring = citypreference.getString("example_list", "");
    int city = Integer.parseInt(citystring);

    final SharedPreferences fuelpreference = PreferenceManager.getDefaultSharedPreferences(this);
    final String fuelstring = fuelpreference.getString("fuel_type", "");
    int fuel = Integer.parseInt(fuelstring);

    SharedPreferences petrolPricesetting = getSharedPreferences("PREFS_NAME", 0);
    SharedPreferences dieselPricesetting = getSharedPreferences("PREFS_NAME", 0);
    float citypetrolprice =petrolPricesetting.getFloat("petrolprice", 70.0f);
    float citydieselprice =dieselPricesetting.getFloat("dieselprice", 60.0f);





    final double price;
    if (fuel==1)
    {
        if (city==1)
        {
            price=69.19;
        }
        else if (city==2)
        {
            price=69.59;
        }
        else if (city==3)
        {
            price=76.18;
        }
        else 
        {
            price=citypetrolprice;

        }

    }
    else
    {
        if (city==1)
        {
            price=47.24;
        }
        else if (city==2)
        {
            price=49.42;
        }
        else if (city==3)
        {
            price=47.56;
        }
        else
        {
            price=citydieselprice;

        }

    }

    Button livecost = (Button) findViewById(R.id.button3);
    livecost.setOnClickListener(new OnClickListener()
    {

        @Override
        public void onClick(View arg0) 
        {
            Intent startlive = new Intent(CalculatorActivity.this,Live.class);
            CalculatorActivity.this.startActivity(startlive);

        }

    });


    Button calculatemileage = (Button) findViewById(R.id.button1);
    calculatemileage.setOnClickListener(new OnClickListener()
    {
        @Override
        public void onClick(View arg0)
        {
            EditText distancetravelled= (EditText) findViewById(R.id.editText1);
            EditText refillingcost= (EditText) findViewById(R.id.editText2);

            if (distancetravelled.length()==0 || refillingcost.length()==0 )
            {
                Log.v(TAG, "Setting has been changed!");
                Toast.makeText(getApplicationContext(), "Please fill both the distance travelled and the cost!", Toast.LENGTH_SHORT).show();
            }

            else 
            {
            String stringdistancetravelled=distancetravelled.getText().toString();
            String stringrefillingcost=refillingcost.getText().toString();
            Log.v(TAG, "Setting has been changed!");


            float floatdistancetravelled =Float.valueOf(stringdistancetravelled);
            float floatrefillingcost =Float.valueOf(stringrefillingcost);

            double mileageanswer = (floatdistancetravelled/floatrefillingcost)*price;
            final float floatmileageanswer = (float) mileageanswer;
            double mileageanswerroundoff = Math.round(floatmileageanswer*100.0)/100.0;

            double mileageanswerimperial = mileageanswer*0.621371192;
            final float floatmileageanswerimperial = (float) mileageanswerimperial;
            double mileageanswerimperialroundoff = Math.round(floatmileageanswerimperial*100.0)/100.0;

            if (unit==1)
            {
            Toast.makeText(getApplicationContext(), mileageanswerroundoff+" km/l", Toast.LENGTH_LONG).show();
            }
            else
            {
                Toast.makeText(getApplicationContext(), mileageanswerimperialroundoff+" mi/ga", Toast.LENGTH_LONG).show();
            }

        }
        }});

    Button calculatecost= (Button) findViewById(R.id.button2);
    calculatecost.setOnClickListener(new OnClickListener()
    {
        @Override
        public void onClick(View arg0)
        {
            EditText mileage= (EditText) findViewById(R.id.editText3);
            EditText distance= (EditText) findViewById(R.id.editText4);

            if (mileage.getText().length()==0 || distance.getText().length()==0 )
            {
                Toast.makeText(getApplicationContext(), "Please fill both the distance to travel and the mileage!", Toast.LENGTH_SHORT).show();
            }
            else 
            {



            String stringmileage=mileage.getText().toString();
            String stringdistance=distance.getText().toString();






            float floatmileage =Float.valueOf(stringmileage);
            float floatdistance =Float.valueOf(stringdistance);

            double costanswer = (floatdistance)*(price/floatmileage);
            final float floatcostanswer = (float) costanswer;
            double costanswerroundoff = Math.round(floatcostanswer*100.0)/100.0;

            double costanswerimperial = costanswer*0.621371192*0.01661;
            final float floatcostanswerimperial = (float) costanswerimperial;
            double costanswerimperialroundoff = Math.round(floatcostanswerimperial*100.0)/100.0;

            if (unit==1)                
            {
                Toast.makeText(getApplicationContext(), "Rs."+costanswerroundoff, Toast.LENGTH_LONG).show();
            }
            else
            {
                Toast.makeText(getApplicationContext(), "$"+costanswerimperialroundoff, Toast.LENGTH_LONG).show();
            }

        }

    }});


}   

@Override
public boolean onCreateOptionsMenu(Menu menu) 
{

    getMenuInflater().inflate(R.menu.activity_calculator, menu);
    return true;
}

public boolean onMenuItemSelected(int featureId, MenuItem item) 
{

    if (item.getItemId()==R.id.menu_settings)
    {
        Intent startsettings = new Intent(CalculatorActivity.this,SettingsActivity.class);
        CalculatorActivity.this.startActivity(startsettings);

    }
    else
    {
        Intent startlogin = new Intent(CalculatorActivity.this,Login.class);
        CalculatorActivity.this.startActivity(startlogin);
        CheckBox keeplog = (CheckBox) findViewById(R.id.checkBox1);
        boolean isChecked = false;
        SharedPreferences settings = getSharedPreferences("PREFS_NAME", 0);
        SharedPreferences.Editor editor = settings.edit();
        editor.putBoolean("isChecked", isChecked);
        editor.commit();
        finish();
    }



return true;
}

@Override
public void onLocationChanged(Location location) 
{

}

@Override
public void onProviderDisabled(String arg0) 
{


}

@Override
public void onProviderEnabled(String arg0) 
{


}

@Override
public void onStatusChanged(String arg0, int arg1, Bundle arg2) 
{


}

}

布局文件:

<TextView
    android:id="@+id/textView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/textView1"
    android:layout_marginLeft="24dp"
    android:layout_marginTop="32dp"
    android:text="Distance: "
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:textColor="@android:color/white" />

<TextView
    android:id="@+id/textView3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/textView2"
    android:layout_below="@+id/textView2"
    android:layout_marginTop="39dp"
    android:text="Cost:"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:textColor="@android:color/white" />

<EditText
    android:id="@+id/editText2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/textView3"
    android:layout_alignBottom="@+id/textView3"
    android:layout_alignLeft="@+id/editText1"
    android:ems="10"
    android:inputType="numberDecimal"
    android:width="50dp" />

<TextView
    android:id="@+id/textView10"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/editText2"
    android:layout_toLeftOf="@+id/editText2"
    android:text="Rs."
    android:textAppearance="?android:attr/textAppearanceSmall"
    android:textColor="@android:color/white" />

<TextView
    android:id="@+id/textView9"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/textView6"
    android:layout_toRightOf="@+id/editText4"
    android:text="kms."
    android:textAppearance="?android:attr/textAppearanceSmall"
    android:textColor="@android:color/white" />

<EditText
    android:id="@+id/editText1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignRight="@+id/textView1"
    android:layout_alignTop="@+id/textView2"
    android:ems="10"
    android:height="10dp"
    android:inputType="numberDecimal"
    android:maxHeight="10dp"
    android:width="50dp" />

<TextView
    android:id="@+id/textView7"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/editText1"
    android:layout_alignBottom="@+id/editText1"
    android:layout_toRightOf="@+id/editText1"
    android:text="kms."
    android:textAppearance="?android:attr/textAppearanceSmall"
    android:textColor="@android:color/white" />

<EditText
    android:id="@+id/editText3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/textView5"
    android:layout_alignBottom="@+id/textView5"
    android:layout_toRightOf="@+id/textView10"
    android:ems="10"
    android:inputType="numberDecimal"
    android:width="50dp" />

<TextView
    android:id="@+id/textView4"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignRight="@+id/button1"
    android:layout_below="@+id/button1"
    android:layout_marginTop="26dp"
    android:text="Distance Cost Calculator"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:textColor="@android:color/white"
    android:textStyle="bold" />

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="36dp"
    android:text="Mileage Calculator"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:textColor="@android:color/white"
    android:textStyle="bold" />

<EditText
    android:id="@+id/editText4"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/textView6"
    android:layout_alignBottom="@+id/textView6"
    android:layout_toLeftOf="@+id/textView7"
    android:ems="10"
    android:inputType="numberDecimal"
    android:width="50dp" >

    <requestFocus />
</EditText>

<TextView
    android:id="@+id/textView6"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/textView3"
    android:layout_below="@+id/textView4"
    android:layout_marginTop="32dp"
    android:text="Distance: "
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:textColor="@android:color/white" />

<TextView
    android:id="@+id/textView5"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/textView6"
    android:layout_below="@+id/editText4"
    android:layout_marginTop="34dp"
    android:text="Mileage: "
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:textColor="@android:color/white" />

<Button
    android:id="@+id/button3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/button2"
    android:layout_alignBottom="@+id/button2"
    android:layout_alignRight="@+id/textView8"
    android:background="@drawable/btn_normal"
    android:paddingBottom="12dp"
    android:paddingLeft="20dp"
    android:paddingRight="20dp"
    android:paddingTop="12dp"
    android:text="Live Cost"
    android:textColor="@android:color/white" />

<Button
    android:id="@+id/button2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/editText3"
    android:layout_marginTop="18dp"
    android:layout_toLeftOf="@+id/button3"
    android:background="@drawable/btn_normal"
    android:paddingBottom="12dp"
    android:paddingLeft="15dp"
    android:paddingRight="15dp"
    android:paddingTop="12dp"
    android:text="Calculate Cost"
    android:textColor="@android:color/white" />

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/editText2"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="17dp"
    android:background="@drawable/btn_normal"
    android:paddingBottom="12dp"
    android:paddingLeft="55dp"
    android:paddingRight="55dp"
    android:paddingTop="12dp"
    android:text="Calculate Mileage"
    android:textColor="@android:color/white" />

<TextView
    android:id="@+id/textView8"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/editText3"
    android:layout_alignBottom="@+id/editText3"
    android:layout_alignLeft="@+id/textView9"
    android:text="km/lt"
    android:textAppearance="?android:attr/textAppearanceSmall"
    android:textColor="@android:color/white" />

<TextView
    android:id="@+id/textView11"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:paddingBottom="5dp"
    android:paddingLeft="5dp"
    android:text="Logged in as" />

<TextView
    android:id="@+id/textView12"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true"
    android:paddingBottom="5dp"
    android:text="City"
    android:textAppearance="?android:attr/textAppearanceSmall" />

显现: Changing the theme in Manifest has no effect, I have tried the themes which run with other successful activities!

日志:

08-01 10:38:14.374: E/Trace(14994): error opening trace file: No such file or directory (2)
08-01 10:38:14.390: V/ActivityThread(14994): com.basic.mileagecalculatorwithsettings white listed for hwui
08-01 10:38:14.452: I/System.out(14994): Sending WAIT chunk
08-01 10:38:14.452: W/ActivityThread(14994): Application com.basic.mileagecalculatorwithsettings is waiting for the debugger on port 8100...
08-01 10:38:14.499: I/dalvikvm(14994): Debugger is active
08-01 10:38:14.655: I/System.out(14994): Debugger has connected
08-01 10:38:14.655: I/System.out(14994): waiting for debugger to settle...
08-01 10:38:14.858: I/System.out(14994): waiting for debugger to settle...
08-01 10:38:15.061: I/System.out(14994): waiting for debugger to settle...
08-01 10:38:15.257: I/System.out(14994): waiting for debugger to settle...
08-01 10:38:15.460: I/System.out(14994): waiting for debugger to settle...
08-01 10:38:15.663: I/System.out(14994): waiting for debugger to settle...
08-01 10:38:15.858: I/System.out(14994): waiting for debugger to settle...
08-01 10:38:16.061: I/System.out(14994): waiting for debugger to settle...
08-01 10:38:16.265: I/System.out(14994): waiting for debugger to settle...
08-01 10:38:16.460: I/System.out(14994): waiting for debugger to settle...
08-01 10:38:16.663: I/System.out(14994): debugger has settled (1366)
08-01 10:38:29.194: D/AndroidRuntime(14994): Shutting down VM
08-01 10:38:29.194: W/dalvikvm(14994): threadid=1: thread exiting with uncaught exception (group=0x40aa0440)
08-01 10:38:29.257: E/AndroidRuntime(14994): FATAL EXCEPTION: main
08-01 10:38:29.257: E/AndroidRuntime(14994): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.basic.mileagecalculatorwithsettings/com.basic.mileagecalculatorwithsettings.CalculatorActivity}: java.lang.NumberFormatException: Invalid int: ""
08-01 10:38:29.257: E/AndroidRuntime(14994):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2187)
08-01 10:38:29.257: E/AndroidRuntime(14994):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2212)
08-01 10:38:29.257: E/AndroidRuntime(14994):    at android.app.ActivityThread.access$600(ActivityThread.java:144)
08-01 10:38:29.257: E/AndroidRuntime(14994):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1210)
08-01 10:38:29.257: E/AndroidRuntime(14994):    at android.os.Handler.dispatchMessage(Handler.java:99)
08-01 10:38:29.257: E/AndroidRuntime(14994):    at android.os.Looper.loop(Looper.java:137)
08-01 10:38:29.257: E/AndroidRuntime(14994):    at android.app.ActivityThread.main(ActivityThread.java:4966)
08-01 10:38:29.257: E/AndroidRuntime(14994):    at java.lang.reflect.Method.invokeNative(Native Method)
08-01 10:38:29.257: E/AndroidRuntime(14994):    at java.lang.reflect.Method.invoke(Method.java:511)
08-01 10:38:29.257: E/AndroidRuntime(14994):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
08-01 10:38:29.257: E/AndroidRuntime(14994):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:558)
08-01 10:38:29.257: E/AndroidRuntime(14994):    at dalvik.system.NativeStart.main(Native Method)
08-01 10:38:29.257: E/AndroidRuntime(14994): Caused by: java.lang.NumberFormatException: Invalid int: ""
08-01 10:38:29.257: E/AndroidRuntime(14994):    at java.lang.Integer.invalidInt(Integer.java:138)
08-01 10:38:29.257: E/AndroidRuntime(14994):    at java.lang.Integer.parseInt(Integer.java:359)
08-01 10:38:29.257: E/AndroidRuntime(14994):    at java.lang.Integer.parseInt(Integer.java:332)
08-01 10:38:29.257: E/AndroidRuntime(14994):    at com.basic.mileagecalculatorwithsettings.CalculatorActivity.onCreate(CalculatorActivity.java:55)
08-01 10:38:29.257: E/AndroidRuntime(14994):    at android.app.Activity.performCreate(Activity.java:5008)
08-01 10:38:29.257: E/AndroidRuntime(14994):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
08-01 10:38:29.257: E/AndroidRuntime(14994):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2141)
08-01 10:38:29.257: E/AndroidRuntime(14994):    ... 11 more

我认为的错误是 Invalid Int:""。可能是什么原因?

4

3 回答 3

1

unitstring必须以空字符串的形式出现。请检查

final String unitstring = unitpreference.getString("unit_type", "");
final int unit = Integer.parseInt(unitstring);

NumberFormatException你至少应该抓住那个。

于 2013-08-01T05:23:34.630 回答
1
final String unitstring = unitpreference.getString("unit_type", "");
final int unit = Integer.parseInt(unitstring);

每个Android版本都应该失败!

尝试:

final String unitstring = unitpreference.getString("unit_type", "0");
final int unit = Integer.parseInt(unitstring);

注意:实现这一目标的更好方法是将“unit_type”存储为 int,而不是 String

于 2013-08-01T05:23:48.070 回答
1
 final String unitstring = unitpreference.getString("unit_type", "");
 final int unit = Integer.parseInt(unitstring);

在初始点,它在单元字符串中获得“”。所以它不能被解析为整数。所以你的偏好可能是空的

尝试使用:

 final String unitstring = unitpreference.getString("unit_type", "1");
 final int unit = Integer.parseInt(unitstring);

所以unitstring的默认值为“1”。

在偏好的同时,获得了一些价值。总是在“keyvalue(这里“1”是默认值)”中放置一些默认值。因此,每当您的“关键偏好(此处,“unit_type”)”为空或“”或未定义时。然后它将给出“键值”,这是该键的默认值。

于 2013-08-01T05:23:53.897 回答