-1

我是初学者,我已经为我的物理研究启动了一个 android 应用程序项目,其中包含所有公式和函数,我有一个恼人的错误,我不知道如何解决他。

错误是,当我运行应用程序时,它会打开我创建的菜单,当我选择此加速选项时,它会使应用程序崩溃。但是所有其他选项都运行顺利。

java类:

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 android.widget.TextView;

public class Acceleration extends Activity {

EditText etV0, etV1, etT0, etT1;
TextView tvAccResult;
Button accC;
double CetV0, CetV1, CetT0, CetT1, CtvAccResult;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_acceleration);

    initialize();

    accC.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            tvAccResult.setText(Double.toString(CtvAccResult));
        }
    });

}

private void initialize() {
    etV0 = (EditText) findViewById(R.id.etV0);
    etV1 = (EditText) findViewById(R.id.etV1);
    etT0 = (EditText) findViewById(R.id.etT0);
    etT1 = (EditText) findViewById(R.id.etT1);
    tvAccResult = (TextView) findViewById(R.id.tvAccResult);
    accC = (Button) findViewById(R.id.accC);

    CetV0 = (Double.parseDouble(etV0.getText().toString()));
    CetV1 = (Double.parseDouble(etV1.getText().toString()));
    CetT0 = (Double.parseDouble(etT0.getText().toString()));
    CetT1 = (Double.parseDouble(etT1.getText().toString()));

    CtvAccResult = (double) ((CetV0 - CetV1) / (CetT0 - CetT1));
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.acceleration, menu);
    return true;
}

}

这是布局代码:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".Acceleration" >

<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:paddingTop="10dp"
    android:text="@string/acceleration"
    android:textSize="30dp" />

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:orientation="horizontal"
    android:paddingTop="40dp"
    android:weightSum="100" >

    <EditText
        android:id="@+id/etV0"
        android:layout_width="15dp"
        android:layout_height="wrap_content"
        android:layout_weight="16"
        android:ems="10"
        android:hint="V0" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="-"
        android:textSize="30dp" />

    <EditText
        android:id="@+id/etV1"
        android:layout_width="15dp"
        android:layout_height="wrap_content"
        android:layout_weight="16"
        android:ems="10"
        android:hint="V1" />
</LinearLayout>

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:text="________________________" />

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:orientation="horizontal"
    android:paddingTop="10dp"
    android:paddingBottom="20dp"
    android:weightSum="100" >

    <EditText
        android:id="@+id/etT0"
        android:layout_width="15dp"
        android:layout_height="wrap_content"
        android:layout_weight="16"
        android:ems="10"
        android:hint="t0" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="-"
        android:textSize="30dp" />

    <EditText
        android:id="@+id/etT1"
        android:layout_width="15dp"
        android:layout_height="wrap_content"
        android:layout_weight="16"
        android:ems="10"
        android:hint="t1" />
</LinearLayout>

<Button
    android:id="@+id/accC"
    android:layout_width="wrap_content"
    android:layout_height="45dp"
    android:layout_gravity="center"
    android:text="Calculate"
    android:textSize="15dp" />

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:orientation="horizontal"
    android:paddingTop="30dp"
    android:weightSum="100" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="50"
        android:paddingLeft="20dp"
        android:text="Result: "
        android:textSize="20dp" />

    <TextView
        android:id="@+id/tvAccResult"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="50"
        android:paddingLeft="20dp"
        android:text=""
        android:textSize="20dp" />
</LinearLayout>

</LinearLayout>

这是logcat:

