我昨天已经在这里发布了关于这个应用程序,这个活动非常像我发布的那个,但我仍然有一个错误,我不知道如何解决它。 问题是,当我打开应用程序并从菜单中选择Velocity时,它会被强制关闭。
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;
}
}
activity_velocity.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>
Logcat:
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
答案 0 :(得分: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
。
答案 1 :(得分:0)
实际上,正如Greg在他的回答中所说,findViewById返回null。这可能会发生,因为您在此处发布的XML不是您在代码中引用的XML:
setContentView(R.layout.activity_acceleration);
您应该将activity_acceleration
替换为activity_velocity
。