【发布时间】:2017-01-05 10:32:36
【问题描述】:
我的活动中有一个编辑文本,并希望请求关注它。但是,它总是在启动 Activity 时崩溃。
Java 代码:
foodText = (EditText) findViewById(R.id.foodName);
prepText = (EditText) findViewById(R.id.prepText);
foodText.requestFocus();
Activity.xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_add_food"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.rcadit.foodgenie.AddFood">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
style="@style/AppTheme"
>
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="@color/colorAccent"
android:transitionName="addFood">
<EditText
android:id="@+id/foodName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="Food name"
android:textSize="32sp"
android:inputType="textCapWords"
android:textColor="@color/colorIcons"
android:layout_marginLeft="64dp"
android:layout_marginRight="64dp"
android:layout_marginBottom="32dp"
android:layout_marginTop="32dp"
android:textColorHint="@color/colorIcons"/>
</LinearLayout>
</LinearLayout>
</ScrollView>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingLeft="@dimen/activity_horizontal_margin">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="16dp"
android:textSize="16sp"
android:text="Hunger Level" />
<Spinner
android:id="@+id/hunger_spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="16dp"
>
</Spinner>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="16dp"
android:textSize="16sp"
android:text="Cuisine" />
<Spinner
android:id="@+id/cuisine_spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="16dp"
>
</Spinner>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="16dp"
android:textSize="16sp"
android:text="Preparation Time" />
<EditText
android:id="@+id/prepText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16sp"
android:padding="16dp"
android:hint="Enter Preparation Time"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="16dp"
android:textSize="16sp"
android:text="Course" />
<Spinner
android:id="@+id/course_spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="16dp"
>
</Spinner>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Salty"
android:padding="16dp"
android:textSize="16sp"
/>
<TextView
android:id="@+id/howSalty"
android:hint="Salty"
android:textSize="12sp"
android:paddingBottom="8dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
/>
<SeekBar
android:id="@+id/salty"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:thumbTint="@color/colorAccent"
android:progressTint="@color/colorAccent"
android:max="2"
android:progress="1"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Sweet"
android:padding="16dp"
android:textSize="16sp"/>
<TextView
android:id="@+id/howSweet"
android:hint="Sweet"
android:textSize="12sp"
android:paddingBottom="8dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"/>
<SeekBar
android:thumbTint="@color/colorAccent"
android:progressTint="@color/colorAccent"
android:id="@+id/sweet"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:max="2"
android:progress="1"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Spicy"
android:padding="16dp"
android:textSize="16sp"/>
<TextView
android:id="@+id/howSpicy"
android:hint="Spicy"
android:textSize="12sp"
android:paddingBottom="8dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
/>
<SeekBar
android:thumbTint="@color/colorAccent"
android:progressTint="@color/colorAccent"
android:id="@+id/spicy"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:max="2"
android:progress="1"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="16dp"
android:text="Special Requirements"
android:textSize="16sp" />
<GridLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:columnCount="2"
android:rowCount="3">
<CheckBox
android:layout_width="wrap_content"
android:id="@+id/dairyFree"
android:buttonTint="@color/colorAccent"
android:layout_height="wrap_content"
android:text="Dairy Free"
android:textSize="16sp"
android:onClick="onCheckboxClicked"/>
<CheckBox
android:layout_width="wrap_content"
android:id="@+id/nutFree"
android:buttonTint="@color/colorAccent"
android:layout_height="wrap_content"
android:text="Nut Free"
android:textSize="16sp"
android:onClick="onCheckboxClicked"/>
<CheckBox
android:id="@+id/eggFree"
android:layout_width="wrap_content"
android:buttonTint="@color/colorAccent"
android:layout_height="wrap_content"
android:text="Egg Free"
android:textSize="16sp"
android:onClick="onCheckboxClicked"
/>
<CheckBox
android:id="@+id/vegetarian"
android:layout_width="wrap_content"
android:buttonTint="@color/colorAccent"
android:layout_height="wrap_content"
android:text="Vegetarian"
android:textSize="16sp"
android:onClick="onCheckboxClicked"/>
</GridLayout>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:onClick="add"
android:text="Add" />
</LinearLayout>
</android.support.constraint.ConstraintLayout>
我查看了其他问题,但我不明白为什么我的代码不起作用,因为 ID 是正确的,我找不到代码中的错误。
日志文件:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.rcadit.foodgenie, PID: 3221
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.rcadit.foodgenie/com.example.rcadit.foodgenie.AddFood}: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.widget.EditText.requestFocus()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.widget.EditText.requestFocus()' on a null object reference
at com.example.rcadit.foodgenie.AddFood.onCreate(AddFood.java:60)
at android.app.Activity.performCreate(Activity.java:6237)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
I/Process: Sending signal. PID: 3221 SIG: 9
Application terminated.
怎么了?
【问题讨论】:
-
在调用方法的地方发布完整代码..
-
确实我们需要知道这三行在哪里 "foodText = (EditText) findViewById(R.id.foodName); prepText = (EditText) findViewById(R.id.prepText); foodText. requestFocus();"
-
尝试将其写入布局本身。
-
另外,最好不要在 android 中的 Id 和 res 中使用任何大写字母...这往往会导致问题
-
在 setcontentview 中检查正确的 xml 文件
标签: android nullpointerexception android-edittext