Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8801669
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T00:53:38+00:00 2026-06-14T00:53:38+00:00

My SherlockActivity, which inflates a layout with an EditText, Button, and a ListView. <RelativeLayout

  • 0

My SherlockActivity, which inflates a layout with an EditText, Button, and a ListView.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >

<ListView
    android:id="@+id/listView1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/editText1" >
</ListView>

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/editText1"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"
    android:text="Add"
    tools:ignore="HardcodedText" />

<EditText
    android:id="@+id/editText1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_toLeftOf="@+id/button1"
    android:ems="10"
    android:hint="Click add!"
    android:inputType="text"
    tools:ignore="HardcodedText" >
</EditText>

My MainActivity extends BaseActivity which inflates a menu layout file of the following

<menu xmlns:android="http://schemas.android.com/apk/res/android" >

<item
    android:id="@+id/menu_settings"
    android:orderInCategory="100"
    android:showAsAction="never"
    android:title="@string/menu_settings"/>
<item
    android:id="@+id/add_item"
    android:orderInCategory="1"
    android:showAsAction="ifRoom"
    android:title="@string/add"/>
<item
    android:id="@+id/calendar"
    android:orderInCategory="2"
    android:showAsAction="ifRoom"
    android:title="@string/show_cal"/>
</menu>

I create a BaseActivity that extends SherlockActivity.

package com.courseorganizer;

import com.actionbarsherlock.app.SherlockActivity;
import com.actionbarsherlock.view.Menu;

public class BaseActivity extends SherlockActivity {
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getSupportMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
}

The MainActivity is where I have the problem. I extend BaseActivity and set up the onOptionsItemSelected() so that if the “Add” tab is selected, the input field is called. Then, I set up the OnClickListener to the button to add the user’s input to the ListView populated in an ArrayList that’s put into an ArrayAdapter.

Here is the MainActivity:

    package com.courseorganizer;

import java.util.ArrayList;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;

import com.actionbarsherlock.view.MenuItem;

public class MainActivity extends BaseActivity {

private final String TAG = "Main Activity";

// LIST OF ARRAY STRINGS WHICH WILL SERVE AS LIST ITEMS
ArrayList<String> listItems = new ArrayList<String>();

// DEFINING STRING ADAPTER WHICH WILL HANDLE DATA OF LISTVIEW
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, listItems);

Button addItem = (Button)findViewById(R.id.button1);
EditText input = (EditText)findViewById(R.id.editText1);
ListView courseList = (ListView)findViewById(R.id.listView1);

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    addItem.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            listItems.add(input.getText().toString());
            courseList.setAdapter(adapter);
        }
    });
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case R.id.add_item:
        input.requestFocus();
        Log.i(TAG, "Add Clicked");
        return true;
    case R.id.calendar:
        Log.i(TAG, "Calendar Clicked");
        return true;
    default:
        return super.onOptionsItemSelected(item);
    }
}
}

I appreciate your help!

EDIT: Here’s my logcat output:

11-04 19:16:24.517: D/AndroidRuntime(305): Shutting down VM
11-04 19:16:24.517: W/dalvikvm(305): threadid=1: thread exiting with uncaught exception     (group=0x4001d800)
11-04 19:16:24.569: E/AndroidRuntime(305): FATAL EXCEPTION: main
11-04 19:16:24.569: E/AndroidRuntime(305): java.lang.RuntimeException: Unable to     instantiate activity ComponentInfo{com.courseorganizer/com.courseorganizer.MainActivity}:     java.lang.IllegalStateException: System services not available to Activities before     onCreate()
11-04 19:16:24.569: E/AndroidRuntime(305):  at     android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2585)
11-04 19:16:24.569: E/AndroidRuntime(305):  at     android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
11-04 19:16:24.569: E/AndroidRuntime(305):  at     android.app.ActivityThread.access$2300(ActivityThread.java:125)
11-04 19:16:24.569: E/AndroidRuntime(305):  at     android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
11-04 19:16:24.569: E/AndroidRuntime(305):  at     android.os.Handler.dispatchMessage(Handler.java:99)
11-04 19:16:24.569: E/AndroidRuntime(305):  at android.os.Looper.loop(Looper.java:123)
11-04 19:16:24.569: E/AndroidRuntime(305):  at     android.app.ActivityThread.main(ActivityThread.java:4627)
11-04 19:16:24.569: E/AndroidRuntime(305):  at     java.lang.reflect.Method.invokeNative(Native Method)
11-04 19:16:24.569: E/AndroidRuntime(305):  at     java.lang.reflect.Method.invoke(Method.java:521)
11-04 19:16:24.569: E/AndroidRuntime(305):  at     com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
11-04 19:16:24.569: E/AndroidRuntime(305):  at     com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
11-04 19:16:24.569: E/AndroidRuntime(305):  at dalvik.system.NativeStart.main(Native     Method)
11-04 19:16:24.569: E/AndroidRuntime(305): Caused by: java.lang.IllegalStateException:     System services not available to Activities before onCreate()
11-04 19:16:24.569: E/AndroidRuntime(305):  at     android.app.Activity.getSystemService(Activity.java:3526)
11-04 19:16:24.569: E/AndroidRuntime(305):  at     android.widget.ArrayAdapter.init(ArrayAdapter.java:271)
11-04 19:16:24.569: E/AndroidRuntime(305):  at android.widget.ArrayAdapter.<init>    (ArrayAdapter.java:150)
11-04 19:16:24.569: E/AndroidRuntime(305):  at com.courseorganizer.MainActivity.<init>    (MainActivity.java:24)
11-04 19:16:24.569: E/AndroidRuntime(305):  at java.lang.Class.newInstanceImpl(Native     Method)
11-04 19:16:24.569: E/AndroidRuntime(305):  at     java.lang.Class.newInstance(Class.java:1429)
11-04 19:16:24.569: E/AndroidRuntime(305):  at     android.app.Instrumentation.newActivity(Instrumentation.java:1021)
11-04 19:16:24.569: E/AndroidRuntime(305):  at     android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2577)
11-04 19:16:24.569: E/AndroidRuntime(305):  ... 11 more
11-04 19:16:26.998: I/Process(305): Sending signal. PID: 305 SIG: 9
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T00:53:41+00:00Added an answer on June 14, 2026 at 12:53 am

    findViewById() cannot be called before onCreate() and setContentView(), as there is no context or layout to search.

    Move these lines into onCreate() after setContentView().

    addItem = (Button)findViewById(R.id.button1);
    input = (EditText)findViewById(R.id.editText1);
    courseList = (ListView)findViewById(R.id.listView1);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My MainActivity extends SherlockActivity to implement the Actionbar on devices with lower Android versions.
I'm implementing some code from an example (http://arvid-g.de/12/android-4-actionbar-with-tabs-example) and trying to convert it to
I have an activity SearchActivity which extends SherlockActivity. In this activity I have an
I have a basic activity (SherlockActivity subclass), and the view it loads has a
This is a follow up to my question earlier here: Android: Autocomplete TextView Similar
I'm a noob to Android development and i want to develop a game you
When I press the home button it doesn't go back like I think it
I have some problems with my custom adapter for a ListView . I'm unsure
I have an Webview which opens when i click on the actionbar item. So
The app I'm developing is using ActionShercklockBar, On the main activity (SherlockActivity) I have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.