gpt4 book ai didi

java - fragment java.lang.ClassCastException错误

转载 作者:行者123 更新时间:2023-12-02 06:27:41 24 4
gpt4 key购买 nike

如何转换Fragment Activity ?

实际上我正在尝试通过Activity上的按钮打开Fragment Activity

Android MainFest:

<activity android:name="parth.any.ttb.Fragment_1"/>

LogCat:

12-04 15:22:43.132: E/AndroidRuntime(25873): FATAL EXCEPTION: main
12-04 15:22:43.132: E/AndroidRuntime(25873): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{parth.any.ttb/parth.any.ttb.Fragment_1}: java.lang.ClassCastException: parth.any.ttb.Fragment_1
12-04 15:22:43.132: E/AndroidRuntime(25873): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1573)
12-04 15:22:43.132: E/AndroidRuntime(25873): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
12-04 15:22:43.132: E/AndroidRuntime(25873): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
12-04 15:22:43.132: E/AndroidRuntime(25873): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
12-04 15:22:43.132: E/AndroidRuntime(25873): at android.os.Handler.dispatchMessage(Handler.java:99)
12-04 15:22:43.132: E/AndroidRuntime(25873): at android.os.Looper.loop(Looper.java:130)
12-04 15:22:43.132: E/AndroidRuntime(25873): at android.app.ActivityThread.main(ActivityThread.java:3687)
12-04 15:22:43.132: E/AndroidRuntime(25873): at java.lang.reflect.Method.invokeNative(Native Method)
12-04 15:22:43.132: E/AndroidRuntime(25873): at java.lang.reflect.Method.invoke(Method.java:507)
12-04 15:22:43.132: E/AndroidRuntime(25873): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
12-04 15:22:43.132: E/AndroidRuntime(25873): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
12-04 15:22:43.132: E/AndroidRuntime(25873): at dalvik.system.NativeStart.main(Native Method)
12-04 15:22:43.132: E/AndroidRuntime(25873): Caused by: java.lang.ClassCastException: parth.any.ttb.Fragment_1
12-04 15:22:43.132: E/AndroidRuntime(25873): at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
12-04 15:22:43.132: E/AndroidRuntime(25873): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1565

)

这是两个类代码:编辑:NewTransaction.java

startActivity(new Intent(getApplicationContext(), Fragment_1.class));

Fragment_1.java

public class Fragment_1 extends Fragment implements OnClickListener {
TextView Home;
Animation animMove;

// slide menu items
private String[] spinMenuTitles;
private TypedArray spinMenuIcons;
private Button Add_New;
private ArrayList<TypeCustomSpinnerItem> spinDropItems;
private TypeSpinnerListAdapter adapter;

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
View view = inflater.inflate(R.layout.fragment_1, null);
Home = (TextView)view.findViewById(R.id.home);
Add_New = (Button)view.findViewById(R.id.add_new);
Add_New.setOnClickListener(this);

animMove = AnimationUtils.loadAnimation(getActivity(),R.anim.move);
Home.startAnimation(animMove);

// load type spin menu items
spinMenuTitles = getResources().getStringArray(R.array.type_spinner_items);

// type spinner icons from resources
spinMenuIcons = getResources().obtainTypedArray(R.array.type_spinner_icons);

spinDropItems = new ArrayList<TypeCustomSpinnerItem>();

// adding type spinner items to array
int i;
for(i=0;i<=9;i++)
{
spinDropItems.add(new TypeCustomSpinnerItem(spinMenuTitles[i], spinMenuIcons.getResourceId(i, -1)));
}
// Recycle the typed array
spinMenuIcons.recycle();

Spinner mySpinner = (Spinner)view.findViewById(R.id.spinner1);
adapter = new TypeSpinnerListAdapter(getActivity(),spinDropItems);
mySpinner.setAdapter(adapter);

return view;
}

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.add_new:
Intent addintent = new Intent(getActivity() , AddNewTransaction.class);
startActivity(addintent);
break;

}
}
}

最佳答案

您不能将 fragment 作为 Activity 启动,它们需要添加到 Activity 中。

在文档 here 中了解更多信息.

另请参阅this

关于java - fragment java.lang.ClassCastException错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20372229/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com