[英]Android Application Project unknow error 2
I have already posted here yesterday about this app, this activity is very like the one i posted, but still i have an error, and i don't know how to solve it. 我昨天已经在这里发布了有关此应用程序的信息,此活动与我发布的活动非常相似,但仍然有一个错误,而且我不知道如何解决。 The problem is that when i open the app and choose the Velocity from the menu, it's forced close. 问题是,当我打开应用程序并从菜单中选择Velocity时,它被强制关闭。
Velocity.java: 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: 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>
The Logcat: 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
This is your problem: 这是你的问题:
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)
Line 25 of your source appears to be: 您的来源的第25行显示为:
VeloCalcB.setOnClickListener(new View.OnClickListener() {
It appears that VeloCalcB
is likely null
. 看来VeloCalcB
可能为null
。 I see that your initialize()
method attempts to set VeloCalcB
to something, but you will want to make sure that's not returning null
. 我看到您的initialize()
方法尝试将VeloCalcB
设置为某种值,但是您将要确保它不会返回null
。
Indeed, as Greg has said in his answer, findViewById returns null. 确实,正如Greg在回答中所说的那样,findViewById返回null。 This might have happend because the XML you posted here is not the one you're referring to in your code: 这可能是因为您在此处发布的XML不是您在代码中所指的XML:
setContentView(R.layout.activity_acceleration);
You should probably replace activity_acceleration
with activity_velocity
. 您可能应该将activity_acceleration
替换为activity_velocity
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.