조각으로 작업해야하는 응용 프로그램을 만들어야합니다. MainActivity에는 WebView가 있습니다. SecondActivity I 에서 Fragment를 사용하기 시작했습니다. 그 후조각으로 작업 할 때 직면하는 문제
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="#ffffff" >
<Button
android:id="@+id/secondbtn1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="first fragment"
android:onClick="chooseFragment" />
<Button
android:id="@+id/secondbtn2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="second fragment"
android:onClick="chooseFragment"/>
<fragment
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:name="com.dev.testapp.fragment1"
android:id="@+id/frag1"
/>
</LinearLayout>
이 코드를 필요로 내가 조각 두 개 클래스를 생성 :
package com.dev.testapp;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.View;
public class Second extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
}
public void choosefragment(View view) {
Fragment fg;
if(view == findViewById(R.id.secondbtn2)) {
fg = new SecondFragment();
}
else
{
fg = new FirstFragment();
}
FragmentManager fm = getFragmentManager();
FragmentTransaction fragmentTransaction = fm.beginTransaction();
fragmentTransaction.replace(R.id.frag1, fg);
fragmentTransaction.commit();
}
}
Hhere가 second.xml 파일 : 여기에
는 SecondActivity의 코드 FirstFragment의 :package com.dev.testapp;
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class FirstFragment extends Fragment{
public View onCreate(LayoutInflater inflater,
ViewGroup container , Bundle savedInstanceState) {
//Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment1,container, false);
}
}
헤이 숀, 내가 당신에게 한 가지 더 친구를 요청해야합니다. 이 문제를 해결 한 후 .. 내 응용 프로그램을 실행하려고하면 오류가 발생합니다 : ** java.lang.RuntimeException : 활동을 인스턴스화 할 수 없습니다. componentInfo (com.dev.testapp/com.dev.testapp.firstFragment) : java.lang.classCastException : com.dev.testapp.FirstFragment를 android.app.activity **로 캐스팅 할 수 없습니다. ** – Devraj
자세한 정보가 없으면 잘 모르겠습니다 만, Fragment를 활동? 실수로 매니페스트에 조각을 포함 시켰습니까? – Shaun
사실 나는 활동으로 만 Second.java를 사용하고 있으며 manifest.xml 파일에서이 문제를 언급했습니다. 그걸 제거해야합니까 ?? – Devraj