应用程序在启动时崩溃 - 无法实例化活动ComponentInfo {

时间:2015-07-28 01:26:55

标签: java android xml

我是Android开发和java编程的新手。我一直在关注Udacity的课程没有问题,所以我决定开始尝试自己创建我的第一个应用程序。这是一个小费计算器。当我尝试在模拟器和Galaxy S6中运行它时,我的应用程序永远不会打开,它只是立即崩溃。我试图查看日志并搜索StackOverflow和其他站点上的过去的线程,但到目前为止还没有解决方案,我绝对迷失了。谁能帮帮我吗。我只编写了对应于到目前为止显示10%提示的按钮的java,并在测试时遇到此错误。我在下面附加了我的MainActivity.java,activity_main.xml和logcat。我还添加了我的AndroidManifest.xml,因为这似乎是大多数其他人遇到类似问题的地方,但正如我所说,我无法获得任何过去的解决方案。任何帮助将非常感谢!谢谢!

activity_main.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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity">

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Please enter the total amount of your bill below."
    android:textSize="16sp" />

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="8dp"
    android:layout_marginTop="8dp"
    android:orientation="horizontal">

    <TextView
        android:id="@+id/check_total_label"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Check Total: "
        android:textSize="24sp" />

    <EditText
        android:id="@+id/check_total"
        android:layout_width="208dp"
        android:layout_height="wrap_content"
        android:inputType="numberDecimal"/>

</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="8dp"
    android:layout_marginTop="8dp"
    android:orientation="vertical">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:text="Choose a percentage below, or enter a custom percentage to calculate your tip."
        android:textSize="16sp" />

    <Button
        android:id="@+id/btn_ten"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:onClick="calcTenPercent"
        android:text="10%"
        android:textSize="24sp" />

    <Button
        android:id="@+id/btn_fifteen"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:text="15%"
        android:textSize="24sp" />

    <Button
        android:id="@+id/btn_twenty"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:text="20%"
        android:textSize="24sp" />

</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <TextView
        android:id="@+id/custom_tip_label"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Custom Percentage:"
        android:textSize="24sp" />

    <EditText
        android:id="@+id/custom_tip"
        android:layout_width="128dp"
        android:layout_height="wrap_content"
        android:inputType="numberDecimal"/>

</LinearLayout>

<Button
    android:id="@+id/calc_custom_tip"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Calculate Custom Tip"
    android:textSize="18sp" />

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:paddingBottom="24dp"
    android:paddingTop="24dp">

    <TextView
        android:id="@+id/final_label"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:gravity="center_vertical"
        android:text="Tip Amount: "
        android:textSize="32sp" />

    <TextView
        android:id="@+id/final_tip"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="16dp"
        android:text="$0.00"
        android:textSize="32sp" />

</LinearLayout>
</LinearLayout>

MainActivity.java

package com.example.android.tipcalculator;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import java.text.NumberFormat;

public class MainActivity extends AppCompatActivity {


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

EditText checkTotal = (EditText) findViewById(R.id.check_total);
String checkTotalValue = checkTotal.getText().toString();

double totalCheck = Double.parseDouble(checkTotalValue);

public void calcTenPercent(View view) {

    double tenPercent = 0.10;

    displayTip(totalCheck * tenPercent);

}

private void displayTip(double number) {

    TextView finalTip = (TextView) findViewById(R.id.final_tip);
    finalTip.setText(NumberFormat.getCurrencyInstance().format(number));
}
}

的AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.android.tipcalculator" >

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.example.android.tipcalculator.MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

</manifest>

logcat的

07-27 20:59:13.331  23270-23270/com.example.android.tipcalculator E/Zygote﹕ MountEmulatedStorage()
    07-27 20:59:13.331  23270-23270/com.example.android.tipcalculator E/Zygote﹕ v2
    07-27 20:59:13.331  23270-23270/com.example.android.tipcalculator I/libpersona﹕ KNOX_SDCARD checking this for 10243
    07-27 20:59:13.331  23270-23270/com.example.android.tipcalculator I/libpersona﹕ KNOX_SDCARD not a persona
    07-27 20:59:13.331  23270-23270/com.example.android.tipcalculator I/SELinux﹕ Function: selinux_compare_spd_ram , priority [2] , priority version is VE=SEPF_SAMSUNG-SM-G920A_5.0.2_0011
    07-27 20:59:13.341  23270-23270/com.example.android.tipcalculator E/SELinux﹕ [DEBUG] get_category: variable seinfo: default sensitivity: NULL, cateogry: NULL
    07-27 20:59:13.341  23270-23270/com.example.android.tipcalculator I/art﹕ Late-enabling -Xcheck:jni
    07-27 20:59:13.351  23270-23270/com.example.android.tipcalculator D/TimaKeyStoreProvider﹕ TimaSignature is unavailable
    07-27 20:59:13.351  23270-23270/com.example.android.tipcalculator D/ActivityThread﹕ Added TimaKeyStore provider
    07-27 20:59:13.381  23270-23270/com.example.android.tipcalculator I/InjectionManager﹕ Inside getClassLibPath + mLibMap{0=, 1=}
    07-27 20:59:13.381  23270-23270/com.example.android.tipcalculator I/InjectionManager﹕ Inside getClassLibPath caller
    07-27 20:59:13.391  23270-23270/com.example.android.tipcalculator D/InjectionManager﹕ InjectionManager
    07-27 20:59:13.391  23270-23270/com.example.android.tipcalculator D/InjectionManager﹕ fillFeatureStoreMap com.example.android.tipcalculator
    07-27 20:59:13.391  23270-23270/com.example.android.tipcalculator I/InjectionManager﹕ Constructor com.example.android.tipcalculator, Feature store :{}
    07-27 20:59:13.391  23270-23270/com.example.android.tipcalculator I/InjectionManager﹕ featureStore :{}
    07-27 20:59:13.411  23270-23270/com.example.android.tipcalculator D/AndroidRuntime﹕ Shutting down VM
    07-27 20:59:13.411  23270-23270/com.example.android.tipcalculator E/AndroidRuntime﹕ FATAL EXCEPTION: main
        Process: com.example.android.tipcalculator, PID: 23270
        java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.android.tipcalculator/com.example.android.tipcalculator.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.Window.findViewById(int)' on a null object reference
                at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2665)
                at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2879)
                at android.app.ActivityThread.access$900(ActivityThread.java:182)
                at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1475)
                at android.os.Handler.dispatchMessage(Handler.java:102)
                at android.os.Looper.loop(Looper.java:145)
                at android.app.ActivityThread.main(ActivityThread.java:6141)
                at java.lang.reflect.Method.invoke(Native Method)
                at java.lang.reflect.Method.invoke(Method.java:372)
                at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
         Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.Window.findViewById(int)' on a null object reference
                at android.app.Activity.findViewById(Activity.java:2197)
                at com.example.android.tipcalculator.MainActivity.<init>(MainActivity.java:20)
                at java.lang.reflect.Constructor.newInstance(Native Method)
                at java.lang.Class.newInstance(Class.java:1650)
                at android.app.Instrumentation.newActivity(Instrumentation.java:1079)
                at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2655)
                at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2879)
                at android.app.ActivityThread.access$900(ActivityThread.java:182)
                at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1475)
                at android.os.Handler.dispatchMessage(Handler.java:102)
                at android.os.Looper.loop(Looper.java:145)
                at android.app.ActivityThread.main(ActivityThread.java:6141)
                at java.lang.reflect.Method.invoke(Native Method)`enter code here`
                at java.lang.reflect.Method.invoke(Method.java:372)
                at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)