09-18 17:08:36.546: E/dalvikvm(8215): could not disable core file generation for         pid8215: Operation not permitted
09-18 17:08:36.648: E/asset(8215): MAS: getAppPckgAndVerCode package: com.bben.physics     === version 1
09-18 17:08:36.664: D/dalvikvm(8215): Trying to load lib lib_glossary.so 0x0
09-18 17:08:36.671: D/dalvikvm(8215): Added shared lib lib_glossary.so 0x0
09-18 17:08:36.671: V/MBGlossaryManager(8215): Current Locale is:he
09-18 17:08:36.679: V/MBGlossaryManager(8215): extended glossary not found, LSA not     installed?
09-18 17:08:36.679: E/MBGlossaryManager(8215): Glossary file(s) not found
09-18 17:08:38.601: D/AndroidRuntime(8215): Shutting down VM
09-18 17:08:38.601: W/dalvikvm(8215): threadid=1: thread exiting with uncaught     exception (group=0x4001e560)
09-18 17:08:38.617: E/AndroidRuntime(8215): FATAL EXCEPTION: main
09-18 17:08:38.617: E/AndroidRuntime(8215): java.lang.RuntimeException: Unable to start     activity ComponentInfo{com.bben.physics/com.bben.physics.Acceleration}:         java.lang.NumberFormatException: 
09-18 17:08:38.617: E/AndroidRuntime(8215):     at         android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1713)
09-18 17:08:38.617: E/AndroidRuntime(8215):     at     android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1738)
09-18 17:08:38.617: E/AndroidRuntime(8215):     at     android.app.ActivityThread.access$1500(ActivityThread.java:124)
09-18 17:08:38.617: E/AndroidRuntime(8215):     at     android.app.ActivityThread$H.handleMessage(ActivityThread.java:980)
09-18 17:08:38.617: E/AndroidRuntime(8215):     at     android.os.Handler.dispatchMessage(Handler.java:99)
09-18 17:08:38.617: E/AndroidRuntime(8215):     at     android.os.Looper.loop(Looper.java:130)
09-18 17:08:38.617: E/AndroidRuntime(8215):     at     android.app.ActivityThread.main(ActivityThread.java:3906)
09-18 17:08:38.617: E/AndroidRuntime(8215):     at     java.lang.reflect.Method.invokeNative(Native Method)
09-18 17:08:38.617: E/AndroidRuntime(8215):     at     java.lang.reflect.Method.invoke(Method.java:507)
09-18 17:08:38.617: E/AndroidRuntime(8215):     at     com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:840)
09-18 17:08:38.617: E/AndroidRuntime(8215):     at     com.android.internal.os.ZygoteInit.main(ZygoteInit.java:598)
09-18 17:08:38.617: E/AndroidRuntime(8215):     at dalvik.system.NativeStart.main(Native Method)
09-18 17:08:38.617: E/AndroidRuntime(8215): Caused by: java.lang.NumberFormatException: 
09-18 17:08:38.617: E/AndroidRuntime(8215):     at org.apache.harmony.luni.util.FloatingPointParser.parseDouble(FloatingPointParser.java:267)
09-18 17:08:38.617: E/AndroidRuntime(8215):     at java.lang.Double.parseDouble(Double.java:318)
09-18 17:08:38.617: E/AndroidRuntime(8215):     at com.bben.physics.Acceleration.initialize(Acceleration.java:43)
09-18 17:08:38.617: E/AndroidRuntime(8215):     at com.bben.physics.Acceleration.onCreate(Acceleration.java:23)
09-18 17:08:38.617: E/AndroidRuntime(8215):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
09-18 17:08:38.617: E/AndroidRuntime(8215):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1677)
09-18 17:08:38.617: E/AndroidRuntime(8215):     ... 11 more
09-18 17:08:42.281: I/Process(8215): Sending signal. PID: 8215 SIG: 9
09-18 17:08:53.015: W/KeyCharacterMap(8345): Can't open keycharmap file
09-18 17:08:53.015: W/KeyCharacterMap(8345): Error loading keycharmap file     '/data/usr/keychars/cy8c201xx.kcm.bin'. hw.keyboards.65539.devname='cy8c201xx'
09-18 17:08:53.015: W/KeyCharacterMap(8345): Can't open keycharmap file
09-18 17:08:53.015: W/KeyCharacterMap(8345): Error loading keycharmap file     '/system/usr/keychars/cy8c201xx.kcm.bin'. hw.keyboards.65539.devname='cy8c201xx'
09-18 17:08:53.015: W/KeyCharacterMap(8345): Can't open keycharmap file
09-18 17:08:53.015: W/KeyCharacterMap(8345): Using default keymap:     /system/usr/keychars/qwerty.kcm.bin
09-18 17:09:03.625: D/AndroidRuntime(8345): Shutting down VM
09-18 17:09:03.625: W/dalvikvm(8345): threadid=1: thread exiting with uncaught     exception (group=0x4001e560)
09-18 17:09:03.625: E/AndroidRuntime(8345): FATAL EXCEPTION: main
09-18 17:09:03.625: E/AndroidRuntime(8345): java.lang.RuntimeException: Unable to start     activity ComponentInfo{com.bben.physics/com.bben.physics.Acceleration}:     java.lang.NumberFormatException: 
09-18 17:09:03.625: E/AndroidRuntime(8345):     at     android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1713)
09-18 17:09:03.625: E/AndroidRuntime(8345):     at     android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1738)
09-18 17:09:03.625: E/AndroidRuntime(8345):     at     android.app.ActivityThread.access$1500(ActivityThread.java:124)
09-18 17:09:03.625: E/AndroidRuntime(8345):     at     android.app.ActivityThread$H.handleMessage(ActivityThread.java:980)
09-18 17:09:03.625: E/AndroidRuntime(8345):     at     android.os.Handler.dispatchMessage(Handler.java:99)
09-18 17:09:03.625: E/AndroidRuntime(8345):     at     android.os.Looper.loop(Looper.java:130)
09-18 17:09:03.625: E/AndroidRuntime(8345):     at     android.app.ActivityThread.main(ActivityThread.java:3906)
09-18 17:09:03.625: E/AndroidRuntime(8345):     at java.lang.reflect.Method.invokeNative(Native Method)
09-18 17:09:03.625: E/AndroidRuntime(8345):     at java.lang.reflect.Method.invoke(Method.java:507)
09-18 17:09:03.625: E/AndroidRuntime(8345):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:840)
09-18 17:09:03.625: E/AndroidRuntime(8345):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:598)
09-18 17:09:03.625: E/AndroidRuntime(8345):     at dalvik.system.NativeStart.main(Native Method)
09-18 17:09:03.625: E/AndroidRuntime(8345): Caused by: java.lang.NumberFormatException: 
09-18 17:09:03.625: E/AndroidRuntime(8345):     at org.apache.harmony.luni.util.FloatingPointParser.parseDouble(FloatingPointParser.java:267)
09-18 17:09:03.625: E/AndroidRuntime(8345):     at     java.lang.Double.parseDouble(Double.java:318)
09-18 17:09:03.625: E/AndroidRuntime(8345):     at     com.bben.physics.Acceleration.initialize(Acceleration.java:43)
09-18 17:09:03.625: E/AndroidRuntime(8345):     at com.bben.physics.Acceleration.onCreate(Acceleration.java:23)
09-18 17:09:03.625: E/AndroidRuntime(8345):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
09-18 17:09:03.625: E/AndroidRuntime(8345):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1677)
09-18 17:09:03.625: E/AndroidRuntime(8345):     ... 11 more
09-18 17:09:06.781: I/Process(8345): Sending signal. PID: 8345 SIG: 9
4

