java - 试图制作一个自动完成程序,但它不起作用

标签 java android eclipse

我正在尝试制作一个自动完成一年中几个月的程序。每次运行它似乎都会崩溃。我尝试尽可能多地检查和纠正,但我是 Android 新手,而且我在编程方面有 2 个月的差距,所以我无法弄清楚我做错了什么。先谢谢您的帮助。!现在任何帮助都会很棒。!

   package com.book.listviewtest;

   //import com.newboston.thefirst.Menu;

   import android.app.Activity;
   import android.graphics.Color;
   import android.os.Bundle;
   import android.view.View;
   import android.view.View.OnClickListener;
   import android.widget.ArrayAdapter;
   import android.widget.AutoCompleteTextView;
   import android.widget.Button;

   public class AutoComplete extends Activity {

    ArrayAdapter<String> monthArray= new ArrayAdapter<String> (this, android.R.layout.simple_list_item_1,Months);
final AutoCompleteTextView textView= (AutoCompleteTextView)findViewById(R.id.testAC);
Button changeB = (Button) findViewById(R.id.btextC);
Button changeB2= (Button) findViewById(R.id.bAC);

public void onCreate (Bundle onSavedInstantState){
    super.onCreate(onSavedInstantState);
    setContentView(R.layout.autocomplete);
    textView.setAdapter(monthArray);
    changeB.setOnClickListener(new Button.OnClickListener(){
        public void onClick(View v){
            if (textView.getHeight()==100){
                textView.setHeight(30);
            } else {
                textView.setHeight(100);
            }   
        }
    });
    changeB2.setOnClickListener(new Button.OnClickListener(){

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            textView.setTextColor(Color.RED);   
        }

    });
}
static final String[] Months= new String [] {"January","February","March","April"};


}

我收到以下错误:

  07-14 19:32:34.504: E/AndroidRuntime(1098): FATAL EXCEPTION: main
  07-14 19:32:34.504: E/AndroidRuntime(1098): Process: com.book.listviewtest, PID: 1098
  07-14 19:32:34.504: E/AndroidRuntime(1098): java.lang.RuntimeException: Unable to     instantiate activityC omponentInfo{com.book.listviewtest/com.book.listviewtest.AutoComplete}: java.lang.IllegalStateException: System services not available to Activities before onCreate()
  07-14 19:32:34.504: E/AndroidRuntime(1098):   at  android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2121)
  07-14 19:32:34.504: E/AndroidRuntime(1098):   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
  07-14 19:32:34.504: E/AndroidRuntime(1098):   at android.app.ActivityThread.access$800(ActivityThread.java:135)
  07-14 19:32:34.504: E/AndroidRuntime(1098):   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
  07-14 19:32:34.504: E/AndroidRuntime(1098):   at android.os.Handler.dispatchMessage(Handler.java:102)
  07-14 19:32:34.504: E/AndroidRuntime(1098):   at android.os.Looper.loop(Looper.java:136)
  07-14 19:32:34.504: E/AndroidRuntime(1098):   at android.app.ActivityThread.main(ActivityThread.java:5017)
  07-14 19:32:34.504: E/AndroidRuntime(1098):   at java.lang.reflect.Method.invokeNative(Native Method)
  07-14 19:32:34.504: E/AndroidRuntime(1098):   at java.lang.reflect.Method.invoke(Method.java:515)
  07-14 19:32:34.504: E/AndroidRuntime(1098):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
  07-14 19:32:34.504: E/AndroidRuntime(1098):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
  07-14 19:32:34.504: E/AndroidRuntime(1098):   at dalvik.system.NativeStart.main(Native Method)
  07-14 19:32:34.504: E/AndroidRuntime(1098): Caused by: java.lang.IllegalStateException: System services not available to Activities before onCreate()
  07-14 19:32:34.504: E/AndroidRuntime(1098):   at android.app.Activity.getSystemService(Activity.java:4532)
  07-14 19:32:34.504: E/AndroidRuntime(1098):   at android.widget.ArrayAdapter.init(ArrayAdapter.java:310)
  07-14 19:32:34.504: E/AndroidRuntime(1098):   at android.widget.ArrayAdapter.<init>(ArrayAdapter.java:128)
  07-14 19:32:34.504: E/AndroidRuntime(1098):   at com.book.listviewtest.AutoComplete.<init>(AutoComplete.java:16)
  07-14 19:32:34.504: E/AndroidRuntime(1098):   at java.lang.Class.newInstanceImpl(Native Method)
  07-14 19:32:34.504: E/AndroidRuntime(1098):   at java.lang.Class.newInstance(Class.java:1208)
  07-14 19:32:34.504: E/AndroidRuntime(1098):   at android.app.Instrumentation.newActivity(Instrumentation.java:1061)
  07-14 19:32:34.504: E/AndroidRuntime(1098):   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2112)
  07-14 19:32:34.504: E/AndroidRuntime(1098):   ... 11 more
  07-14 19:32:36.854: I/Process(1098): Sending signal. PID: 1098 SIG: 9

最佳答案

ArrayAdapter<String> monthArray= new ArrayAdapter<String> (this, android.R.layout.simple_list_item_1,Months);
final AutoCompleteTextView textView= (AutoCompleteTextView)findViewById(R.id.testAC);
Button changeB = (Button) findViewById(R.id.btextC);
Button changeB2= (Button) findViewById(R.id.bAC);

将这些初始化移至 onCreate() 以及 setContentView() 之后的 findViewById() 调用。

onCreate() 之前您无法真正运行任何 Activity 代码。

关于java - 试图制作一个自动完成程序,但它不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24764149/

相关文章:

java - 为什么在 Java 中不能有 "List<List<String>>"?

java - 将通用 RecyclerView 适配器转换为 Kotlin

java - Android : java. lang.String无法转换为JSONArray

java - 在 Android textSwitcher 中使用上标

java - 线程 "main"java.lang.UnsatisfiedLinkError 中的异常

java - 如何在 Selenium 中找到 Telerik 控件?

java - com.google.firebase.database.DatabaseException : Found two getters or fields with conflicting case sensitivity for property: user_name

java - 如何将 eclipse 与 weblogic 服务器一起使用

java - 为什么我不断收到 'SVN: Working Copy XXXX locked; try performing ' cleanup'?

java - 使用 Java 连接到 MySQL