调用类扩展Appcompatactivity时遇到问题

时间:2016-03-08 22:49:17

标签: java android android-fragments android-activity fragment

目前我的webview类扩展了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 个答案:

答案 0 :(得分: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()方法。你不需要那个活动。

使用PostViewActivity onCreate()方法:

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

然后离开。