相对布局中的AutoCompleteTextView[重复]

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (1)
  • 关注 (0)
  • 查看 (73)

这个问题已经有了一个答案:

  • 什么是NullPointerException,以及如何修复它?12份答复

我想实现一个自动完成编辑文本在我的申请中。

我准备好了activity_add_customer.xml对于我的AutoCompleteTextView,如下所示

<AutoCompleteTextView
            android:id="@+id/monthAuto"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="20dp"
            android:hint="Select a Month"
            android:completionThreshold="1"
            android:completionHint="Select a Month"
            android:textAlignment="center"/>

和我的java类

public class AddCustomer extends AppCompatActivity {

private static final String[] MONTHS = new String[] {
        "January", "February", "March", "April", "May","June","July","August","September","October","November","December"
};

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

    AutoCompleteTextView textView = findViewById(R.id.monthAuto);
    ArrayAdapter<String> adapter= new ArrayAdapter<String>(this,android.R.layout.simple_expandable_list_item_1,MONTHS);
    textView.setAdapter(adapter);
}
}

逻辑猫

06-02 21:35:40.580 32207-32207/com.artistdipin.business E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.artistdipin.business, PID: 32207
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.artistdipin.business/com.artistdipin.business.AddCustomer}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.AutoCompleteTextView.setAdapter(android.widget.ListAdapter)' on a null object reference
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2762)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2823)
    at android.app.ActivityThread.-wrap12(ActivityThread.java)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1545)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:154)
    at android.app.ActivityThread.main(ActivityThread.java:6349)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:893)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:783)
 Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.AutoCompleteTextView.setAdapter(android.widget.ListAdapter)' on a null object reference
    at com.artistdipin.business.AddCustomer.onCreate(AddCustomer.java:22)
    at android.app.Activity.performCreate(Activity.java:6772)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2715)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2823) 
    at android.app.ActivityThread.-wrap12(ActivityThread.java) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1545) 
    at android.os.Handler.dispatchMessage(Handler.java:102) 
    at android.os.Looper.loop(Looper.java:154) 
    at android.app.ActivityThread.main(ActivityThread.java:6349) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:893) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:783) 
提问于
用户回答回答于

更改这一行:

setContentView(R.layout.activity_add_customer);

对此

setContentView(R.layout.main);

还尝试将创建适配器实例的行更改为:

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, MONTHS);

扫码关注云+社区

领取腾讯云代金券