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>