1

After browsing multiple questions regarding that and trying multiple things, I end up with the current error. Basically, I am trying to dynamically fill a list view by clicking a button.

For that, I have the following .xml extract:

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/new_exercise_button"
        android:id="@+id/exercise_add"
        android:onClick="addItems"             />
    <ListView
        android:id="@android:id/list"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:drawSelectorOnTop="false" />

I implemented a class RoutineListView for handling the list:

public class RoutineListView extends ListActivity {
    ArrayList<String> listItems=new ArrayList<String>();
    ArrayAdapter<String> adapter;

    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setContentView(R.layout.fragment_main);
        adapter=new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1,
                listItems);
        setListAdapter(adapter);
    }
    public void addLItems(View v) {
        adapter.add("hi");
    }
}

And when the button in my main activity is clicked, I try to invoke the method with the following code in my MainActivity:

public void addItems(View v){
    RoutineListView routineListView = (RoutineListView)this.getApplicationContext();
    routineListView.addLItems(v);
}

However, debugger throws me the following error for the getApplicationContext() - if I don't implement it, I get a null exception for adapter.add().

Caused by: java.lang.ClassCastException: android.app.Application cannot be cast to com.mycompany.ownfitnesstracker.RoutineListView

And last, but not least, I registered my RoutineListView in the manifest:

    <activity
        android:name=".RoutineListView"
        android:label="@string/routine_list_name" >
        <intent-filter>
            <action android:name="android.intent.action.GET_CONTENT" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>
2
  • Can you please show your code from the MainActivity? Commented Aug 11, 2015 at 15:27
  • 1
    and the stack trace of the error Commented Aug 11, 2015 at 15:34

2 Answers 2

1

RoutineListView is an Activity, not an Application. getApplicationContext() does not return an Activity. You can't obtain a reference to a RoutineListView this way.

There are certain ways to communicate between activities, but rarely does that involve direct method invocations from one to another. Most of the time you use Intents, or you store data outside the activities (e.g. with SharedPreferences).

Sign up to request clarification or add additional context in comments.

Comments

0

You can directly call addItems without using an object.

public void addItems(View v){
    addLItems(v);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.