【问题标题】:Trying to make an auto complete program but it is not working试图制作一个自动完成程序,但它不工作
【发布时间】:2014-09-05 23:59:28
【问题描述】:

我正在尝试制作一年中几个月的自动完成程序。每次运行它似乎都会崩溃。我尝试尽可能多地检查和纠正,但我是 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

【问题讨论】:

    标签: java android eclipse


    【解决方案1】:
    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() 之前你不能真正运行任何活动代码。

    【讨论】:

    • 非常感谢..!!效果很好!
    【解决方案2】:

    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方法中因为findViewById在设置内容布局后调用。

    欲了解更多信息,请参阅System services not available to Activities before onCreate()

    【讨论】:

    • @PriyaKar 很高兴为您提供帮助,如果此答案或任何其他答案对您有所帮助,那么请不要忘记接受该答案,无论如何享受编码。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-01-25
    • 2013-10-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-08-13
    相关资源
    最近更新 更多