我的项目运行良好,直到我为我的微调器声明了一个onItemSelctedListener。 现在我的项目没有运行..日志猫显示以下错误...
03-03 01:59:08.465: E/AndroidRuntime(1401):
FATAL EXCEPTION: main
Process: com.example.testqstn, PID: 1401
java.lang.RuntimeException: Unable to instantiate activity
ComponentInfo{com.example.testqstn/com.example.testqstn.MainActivity}:
java.lang.IllegalStateException: System services not available to Activities
before onCreate()
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2121)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.IllegalStateException: System services not available to Activities before onCreate()
at android.app.Activity.getSystemService(Activity.java:4532)
at android.widget.ArrayAdapter.init(ArrayAdapter.java:310)
at android.widget.ArrayAdapter.<init>(ArrayAdapter.java:153)
at com.example.testqstn.MainActivity.<init>(MainActivity.java:39)
at java.lang.Class.newInstanceImpl(Native Method)
at java.lang.Class.newInstance(Class.java:1208)
at android.app.Instrumentation.newActivity(Instrumentation.java:1061)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2112)
现在要注意的要点是
1)主要活动方法根本没有实现..甚至没有super.onCreate
现在这是我输入的代码..但由于它没有实现,id odnt知道abt它的相关性..
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
main();
}
public void main()
{
TextView textyear,textsubject,textschema;
Typeface typeface = Typeface.createFromAsset(getAssets(), "fonts/Roboto/Roboto-Light.ttf");
textyear = (TextView)findViewById(R.id.textyear);
textsubject = (TextView)findViewById(R.id.textsubject);
textschema = (TextView)findViewById(R.id.textscheme);
textyear.setTypeface(typeface);
textsubject.setTypeface(typeface);
textschema.setTypeface(typeface);
year = (Spinner)findViewById(R.id.spinyear);
subject = (Spinner)findViewById(R.id.spinsub);
scheme = (Spinner)findViewById(R.id.spinscheme);
schemetxt = scheme.toString();
buttonSound = MediaPlayer.create(MainActivity.this,R.raw.sound );
btn=(Button)findViewById(R.id.button);
btn.setBackgroundResource(R.drawable.selector);
// set dynamic value change
scheme.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
listYear(schemetxt);
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v)
{
pdfSelection(v);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
这是列表视图功能
myAdapter1 = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, listYear);
myAdapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
year.setAdapter(myAdapter1);
那么错误是什么?提前谢谢..
这是代码b4我的oncreate函数
Button btn;
Spinner year , subject , scheme;
String yeartxt = null;
String subtxt = null;
String schemetxt = null;
List<String> listYear;
ArrayAdapter<String> myAdapter1 = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, listYear);
MediaPlayer buttonSound;
答案 0 :(得分:5)
关键部分在这里:
Caused by: java.lang.IllegalStateException: System services not available to Activities before onCreate()
进一步向下走栈:
at android.widget.ArrayAdapter.<init>(ArrayAdapter.java:153)
at com.example.testqstn.MainActivity.<init>(MainActivity.java:39)
因此,您要将ArrayAdapter
初始化为MainActivity
的成员变量,特别是MainActivity.java
的第39行。你不能这样做。您可以将其声明为成员变量,但在new
或更高版本中对其进行初始化(onCreate()
}。
从更新的问题代码中,替换声明+初始化
ArrayAdapter<String> myAdapter1 = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, listYear);
只有声明
ArrayAdapter<String> myAdapter1;
并移动初始化
myAdapter1 = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, listYear);
介于onCreate()
和首次使用myAdapter1
之间的某个位置。
答案 1 :(得分:2)
引起:java.lang.IllegalStateException:系统服务没有 在onCreate()
之前可用于活动
创建活动后可以使用上下文。
http://developer.android.com/reference/android/content/Context.html
编辑:
ArrayAdapter<String> myAdapter1;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// make sure listYear is populated before this
myAdapter1 = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, listYear);
同时重命名listYear(schemetxt);
或List<String> listYear
有助于实现可读性