Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/209.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 试图制作一个自动完成的程序,但它不起作用_Java_Android_Eclipse - Fatal编程技术网

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

Java 试图制作一个自动完成的程序,但它不起作用,java,android,eclipse,Java,Android,Eclipse,我正在努力做一个自动完成一年中几个月的计划。每次我运行它时,它似乎都会崩溃。我试着尽可能多地检查和纠正,但我是android新手,在编程方面有两个月的差距,所以我无法找出我做错了什么。提前谢谢你的帮助。!现在任何帮助都会很好 package com.book.listviewtest; //import com.newboston.thefirst.Menu; import android.app.Activity; import android.graphics.C

我正在努力做一个自动完成一年中几个月的计划。每次我运行它时,它似乎都会崩溃。我试着尽可能多地检查和纠正,但我是android新手,在编程方面有两个月的差距,所以我无法找出我做错了什么。提前谢谢你的帮助。!现在任何帮助都会很好

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


}
package com.book.listviewtest;
//导入com.newboston.thefirst.Menu;
导入android.app.Activity;
导入android.graphics.Color;
导入android.os.Bundle;
导入android.view.view;
导入android.view.view.OnClickListener;
导入android.widget.ArrayAdapter;
导入android.widget.AutoCompleteTextView;
导入android.widget.Button;
公共类自动完成扩展活动{
ArrayAdapter monthArray=新的ArrayAdapter(这是android.R.layout.simple\u list\u item\u 1,月);
最终自动完成文本视图文本视图=(自动完成文本视图)findViewById(R.id.testAC);
按钮更改b=(按钮)findViewById(R.id.btextC);
按钮更改B2=(按钮)findViewById(R.id.bAC);
创建时的公共void(在SavedInstantState上绑定){
super.onCreate(onSavedInstantState);
setContentView(R.layout.autocomplete);
textView.setAdapter(蒙塔雷);
changeB.setOnClickListener(新建按钮.OnClickListener(){
公共void onClick(视图v){
if(textView.getHeight()==100){
textView.setHeight(30);
}否则{
textView.setHeight(100);
}   
}
});
changeB2.setOnClickListener(新建按钮.OnClickListener(){
@凌驾
公共void onClick(视图v){
//TODO自动生成的方法存根
textView.setTextColor(Color.RED);
}
});
}
静态最终字符串[]个月=新字符串[]{“一月”、“二月”、“三月”、“四月”};
}
我收到以下错误:

  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
07-1419:32:34.504:E/AndroidRuntime(1098):致命异常:main
07-1419:32:34.504:E/AndroidRuntime(1098):进程:com.book.listviewtest,PID:1098
07-14 19:32:34.504:E/AndroidRuntime(1098):java.lang.RuntimeException:无法实例化activityC组件Info{com.book.listviewtest/com.book.listviewtest.AutoComplete}:java.lang.IllegalStateException:onCreate()之前的活动无法使用系统服务
07-14 19:32:34.504:E/AndroidRuntime(1098):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2121)
07-14 19:32:34.504:E/AndroidRuntime(1098):位于android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
07-14 19:32:34.504:E/AndroidRuntime(1098):在android.app.ActivityThread.access$800(ActivityThread.java:135)
07-14 19:32:34.504:E/AndroidRuntime(1098):在android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
07-14 19:32:34.504:E/AndroidRuntime(1098):在android.os.Handler.dispatchMessage(Handler.java:102)上
07-14 19:32:34.504:E/AndroidRuntime(1098):在android.os.Looper.loop(Looper.java:136)
07-14 19:32:34.504:E/AndroidRuntime(1098):位于android.app.ActivityThread.main(ActivityThread.java:5017)
07-14 19:32:34.504:E/AndroidRuntime(1098):位于java.lang.reflect.Method.Invokenactive(本机方法)
07-14 19:32:34.504:E/AndroidRuntime(1098):位于java.lang.reflect.Method.invoke(Method.java:515)
07-14 19:32:34.504:E/AndroidRuntime(1098):在com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
07-14 19:32:34.504:E/AndroidRuntime(1098):位于com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
07-14 19:32:34.504:E/AndroidRuntime(1098):在dalvik.system.NativeStart.main(本机方法)
07-14 19:32:34.504:E/AndroidRuntime(1098):由以下原因引起:java.lang.IllegalStateException:onCreate()之前的活动无法使用系统服务
07-14 19:32:34.504:E/AndroidRuntime(1098):在android.app.Activity.getSystemService(Activity.java:4532)上
07-14 19:32:34.504:E/AndroidRuntime(1098):位于android.widget.ArrayAdapter.init(ArrayAdapter.java:310)
07-14 19:32:34.504:E/AndroidRuntime(1098):在android.widget.ArrayAdapter.(ArrayAdapter.java:128)
07-14 19:32:34.504:E/AndroidRuntime(1098):在com.book.listviewtest.AutoComplete上。(AutoComplete.java:16)
07-14 19:32:34.504:E/AndroidRuntime(1098):位于java.lang.Class.newInstanceImpl(本机方法)
07-14 19:32:34.504:E/AndroidRuntime(1098):位于java.lang.Class.newInstance(Class.java:1208)
07-14 19:32:34.504:E/AndroidRuntime(1098):在android.app.Instrumentation.newActivity(Instrumentation.java:1061)上
07-14 19:32:34.504:E/AndroidRuntime(1098):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2112)上
2007-14 19:32:34.504:E/AndroidRuntime(1098):。。。还有11个
07-14 19:32:36.854:I/进程(1098):发送信号。PID:1098信号: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);
ArrayAdapter monthArray=新的ArrayAdapter(这是android.R.layout.simple\u list\u item\u 1,月);
最终自动完成文本视图文本视图=(自动完成文本视图)findViewById(R.id.testAC);
按钮更改b=(按钮)findViewById(R.id.btextC);
按钮更改B2=(按钮)findViewById(R.id.bAC);
OnCreate
方法中,因为
findViewById
在设置内容布局后调用

有关更多信息,请参见

ArrayAdapter monthArray=new ArrayAdapter(这是android.R.layout.simple\u list\u item\u 1,Months);
最终自动完成文本视图文本视图=(自动完成文本视图)findViewById(R.id.testAC);
按钮更改b=(按钮)findViewById(R.id.btextC);
按钮更改B2=(按钮)findViewById(R.id.bAC);
将这些初始化移动到
onCreate()
,并在
setContentView()之后调用
findViewById()


onCreate()

之前,您无法运行任何活动代码。非常感谢您。。!!很好@PriyaKar很乐意提供帮助,如果这个答案或任何其他答案对您有所帮助,那么不要忘记接受这个答案,无论如何,请享受编码。
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);