如何投射片段活动?
实际上我正试图通过活动上的按钮打开片段活动
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;
}
}
}
答案 0 :(得分:2)
答案 1 :(得分:0)
Frament_1
是一个片段
public class Fragment_1 extends Fragment implements OnClickListener {
您不能将startActivity用于片段。 Fragment
由活动托管。
在您的活动布局中,xml具有以下内容。
以编程方式将片段添加到现有ViewGroup。
<FrameLayout
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" />
FrameLayout是容器。您正在向容器添加片段。
活动
FragmentTransaction fragTran = getSupportFragmentManager().beginTransaction();
Fragment_1 fragment1 = new Fragment_1()
fragTran.replace(R.id.content_frame, fragment1);
您也可以在xml中使用以下内容。在活动的布局文件中声明片段。
<fragment android:name="com.example.fragment.Fragment_1"
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="match_parent" />
了解更多信息
http://developer.android.com/guide/components/fragments.html