当我调用EditText

时间:2017-11-25 22:55:15

标签: android android-layout crash

当我调用EDITTEXT时应用程序崩溃。

布局

<EditText
    android:id="@+id/T_Price"
    android:layout_width="200dp"
    android:layout_height="wrap_content"
    android:layout_below="@+id/B_LessMoney"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="65dp"
    android:text="@string/Price"
    android:visibility="visible" />

主要

    EditText mPrice = (EditText) findViewById(R.id.T_Price);

这是报告

  

E / AndroidRuntime:致命异常:主要                     流程:budgetreport.com.budgetreport,PID:2791                     java.lang.RuntimeException:无法实例化活动ComponentInfo {budgetreport.com.budgetreport / budgetreport.com.budgetreport.Report}:java.lang.NullPointerException:尝试调用虚拟方法'android.view.Window $ Callback android.view。关于空对象引用的Window.getCallback()'                         在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2488)                         在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2665)                         在android.app.ActivityThread.-wrap11(ActivityThread.java)                         在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1499)                         在android.os.Handler.dispatchMessage(Handler.java:111)                         在android.os.Looper.loop(Looper.java:207)                         在android.app.ActivityThread.main(ActivityThread.java:5765)                         at java.lang.reflect.Method.invoke(Native Method)                         在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:789)                         在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:679)                      引起:java.lang.NullPointerException:尝试在空对象引用上调用虚方法'android.view.Window $ Callback android.view.Window.getCallback()'                         在android.support.v7.app.AppCompatDelegateImplBase。(AppCompatDelegateImplBase.java:117)                         在android.support.v7.app.AppCompatDelegateImplV9。(AppCompatDelegateImplV9.java:149)                         在android.support.v7.app.AppCompatDelegateImplV11。(AppCompatDelegateImplV11.java:29)                         在android.support.v7.app.AppCompatDelegateImplV14。(AppCompatDelegateImplV14.java:54)                         在android.support.v7.app.AppCompatDelegateImplV23。(AppCompatDelegateImplV23.java:31)                         在android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:200)                         在android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:183)                         在android.support.v7.app.AppCompatActivity.getDelegate(AppCompatActivity.java:519)                         在android.support.v7.app.AppCompatActivity.findViewById(AppCompatActivity.java:190)                         在budgetreport.com.budgetreport.Report。(Report.java:21)                         at java.lang.Class.newInstance(Native Method)                         在android.app.Instrumentation.newActivity(Instrumentation.java:1072)                         在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2478)                         在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2665)                         在android.app.ActivityThread.-wrap11(ActivityThread.java)                         在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1499)                         在android.os.Handler.dispatchMessage(Handler.java:111)                         在android.os.Looper.loop(Looper.java:207)                         在android.app.ActivityThread.main(ActivityThread.java:5765)                         at java.lang.reflect.Method.invoke(Native Method)                         在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:789)                         在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:679)   I /处理:发送信号。 PID:2791 SIG:9   申请已终止。

1 个答案:

答案 0 :(得分:0)

你在Activity的findViewById方法之前调用onCreate(在构造函数中?)。

你应该做的是在Activity的onCreate中初始化它:

Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.your_layout);
    mPrice = (EditText) findViewById(R.id.T_Price);
}