2016-03-08 115 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谢谢你! –