[英]requestFocus() giving null pointer exception
I have an edit text in my activity and want to request focus on it. 我的活动中有一个编辑文本,并希望请求关注它。 However, it always crashes upon launching the activity.
但是,它在启动活动时总是崩溃。
Java code: Java代码:
foodText = (EditText) findViewById(R.id.foodName);
prepText = (EditText) findViewById(R.id.prepText);
foodText.requestFocus();
Activity.xml: 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>
I have looked at other questions but I can't see why my code doesn't work as the ID's are correct and I can't find the fault in the code. 我已经查看了其他问题,但我不明白为什么我的代码不能正常工作,因为ID是正确的,我无法在代码中找到错误。
Log file: 日志文件:
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.
What's wrong? 怎么了?
So I was able to reproduce your error by not calling setContentView() before using your code. 所以我能够在使用你的代码之前不调用setContentView()来重现你的错误。 So the correct way to do it is to do:
所以正确的方法是:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.your_activity);
foodText = (EditText) findViewById(R.id.foodName);
prepText = (EditText) findViewById(R.id.prepText);
foodText.requestFocus();
}
your_activity
is basically the name of your xml
file without the xml
extension. your_activity
基本上是没有xml
扩展名的xml
文件的名称。 For more documentation on Views you can check this link or this link ifwhat you're doing is creating a second Activity. 有关视图的更多文档,您可以查看此链接或此链接,如果您正在创建第二个活动。 (both refer to the official documentation and give examples and explanations)
(均参考官方文档并提供示例和解释)
Also I suppose that you would get the same error if you don't use the right name of file for the setContentView()
method. 另外我想如果你没有为
setContentView()
方法使用正确的文件名,你会得到同样的错误。
I have tried at my end its working fine with exact your params. 我已经尝试过我的工作正常你的准确。
MainActivity: 主要活动:
public class MainActivity extends AppCompatActivity {
Context context;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
context = this;
EditText number = (EditText) findViewById(R.id.foodName);
number.requestFocus();
}
}
activity_main.xml: activity_main.xml中:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/base"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:background="@color/white"
android:orientation="horizontal"
android:weightSum="10">
<EditText
android:id="@+id/foodName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="32dp"
android:layout_marginLeft="64dp"
android:layout_marginRight="64dp"
android:layout_marginTop="32dp"
android:hint="Food name"
android:inputType="textCapWords"
android:textColor="@color/colorPrimaryDark"
android:textColorHint="@color/colorPrimaryDark"
android:textSize="32sp" />
</RelativeLayout>
我猜你在setContentView()
方法之前调用了findViewById()
setContentView()
方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.