我正在尝试制作一年中几个月的自动完成程序。每次运行它似乎都会崩溃。我试图尽可能多地检查和纠正,但我是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
答案 0 :(得分: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()
之后将这些初始化移至findViewById()
和setContentView()
来电。
在onCreate()
之前,您无法真正运行任何活动代码。
答案 1 :(得分:0)
写
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()