2016-08-26 47 views
0

我有简单的项目有列表视图 时,任何项目任何一个点击列表视图 在单一窗口 开放的片段 问题是,我想取代我对在这个项目上 http://www.truiton.com/2015/06/android-tabs-example-fragments-viewpager/需要android.support.v4.app.fragment

我把我的mainactivty类开关的情况下,但它得到了我这个错误 在第一张图片 我尝试导入进口android.support.v4.app.Fragment这个标签片段的片段 但也是相同的promplem

这是我的项目 .... Main2Activity类

public class Main2Activity extends AppCompatActivity implements fragmentA.comunicatir 
     { 

     fragmentA f1; 
     fragmentB f2; 
     FragmentManager manegaer; 

     @Override 
     protected void onCreate(Bundle savedInstanceState){ 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main2); 

       manegaer=getSupportFragmentManager(); 
     f1=(fragmentA)manegaer.findFragmentById(R.id.fragment1); 
     f1.setcomunicatir(this); 
     } 


     @Override 
     public void respned(int index){ 
       f2=(fragmentB)manegaer.findFragmentById(R.id.fragment2); 

     if(f2!=null&&f2.isVisible()){ 

     f2.cahngedata(index); 
     }else{ 
     Intent intent= 
     new Intent(this, 
     AnotherActivity.class); 
     intent.putExtra("index",index); 
     startActivity(intent); 
     } 


     } 
     } 

error 2

的clases的另一个类 类fragmentA

public class fragmentA extends Fragment implements AdapterView.OnItemClickListener 
{ 
    ListView list; 
    comunicatir com; 
    @Override 
    public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState) 
    { 
     View view = inflater.inflate(R.layout.fragment1,container,false); 
     list=(ListView)view.findViewById(R.id.listView1); 

     ArrayAdapter adapter = ArrayAdapter.createFromResource(getActivity(), 
       R.array.chapters, android.R.layout.simple_list_item_1); 
     list.setAdapter(adapter); 
     list.setOnItemClickListener(this); 
     return view; 

    } 

    public void setcomunicatir (comunicatir comunicator) 
    { 

     this.com=comunicator; 
    } 
    @Override 
    public void onItemClick(AdapterView<?> adapterView, View veiw, int i, long l) { 

     com.respned(i); 

    } 


    public interface comunicatir 
    { 
     public void respned(int index); 

    } 
} 

    public class fragmentB extends Fragment 

{ 

    TextView text; 
    TextView textView2; 

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

     View view = inflater.inflate(R.layout.fragment2, container, false); 
     //text = (TextView) view.findViewById(R.id.textView1); 
     //imgv=(ImageView) view.findViewById(R.id.imageView1); 
     textView2 =(TextView)view.findViewById(R.id.textView22); 

     return view; 

    } 
    public void cahngedata(int index) 
    { 

    /*String[] descrption =getResources().getStringArray(R.array.hadeith); 
      text.setText(descrption[index]);*/ 

     String []Hadith=getResources().getStringArray(R.array.hadeith); 
     //imgv.setImageResource(img[index]); 
     textView2.setText(Hadith[index]); 

     //int []descrption =getResources().getIntArray(img[index]); 
     // imgv.setImageResource(descrption[index]);  

      /*text.setText(descrption[index]); 
     int arr[]=getResources().getIntArray(img[index]); 
      imgv.setImageResource(arr[index]);*/ 


    } 
} 



public class AnotherActivity extends Activity 

{ 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     // TODO Auto-generated method stub 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.another); 

     Intent intent = getIntent(); 
     int index= intent.getIntExtra("index", 0); 
     fragmentB f2= (fragmentB) 
       getFragmentManager(). 
         findFragmentById(R.id.fragment2); 
     if (f2!=null) 
      f2.cahngedata(index); 

    } 
} 

的另一个活动类也有错误的图片2

error 2

回答

0

第1期:你不能把一个活动上的标签,但你可能不会需要getItem()时,你可以删除方法

第2期:

替换:

fragmentB f2 = (fragmentB) ... 

有了:

fragment f2 = (Fragment) ... 
+0

getItem()无法删除,因为它是植入方法 –

+0

只需将“返回新的Tab1()”就可以了。不要紧,如果你不会使用它 –

+0

好吧,但它不显示我的Main2Activity我试图改变Main2Activity扩展片段看到这张照片https://drive.google.com/file/d/0ByZGzcCum73xQ0trVEh3cmhiNHM/view?usp=sharing –

0

转到您遇到问题的tab.class并更改 import android.app.Fragment; 至 import android.support.v4.app.Fragment;

它会运行。

相关问题