【问题标题】:Android Application Project unknow error 2Android应用项目未知错误2
【发布时间】:2013-09-19 00:43:50
【问题描述】:

我昨天已经在这里发布了关于这个应用程序的信息,这个活动很像我发布的那个,但我仍然有一个错误,我不知道如何解决它。 问题是当我打开应用程序并从菜单中选择 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

【问题讨论】:

    标签: java android sdk developer-tools


    【解决方案1】:

    这是你的问题:

    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

    【讨论】:

    • 这取决于问题是什么。你能确认VeloCalcB == null吗?对我来说,这似乎是可能的原因,但你是编译器的人,因此可以确认这一点。
    • 我是初学者,所以不太了解,如何确认?
    • “如有疑问,请打印更多内容”的原则在这里很有帮助。调用initialize() 后,执行Log.d("Velocity", VeloCalcB == null ? "VeloCalcB is null" : "VeloCalcB is not null"); 之类的操作,然后查看日志输出中的内容。
    • 我放了一个 if 语句来检查它是否为空。它是空的
    • 嗯,这证实了findViewById() 正在返回null。根据您显示的内容,它实际上应该返回对您的按钮的引用。我要做的第一件事是清理您的项目并重建所有内容,以防您的布局资源不同步或其他原因。
    【解决方案2】:

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

    setContentView(R.layout.activity_acceleration);
    

    您应该将activity_acceleration 替换为activity_velocity

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-28
      • 2014-06-10
      相关资源
      最近更新 更多