Click here to Skip to main content
15,892,674 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
MainActivity.java

package com.example.alex.something;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;


public class MainActivity extends ActionBarActivity {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        new Something();

    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement

        if (id == R.id.action_settings) {

            return true;

        }

        return super.onOptionsItemSelected(item);
    }
}



Something.java

import android.view.View;
import android.widget.*;




public class Something extends MainActivity {

    private Button b;


    public Something(){

        setContentView(R.layout.activity_main);

        b = (Button) findViewById(R.id.button);
        b.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                b.setText("Something else");
            }
        });

    }

}


Log Cat

  --------- beginning of crash
04-04 13:17:42.026    1902-1902/com.example.alex.something E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.example.alex.something, PID: 1902
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.alex.something/com.example.alex.something.MainActivity}: java.lang.IllegalStateException: System services not available to Activities before onCreate()
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
            at android.app.ActivityThread.access$800(ActivityThread.java:144)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5221)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
     Caused by: java.lang.IllegalStateException: System services not available to Activities before onCreate()
            at android.app.Activity.getSystemService(Activity.java:4993)
            at android.view.LayoutInflater.from(LayoutInflater.java:219)
            at android.support.v7.app.ActionBarActivityDelegateBase.ensureSubDecor(ActionBarActivityDelegateBase.java:309)
            at android.support.v7.app.ActionBarActivityDelegateBase.setContentView(ActionBarActivityDelegateBase.java:237)
            at android.support.v7.app.ActionBarActivity.setContentView(ActionBarActivity.java:102)
            at com.example.alex.something.Something.<init>(Something.java:18)
            at com.example.alex.something.MainActivity.onCreate(MainActivity.java:16)
            at android.app.Activity.performCreate(Activity.java:5937)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
            at android.app.ActivityThread.access$800(ActivityThread.java:144)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5221)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)


I'm not really sure how it works in android, but isn't the "onCreate" method like the
"Main" method in Java, if so why ins't it working? I'm having it the "GUI" and "Listeners" are in another class and all I have to do in my "Main" class is call them and the "Activity" will execute. I just want One "Activity" and then a whole bunch of classes doing stuff, for instance having one class handle the GUI and the other handle the listeners which seems like it's not going to happen if I can't do this with just one main class and then everything in another class. Again, this would of worked in plain Java, but I'm not really sure why it isn't working here? By that I mean have your GUI and Listener classes execute in your main method to converge into one single program Anyways, anyone has any idea why this won't work?
Posted
Comments
Mohibur Rashid 4-Apr-15 19:36pm    
follow the link,
http://stackoverflow.com/questions/11645906/system-services-not-available-to-activities-before-oncreate-error-message
same issue.
Zink713 4-Apr-15 21:27pm    
It isn't quite the same problem, his is caused my initializing an object outside the"onCreate" method. I call mine inside my "onCrease method" and it crashed for some reason, I don't really know what's up since this works in plain old Java

1 solution

The following statement in your MainActivity.onCreate method makes no sense.
Java
new Something();

Take a look at some of the Android articles at http://www.codeproject.com/KB/android/[^] to see how it should be done.
 
Share this answer
 
Comments
Zink713 5-Apr-15 14:50pm    
Why, because I haven't made an instance of it? I don't want to reuse the code, like an anonymous listener nested inside a button I just want to use it once. Anyways, you have any idea why my code isn't working?
Richard MacCutchan 6-Apr-15 3:50am    
I suspect it is because of that call which will go back to the MainActivity constructor. Like I said, it makes no sense.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900