1

我昨天已经在这里发布了这个应用程序,这个活动很像我发布的那个,但我仍然有一个错误,我不知道如何解决它。问题是当我打开应用程序并从菜单中选择 Velocity 时,它会被强制关闭。

速度.java:

package com.bben.physics;

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 Velocity extends Activity {

EditText X0, X1, T0, T1;
Button VeloB;
TextView VeloResult;
double dX, dT, velo;

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

    X0 = (EditText) findViewById(R.id.X0);
    X1 = (EditText) findViewById(R.id.X1);
    T0 = (EditText) findViewById(R.id.T0);
    T1 = (EditText) findViewById(R.id.T1);
    VeloB = (Button) findViewById(R.id.VeloB);
    VeloResult = (TextView) findViewById(R.id.VeloResult);

    if (VeloB == null) {
        System.out.println("VeloB = null");
    } else {
        VeloB.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                dX = (Double.parseDouble(X0.getText().toString()))
                        -  (Double.parseDouble(X1.getText().toString()));
                dT = (Double.parseDouble(T0.getText().toString()))
                        - (Double.parseDouble(T1.getText().toString()));
                velo = dX / dT;
                VeloResult.setText(Double.toString(velo));
            }
        });
    }
}

@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;
}

}

活动速度.xml:

<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:singleLine="true"
    android:text="@string/velocity"
    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/X0"
        android:layout_width="15dp"
        android:layout_height="wrap_content"
        android:layout_weight="30"
        android:digits=".-0123456789"
        android:ems="10"
        android:hint="x0"
        android:inputType="numberSigned"
        android:singleLine="true" />

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

    <EditText
        android:id="@+id/X1"
        android:layout_width="15dp"
        android:layout_height="wrap_content"
        android:layout_weight="30"
        android:digits=".-0123456789"
        android:ems="10"
        android:hint="x1"
        android:inputType="numberSigned"
        android:singleLine="true" />
</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:paddingBottom="20dp"
    android:paddingTop="10dp"
    android:weightSum="100" >

    <EditText
        android:id="@+id/T0"
        android:layout_width="15dp"
        android:layout_height="wrap_content"
        android:layout_weight="30"
        android:digits=".-0123456789"
        android:ems="10"
        android:hint="t0"
        android:inputType="numberSigned"
        android:singleLine="true" />

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

    <EditText
        android:id="@+id/T1"
        android:layout_width="15dp"
        android:layout_height="wrap_content"
        android:layout_weight="30"
        android:digits=".-0123456789"
        android:ems="10"
        android:hint="t1"
        android:inputType="numberSigned"
        android:singleLine="true" />
</LinearLayout>

<Button
    android:id="@+id/VeloCalcB"
    android:layout_width="wrap_content"
    android:layout_height="45dp"
    android:layout_gravity="center"
    android:text="חשב"
    android:textSize="20dp" />

<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/tvVeloResult"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="50"
        android:paddingLeft="20dp"
        android:text=""
        android:textSize="20dp" />
</LinearLayout>

</LinearLayout>

日志猫:

09-19 02:41:50.022: D/AndroidRuntime(26165): Shutting down VM
09-19 02:41:50.022: W/dalvikvm(26165): threadid=1: thread exiting with uncaught     exception (group=0x2aac8578)
09-19 02:41:50.022: E/AndroidRuntime(26165): FATAL EXCEPTION: main
09-19 02:41:50.022: E/AndroidRuntime(26165): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.bben.physics/com.bben.physics.Velocity}: java.lang.NullPointerException
09-19 02:41:50.022: E/AndroidRuntime(26165):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1659)
09-19 02:41:50.022: E/AndroidRuntime(26165):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1675)
09-19 02:41:50.022: E/AndroidRuntime(26165):    at android.app.ActivityThread.access$1500(ActivityThread.java:121)
09-19 02:41:50.022: E/AndroidRuntime(26165):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:943)
09-19 02:41:50.022: E/AndroidRuntime(26165):    at android.os.Handler.dispatchMessage(Handler.java:99)
09-19 02:41:50.022: E/AndroidRuntime(26165):    at android.os.Looper.loop(Looper.java:130)
09-19 02:41:50.022: E/AndroidRuntime(26165):    at android.app.ActivityThread.main(ActivityThread.java:3701)
09-19 02:41:50.022: E/AndroidRuntime(26165):    at java.lang.reflect.Method.invokeNative(Native Method)
09-19 02:41:50.022: E/AndroidRuntime(26165):    at java.lang.reflect.Method.invoke(Method.java:507)
09-19 02:41:50.022: E/AndroidRuntime(26165):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
09-19 02:41:50.022: E/AndroidRuntime(26165):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:624)
09-19 02:41:50.022: E/AndroidRuntime(26165):    at dalvik.system.NativeStart.main(Native Method)
09-19 02:41:50.022: E/AndroidRuntime(26165): Caused by: java.lang.NullPointerException
09-19 02:41:50.022: E/AndroidRuntime(26165):    at com.bben.physics.Velocity.onCreate(Velocity.java:25)
09-19 02:41:50.022: E/AndroidRuntime(26165):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
09-19 02:41:50.022: E/AndroidRuntime(26165):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1623)
09-19 02:41:50.022: E/AndroidRuntime(26165):    ... 11 more
4

2 回答 2

2

这是你的问题:

09-19 02:41:50.022: E/AndroidRuntime(26165): Caused by: java.lang.NullPointerException
09-19 02:41:50.022: E/AndroidRuntime(26165):    at com.bben.physics.Velocity.onCreate(Velocity.java:25)

您的来源的第 25 行似乎是:

VeloCalcB.setOnClickListener(new View.OnClickListener() {

看来VeloCalcB很有可能null。我看到你的initialize()方法试图设置VeloCalcB一些东西,但你会想确保它没有返回null

于 2013-09-19T00:46:43.250 回答
0

事实上,正如 Greg 在他的回答中所说,findViewById 返回 null。这可能是因为您在此处发布的 XML 不是您在代码中引用的 XML:

setContentView(R.layout.activity_acceleration);

您可能应该替换activity_accelerationactivity_velocity.

于 2013-09-22T10:43:37.943 回答