相对布局中的AutoCompleteTextView

时间:2018-06-02 16:00:19

标签: android arraylist nullpointerexception textview autocompletetextview

我想在我的应用程序中实现自动填充 EditText

我为我的AutoCompleteTextView准备了activity_add_customer.xml,如下所示

<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);
}
}

我删除了代码并进行了检查。那时它完美的工作。它一直在崩溃。我怎么能解决这个问题。

logcat的

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) 

1 个答案:

答案 0 :(得分:0)

更改此行:

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);