2014-02-14 61 views
0

我正在使用選項卡來執行項目。我收到以下錯誤。 android.support.v4.app.FragmentTransaction中的attach(android.support.v4.app.Fragment)不能應用於(com.outdeh.EventsFragment)。片段錯誤與解析

package com.outdeh; 


import android.os.Bundle; 
import android.support.v4.app.Fragment; 
import android.support.v4.app.FragmentActivity; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.content.Context; 

import com.parse.Parse; 
import com.parse.ParseAnalytics; 
import com.parse.ParseObject; 
import com.parse.PushService; 


public class EventsFragment extends FragmentActivity { 

    @Override 
    public void onCreate(Bundle savedInstanceState){ 


     // Parse.initialize(this, "nSXPAwYG29VZMaCkasdjhakhsdkjsahdhj", "PWeiawu25r7sAmOYdgP0kajhsdkahaskjdhashj"); 

     // ParseObject testObject = new ParseObject("TestObject"); 
     //testObject.put("foo", "bar new"); 
     //testObject.saveInBackground(); 

    } 

    public EventsFragment() { 
     //Required empty public constructor 
    } 


    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
     //Inflate the layout for this fragment 
     return inflater.inflate(R.layout.event_fragment, container, false); 
    } 
} 






package com.outdeh; 

import com.outdeh.R; 
import android.os.Bundle; 
import android.os.StrictMode; 
import android.support.v4.app.FragmentActivity; 
import android.view.Window; 
import android.view.WindowManager; 
import android.widget.TabHost; 
import android.support.v4.app.Fragment; 

import com.parse.Parse; 
import com.parse.ParseAnalytics; 
import com.parse.PushService; 
import com.parse.ParseInstallation; 

public class MainActivity extends FragmentActivity { 
    TabHost tHost; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 

     //Push notifications 
     Parse.initialize(this, "nSXPAwYG29VZMdfsddfsdfsdfdselz", "PWeiawu25r7sAmOYddfdsfsdfsdfssVkSJJEWU"); 
     PushService.setDefaultPushCallback(this, MainActivity.class); 
     ParseAnalytics.trackAppOpened(getIntent()); 
     // Save the current Installation to Parse. 
     ParseInstallation.getCurrentInstallation().saveInBackground(); 


     //Remove title bar 
     this.requestWindowFeature(Window.FEATURE_NO_TITLE); 

     //Remove notification bar 
     //this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); 

     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     tHost = (TabHost) findViewById(android.R.id.tabhost); 
     tHost.setup(); 

     /** Defining Tab Change Listener event. This is invoked when tab is changed */ 
     TabHost.OnTabChangeListener tabChangeListener = new TabHost.OnTabChangeListener() { 

      @Override 
      public void onTabChanged(String tabId) { 
       android.support.v4.app.FragmentManager fm = getSupportFragmentManager(); 
       HomeFragment homeFragment = (HomeFragment) fm.findFragmentByTag("outdeh"); 
       EventsFragment eventsFragment = (EventsFragment) fm.findFragmentByTag("events"); 
       PicsFragment picsFragment = (PicsFragment) fm.findFragmentByTag("pics"); 
       NewsFragment newsFragment = (NewsFragment) fm.findFragmentByTag("news"); 
       android.support.v4.app.FragmentTransaction ft = fm.beginTransaction(); 

       /** Detaches the homeFragment if exists */ 
       if(homeFragment!=null) 
        ft.detach(homeFragment); 

       /** Detaches the eventsFragment if exists*/ 
       if(eventsFragment!=null) 
       ft.detach(eventsFragment); 

       /** Detaches the newsFragment if exists */ 
       if(newsFragment!=null) 
        ft.detach(newsFragment); 

       /** Detaches the picsFragment if exists */ 
       if(picsFragment!=null) 
        ft.detach(picsFragment); 


       /** If current tab is android */ 
       if(tabId.equalsIgnoreCase("outdeh")){ 

        if(homeFragment==null){ 
         /** Create homeFragment and adding to fragmenttransaction */ 
         ft.add(R.id.realtabcontent,new HomeFragment(), "outdeh"); 
        }else{ 
         /** Bring to the front, if already exists in the fragmenttransaction */ 
         ft.attach(homeFragment); 
        } 

       }else if(tabId.equalsIgnoreCase("events")){ 

        if(eventsFragment==null){ 
         /** Create homeFragment and adding to fragmenttransaction */ 
         ft.add(R.id.realtabcontent,new EventsFragment(), "events"); 
        }else{ 
         /** Bring to the front, if already exists in the fragmenttransaction */ 
         ft.attach(eventsFragment); 
        } 

       }else if(tabId.equalsIgnoreCase("news")){ 

        if(newsFragment==null){ 
         /** Create homeFragment and adding to fragmenttransaction */ 
         ft.add(R.id.realtabcontent,new NewsFragment(), "news"); 
        }else{ 
         /** Bring to the front, if already exists in the fragmenttransaction */ 
         ft.attach(newsFragment); 
        } 

       }else if(tabId.equalsIgnoreCase("pics")){ 

        if(picsFragment==null){ 
         /** Create homeFragment and adding to fragmenttransaction */ 
         ft.add(R.id.realtabcontent,new PicsFragment(), "pics"); 
        }else{ 
         /** Bring to the front, if already exists in the fragmenttransaction */ 
         ft.attach(picsFragment); 
        } 

       } 
       ft.commit(); 
      } 
     }; 


