2014-06-13 6 views
1

조각으로 작업해야하는 응용 프로그램을 만들어야합니다. 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); 

     } 

    } 

screen shot of error

답변

1

이전 답변에서 언급했듯이 문제가 올바르지 않습니다. FirstFragment의 Fragment library. FirstFragment.java의

변경 행 5 :

import android.support.v4.app.Fragment; 

에 :

import android.app.Fragment; 
+0

헤이 숀, 내가 당신에게 한 가지 더 친구를 요청해야합니다. 이 문제를 해결 한 후 .. 내 응용 프로그램을 실행하려고하면 오류가 발생합니다 : ** java.lang.RuntimeException : 활동을 인스턴스화 할 수 없습니다. componentInfo (com.dev.testapp/com.dev.testapp.firstFragment) : java.lang.classCastException : com.dev.testapp.FirstFragment를 android.app.activity **로 캐스팅 할 수 없습니다. ** – Devraj

+0

자세한 정보가 없으면 잘 모르겠습니다 만, Fragment를 활동? 실수로 매니페스트에 조각을 포함 시켰습니까? – Shaun

+0

사실 나는 활동으로 만 Second.java를 사용하고 있으며 manifest.xml 파일에서이 문제를 언급했습니다. 그걸 제거해야합니까 ?? – Devraj

1

활동의 사용 :

나는이 모든으로 수행하고
package com.dev.testapp; 

    import android.app.Fragment; 
    import android.os.Bundle; 
    import android.view.LayoutInflater; 
    import android.view.View; 
    import android.view.ViewGroup; 

    public class SecondFragment extends Fragment{ 

     public View onCreate(LayoutInflater inflater, 
       ViewGroup container , Bundle savedInstanceState) { 

      //Inflate the layout for this fragment 

      return inflater.inflate(R.layout.fragment2,container, false); 

     } 

    } 

, 내 SecondActivity 내가 처리하는 방법을 모르는 오류를 보여주고있다 :그리고 여기가 SecondFragment입니다 이 import android.app.Fragment;

당신의 조각은 그래서 당신은 모두 exten이 중 import android.support.v4.app.Fragment;

입니다 앱 패키지 또는 지원 라이브러리에서 조각을 파헤 치기

1

두 번째 조각의 형식은 android.support.v4.app.Fragment 인 반면, 두 번째 조각의 형식은 android.support.v4.app.Fragment입니다.

관련 문제