2 回答 2

0
only this code should be placed in onCreate()

    etV0 = (EditText) findViewById(R.id.etV0);
    etV1 = (EditText) findViewById(R.id.etV1);
    etT0 = (EditText) findViewById(R.id.etT0);
    etT1 = (EditText) findViewById(R.id.etT1);
    tvAccResult = (TextView) findViewById(R.id.tvAccResult);
    accC = (Button) findViewById(R.id.accC);


and this code should be present in the block of event fired (like when button ***accC*** is clicked):


    CetV0 = (Double.parseDouble(etV0.getText().toString()));
    CetV1 = (Double.parseDouble(etV1.getText().toString()));
    CetT0 = (Double.parseDouble(etT0.getText().toString()));
    CetT1 = (Double.parseDouble(etT1.getText().toString()));

    CtvAccResult = (double) ((CetV0 - CetV1) / (CetT0 - CetT1));

> so the final result will be  :

    public class Acceleration extends Activity {

    EditText etV0, etV1, etT0, etT1
    TextView tvAccResult;
    Button accC;
    double CetV0, CetV1, CetT0, CetT1, CtvAccResult;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_acceleration);

    initialize();

    accC.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

    CetV0 = (Double.parseDouble(etV0.getText().toString()));
    CetV1 = (Double.parseDouble(etV1.getText().toString()));
    CetT0 = (Double.parseDouble(etT0.getText().toString()));`enter code here`
    CetT1 = (Double.parseDouble(etT1.getText().toString()));

    CtvAccResult = (double) ((CetV0 - CetV1) / (CetT0 - CetT1));
            tvAccResult.setText(Double.toString(CtvAccResult));
        }
    });

}

    private void initialize() {
        etV0 = (EditText) findViewById(R.id.etV0);
        etV1 = (EditText) findViewById(R.id.etV1);
        etT0 = (EditText) findViewById(R.id.etT0);
        etT1 = (EditText) findViewById(R.id.etT1);
        tvAccResult = (TextView) findViewById(R.id.tvAccResult);
        accC = (Button) findViewById(R.id.accC);

    }



 @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.acceleration, menu);
        return true;
    }

    }
于 2013-09-18T15:14:59.870 回答
0

移动下面的内部按钮 onClick。也使用 try catch

try
{
CetV0 = (Double.parseDouble(etV0.getText().toString()));
CetV1 = (Double.parseDouble(etV1.getText().toString()));
CetT0 = (Double.parseDouble(etT0.getText().toString()));
CetT1 = (Double.parseDouble(etT1.getText().toString()));

CtvAccResult = (double) ((CetV0 - CetV1) / (CetT0 - CetT1));
}catch(NumberFormatException e)
{
      e.printStackTrace();
}    
于 2013-09-18T15:19:33.067 回答