     /** Setting tabchangelistener for the tab */ 
     tHost.setOnTabChangedListener(tabChangeListener); 

     /** Defining tab builder for Home tab */ 
     TabHost.TabSpec tSpecOutdeh = tHost.newTabSpec("outdeh"); 
     tSpecOutdeh.setIndicator("OutDeh",getResources().getDrawable(R.drawable.home)); 
     tSpecOutdeh.setContent(new DefinedTabContent(getBaseContext())); 
     tHost.addTab(tSpecOutdeh); 


     /** Defining tab builder for Events tab */ 
     TabHost.TabSpec tSpecEvents = tHost.newTabSpec("events"); 
     tSpecEvents.setIndicator("Upcoming Events",getResources().getDrawable(R.drawable.events)); 
     tSpecEvents.setContent(new DefinedTabContent(getBaseContext())); 
     tHost.addTab(tSpecEvents); 

     /** Defining tab builder for Pics tab */ 
     TabHost.TabSpec tSpecPics = tHost.newTabSpec("pics"); 
     tSpecPics.setIndicator("Recent Event Pictures",getResources().getDrawable(R.drawable.pics)); 
     tSpecPics.setContent(new DefinedTabContent(getBaseContext())); 
     tHost.addTab(tSpecPics); 

     /** Defining tab builder for News tab */ 
     TabHost.TabSpec tSpecNews = tHost.newTabSpec("news"); 
     tSpecNews.setIndicator("Entertainment News",getResources().getDrawable(R.drawable.news)); 
     tSpecNews.setContent(new DefinedTabContent(getBaseContext())); 
     tHost.addTab(tSpecNews); 

    } 

} 

//下一個類下面


當我運行它,我得到以下錯誤。

inconvertible types 
found : android.support.v4.app.Fragment 
required: com.outdeh.EventsFragment 


detach(android.support.v4.app.Fragment) in android.support.v4.app.FragmentTransaction cannot be applied to (com.outdeh.EventsFragment) 

cannot find symbol method add(int,com.outdeh.EventsFragment,java.lang.String) 

attach(android.support.v4.app.Fragment) in android.support.v4.app.FragmentTransaction cannot be applied to (com.outdeh.EventsFragment) 
+0

你的'EventsFragment'擴展'Fragments' – mzzzzb

+0

你最好在這裏發佈你的代碼。所以這很容易幫助 – virtualpathum

回答

1

這意味着你的EventsFragment需要實現FragmentTransaction:

考慮您的例外:attach(android.support.v4.app.Fragment) in android.support.v4.app.FragmentTransaction cannot be (com.outdeh.EventsFragment)

可能使用getFragmentManager(),更改爲getSupportFragmentManager()

0

通常,這樣的錯誤造成的,當你不在所有碎片中使用支持庫。

說到這一點,請檢查並確保所有對Fragment,FragmentManager和FragmentTransaction的引用僅來自支持庫,而不是其他引用。另外,你對FragmentManager的調用應該使用getSupportFragmentManager()。

這應該可以解決問題。如果不是這種情況,並且您正在使用正確的導入,那麼請發佈您的代碼。