Frage

my project was running along fine until I declared an onItemSelctedListener for my spinner.. now my project is not running.. and the log cat is showing the following error...

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)

NOW the points to be noted are

1) the main activity method is not being implemented at all.. not even super.onCreate

now this was the code that i entered.. but as its not being implemented, id odnt know abt its relevence..

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

this is the listview function

    myAdapter1 = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, listYear);
    myAdapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    year.setAdapter(myAdapter1);

so what is the error?? thanks in advance..

this is the code b4 my oncreate function

   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;
War es hilfreich?

Lösung

The key part is here:

Caused by: java.lang.IllegalStateException: System services not available to Activities before onCreate()

Further down the stacktrace:

at android.widget.ArrayAdapter.<init>(ArrayAdapter.java:153)
at com.example.testqstn.MainActivity.<init>(MainActivity.java:39)

So you're initializing an ArrayAdapter as a member variable of your MainActivity, specifically on row 39 of MainActivity.java. You can't do that. You can declare it as a member variable but initialize it (with new) in onCreate() or later.

From your updated question code, replace the declaration+initialization

ArrayAdapter<String> myAdapter1 = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, listYear);

with just declaration

ArrayAdapter<String> myAdapter1;

and move the initialization

myAdapter1 = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, listYear);

to somewhere between onCreate() and where myAdapter1 is used for the first time.

Andere Tipps

Caused by: java.lang.IllegalStateException: System services not available to Activities before onCreate()

Context is available once activity is created.

http://developer.android.com/reference/android/content/Context.html

Edit:

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);

Also renaming listYear(schemetxt); or List<String> listYear would help readablity

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top