原因:java.lang.ClassCastException:android.app.Application无法转换为com.xxx

原因:java.lang.ClassCastException:android.app.Application无法转换为com.xxx,android,listview,classcastexception,Android,Listview,Classcastexception,在浏览了多个与此相关的问题并尝试了多件事情之后,我最终发现了当前的错误。 基本上,我试图通过单击按钮动态填充列表视图 为此,我有以下.xml摘录: <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/new_exercise_button" android:id="@

在浏览了多个与此相关的问题并尝试了多件事情之后,我最终发现了当前的错误。 基本上,我试图通过单击按钮动态填充列表视图

为此,我有以下.xml摘录:

    <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" />
但是,调试器为
getApplicationContext()
抛出以下错误-如果我不实现它,我会为
adapter.add()
获得一个空异常

原因:java.lang.ClassCastException:android.app.Application无法转换为com.mycompany.ownfitnesstracker.RoutineListView

最后,但并非最不重要的是,我在清单中注册了我的
RoutineListView

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

RoutineListView是一个
活动
,而不是
应用程序
getApplicationContext()
不返回
活动
。您无法通过这种方式获取对RoutineListView的引用


活动之间有一定的通信方式,但很少涉及从一个到另一个的直接方法调用。大多数情况下,您使用
Intent
s,或者在活动之外存储数据(例如,使用
SharedReferences
)。

您可以直接调用
addItems
,而无需使用对象

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

请显示MainActivity?中的代码以及错误的堆栈跟踪
    <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>
public void addItems(View v){
    addLItems(v);
}