Android:在片段上创建按钮时获取classcastexception

时间:2014-03-21 00:29:20

标签: java android button

这是片段代码。

我正在尝试在里面创建一个按钮,但我一直收到错误。

public static class PlaceholderFragment extends Fragment {

    Button startButton;

    public PlaceholderFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        View rootView;
        rootView = inflater.inflate(R.layout.fragment_main, container, false);

        startButton = (Button) rootView.findViewById(R.id.enter);
        startButton.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                startActivity(new Intent("com.appname.STUFF"));
            }
        });

        return rootView;
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);

    }
}

这是我不断得到的错误。我不知道为什么,香港专业教育学院尝试在很多地方这样做,但仍然得到这个错误。

03-20 20:27:21.093: E/AndroidRuntime(27201): FATAL EXCEPTION: main
03-20 20:27:21.093: E/AndroidRuntime(27201): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.appname/com.appname.MainActivity}: java.lang.ClassCastException: android.widget.Button
03-20 20:27:21.093: E/AndroidRuntime(27201):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651)
03-20 20:27:21.093: E/AndroidRuntime(27201):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
03-20 20:27:21.093: E/AndroidRuntime(27201):    at android.app.ActivityThread.access$1500(ActivityThread.java:117)
03-20 20:27:21.093: E/AndroidRuntime(27201):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
03-20 20:27:21.093: E/AndroidRuntime(27201):    at android.os.Handler.dispatchMessage(Handler.java:99)
03-20 20:27:21.093: E/AndroidRuntime(27201):    at android.os.Looper.loop(Looper.java:130)
03-20 20:27:21.093: E/AndroidRuntime(27201):    at android.app.ActivityThread.main(ActivityThread.java:3691)
03-20 20:27:21.093: E/AndroidRuntime(27201):    at java.lang.reflect.Method.invokeNative(Native Method)
03-20 20:27:21.093: E/AndroidRuntime(27201):    at java.lang.reflect.Method.invoke(Method.java:507)
03-20 20:27:21.093: E/AndroidRuntime(27201):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:912)
03-20 20:27:21.093: E/AndroidRuntime(27201):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:670)
03-20 20:27:21.093: E/AndroidRuntime(27201):    at dalvik.system.NativeStart.main(Native Method)
03-20 20:27:21.093: E/AndroidRuntime(27201): Caused by: java.lang.ClassCastException: android.widget.Button
03-20 20:27:21.093: E/AndroidRuntime(27201):    at com.appname.MainActivity$PlaceholderFragment.onCreateView(MainActivity.java:179)
03-20 20:27:21.093: E/AndroidRuntime(27201):    at android.support.v4.app.Fragment.performCreateView(Fragment.java:1500)
03-20 20:27:21.093: E/AndroidRuntime(27201):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:928)
03-20 20:27:21.093: E/AndroidRuntime(27201):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1105)
03-20 20:27:21.093: E/AndroidRuntime(27201):    at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
03-20 20:27:21.093: E/AndroidRuntime(27201):    at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1468)
03-20 20:27:21.093: E/AndroidRuntime(27201):    at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:570)
03-20 20:27:21.093: E/AndroidRuntime(27201):    at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1132)
03-20 20:27:21.093: E/AndroidRuntime(27201):    at android.app.Activity.performStart(Activity.java:3833)
03-20 20:27:21.093: E/AndroidRuntime(27201):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1624)
03-20 20:27:21.093: E/AndroidRuntime(27201):    ... 11 more

这是XML

<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"
    android:background="@drawable/main"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.appname.MainActivity$PlaceholderFragment" >

    <Button
        android:id="@+id/enter"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="192dp"
        android:text="Enter" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:text="TITLE"
        android:textSize="50sp" />

</RelativeLayout>

2 个答案:

答案 0 :(得分:0)

在你的情况下,一切看起来都是正确的,我相信你应该做了正确的导入。

Eclipse会不时地弄乱你的资源。这会导致一些奇怪的行为,例如在您的应用程序中交换字符串和图像,更常见的是classCastException(s),当Eclipse切换您的视图时会发生这种情况。 ids周围。

该问题的一些解决方案:

Clean your project.

Modify an xml layout file and save.

Delete your R file. (Don't worry it will be automatically generated again).

基本上任何使项目重建并重新生成R文件的东西。

希望这有帮助。

答案 1 :(得分:0)

检查您的导入:

import android.widget.Button;  

清理并重建 (显然,这对您没有任何改变)

删除并手动重建xml按钮,它会自动重建您的R文件。

不确定,但也许相关.. 更改您的意图方法

Intent i = new Intent(); 
i.setClassName("com.appname.ACTIVITY", "com.appname.STUFF");
getActivity().startActivity(i);  

或使用没有PackageName的ClassName:

getActivity().startActivity(new Intent(getActivity(), StuffActivity.class);  

还要确保您正在导入正确的班级名称。

如果这不起作用,请将您的onClickListener更改为 onClick方法,如:

// in your xml Button
android:onClick="launchActivity"  

// in your Fragment
public void launchActivity(View view) {
    getActivity().startActivity(new Intent(getActivity(), StuffActivity.class);
}  

您还可以将FragmentFragmentActivity的方法称为:

// Fragment: onClickListener
startButton.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
        ((Activity) getActivity()).launchActivity();
    }
}

// FragmentActivity: launchActivity method
public void launchActivity() {
    startActivity(new Intent(Activity.this, StuffActivity.class));
} 

我希望你能实现你想要的目标。