2016-03-08 52 views
0

目前我的webview類擴展了appcompatactivity,它用於擴展片段。 在我的方法我稱之爲片段類,如:有問題調用類擴展Appcompatactivity

@Override 
public void onPostSelected(int index) { 
    PostData data = PostDataModel.getInstance().listData.get(index); 
    FragmentManager fragmentManager = getSupportFragmentManager(); 
    PostViewFragment postViewFragment = (PostViewFragment) 
getSupportFragmentManager().findFragmentByTag("postview_fragment"); 
    if(postViewFragment == null) { 
     postViewFragment = PostViewFragment.newInstance(data.postLink); 
    } else { 
     postViewFragment.urlLink = data.postLink; 
    } 

    postViewFragment.title = data.postTitle; 
    FragmentTransaction ft = fragmentManager.beginTransaction(); 
    ft.replace(R.id.container, postViewFragment, "postview_fragment"); 
    ft.addToBackStack(null); 
    ft.commit(); 
} 

但林不知道如何調用appcompatactivty這裏擴展一個類是我的課:

public class PostViewFragment extends AppCompatActivity { 

    private VideoEnabledWebView webView; 
    private VideoEnabledWebChromeClient webChromeClient; 
    public String urlLink; 

    /** 
    * ATTENTION: This was auto-generated to implement the App Indexing API. 
    * See https://g.co/AppIndexing/AndroidStudio for more information. 
    */ 
    private GoogleApiClient client; 

    public static PostViewFragment Instance(String posturl) { 
     PostViewFragment fragment = new PostViewFragment(); 

     /* 
     Bundle args = new Bundle(); 
     args.putString(POST_URL, posturl); 
     fragment.setArguments(args);*/ 
     fragment.urlLink = posturl; 
     return fragment; 
    } 
} 

換句話說,我不知道有什麼用取代片段管理器。每次點擊一個新帖子時,我需要一個新的posturl實例用於我的webview。

+1

爲什麼你要調用一個擴展'Activity'的'PostViewFragment'類?這是超級混亂。如果你擴展活動,那麼你的類應該被稱爲活動,你應該通過'Intent'將它當作活動,而不是通過'FragmentManager'。你不能用'FragmentManager'開始活動。 –

回答

0

onPostSelected()應該是這樣的:

public void onPostSelected(int index) { 
    PostData data = PostDataModel.getInstance().listData.get(index); 
    Intent intent = new Intent(this, PostViewActivity.class); 
    intent.putExtra("postLink", data.postLink); 
    intent.putExtra("postTitle", data.postTitle); 
    startActivity(intent); 
} 

擺脫newInstance()方法。你不需要這樣的活動。

PostViewActivityonCreate()方法:

String postLink = getIntent().getStringExtra("postLink"); 
    String postTitle = getIntent().getStringExtra("postTitle"); 

和您去。

+0

我有類似的東西,但我不習慣使用putextra謝謝你! –