更新的LOGCAT

应用程序现在运行,但似乎在onClick期间终止。我将努力解决这个问题,并在需要时提出一个单独的问题。

07-27 23:31:04.481  10846-10846/com.example.android.tipcalculator E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.android.tipcalculator, PID: 10846
java.lang.IllegalStateException: Could not execute method of the activity
        at android.view.View$1.onClick(View.java:4273)
        at android.view.View.performClick(View.java:5217)
        at android.view.View$PerformClick.run(View.java:20983)
        at android.os.Handler.handleCallback(Handler.java:739)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:145)
        at android.app.ActivityThread.main(ActivityThread.java:6141)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
 Caused by: java.lang.reflect.InvocationTargetException
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at android.view.View$1.onClick(View.java:4268)
        at android.view.View.performClick(View.java:5217)
        at android.view.View$PerformClick.run(View.java:20983)
        at android.os.Handler.handleCallback(Handler.java:739)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:145)
        at android.app.ActivityThread.main(ActivityThread.java:6141)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
 Caused by: java.lang.NumberFormatException: Invalid double: ""
        at java.lang.StringToReal.invalidReal(StringToReal.java:63)
        at java.lang.StringToReal.parseDouble(StringToReal.java:267)
        at java.lang.Double.parseDouble(Double.java:301)
        at com.example.android.tipcalculator.MainActivity.calcTenPercent(MainActivity.java:30)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at android.view.View$1.onClick(View.java:4268)
        at android.view.View.performClick(View.java:5217)
        at android.view.View$PerformClick.run(View.java:20983)
        at android.os.Handler.handleCallback(Handler.java:739)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:145)
        at android.app.ActivityThread.main(ActivityThread.java:6141)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at    com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)

2 个答案:

答案 0 :(得分:2)

问题在于:

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

EditText checkTotal = (EditText) findViewById(R.id.check_total);
String checkTotalValue = checkTotal.getText().toString();

不要在onCreate之前调用的全局变量初始化中调用findViewById,因此Activity仍然没有内容视图(您将在onCreate上设置)。
因此,您可以将代码更改为:

EditText checkTotal;
String checkTotalValue;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    checkTotal = (EditText) findViewById(R.id.check_total);
    checkTotalValue = checkTotal.getText().toString();
}

答案 1 :(得分:0)

Caused by: java.lang.NumberFormatException: Invalid double: ""

好像你正在传递一个空的双...可能你的totalCheck值没有从文本字段中正确解析?

尝试查看checkTotal初始化时checkTotalValue包含的内容,然后查看您传入displayTip的内容