将数据从一个活动发送到第二个活动的片段 [英] Send data from one activity to a fragment on a second activity

查看:67
本文介绍了将数据从一个活动发送到第二个活动的片段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个主要的活动,其中包含一个导航抽屉和几个片段,另一个活动是收集一些我存储在称为端点"的字符串中的数据.我正在尝试将字符串从辅助活动发送到主要活动的片段之一:

I have a main activity with a navigation drawer and several fragments and a secondary activity that collects some data that I store on a string called "endpoints". I'm trying to send the string from the secondary activity to one of the fragments on the main activity:

次要活动(通过意图向主要"活动发送端点"字符串):

Secondary Activity (sending "endpoint" string via intent to the Main activity):

        Intent intent = new Intent(Secondary.this,Main.class);
        intent.putExtra("endpoint",endpoint);
        startActivity(intent);

主要活动(从次要活动中获取字符串):

Main Activity (getting string from the Secondary activity):

    public void onNavigationDrawerItemSelected(int position) {
    // update the main content by replacing fragments
    FragmentManager fragmentManager = getFragmentManager();
    fragmentManager.beginTransaction()
            .replace(R.id.container, PlaceholderFragment.newInstance(position))
            .commit();
    switch (position) {
        case 0:
            Bundle args = new Bundle();
            args.putString("endpoint",getIntent().getExtras().getString("endpoint"));
            Fragment1 fragment = new Fragment1();
            fragment1.setArguments(args);
            fragmentManager.beginTransaction().replace(R.id.container, Fragment1.newInstance(position)).commit();
            break;
        case 1:
            fragmentManager.beginTransaction().replace(R.id.container, Fragment2newInstance(position)).commit();
            break;
        case 2:
            fragmentManager.beginTransaction().replace(R.id.container, Fragment3.newInstance(position)).commit();
            break;
        case 3: ...

在Fragment类上,我试图在TextView上显示数据:

And on the Fragment class I'm trying to display the data on a TextView:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    String endpoints = getActivity().getIntent().getStringExtra("endpointStr");
    View rootView = inflater.inflate(R.layout.fragment_overview, container, false);
    TextView textView = (TextView) rootView.findViewById(R.id.overviewTV);
    textView.setText(endpoint);
    return rootView;
}

这就是我得到的:

E/AndroidRuntime:致命异常:主要 进程:com.Main.app,PID:16681 java.lang.RuntimeException:无法启动活动ComponentInfo {com.Main.app/com.Main.app.Main}: android.view.InflateException:二进制XML文件第30行:错误 膨胀类片段 原因:android.view.InflateException:二进制XML文件第30行:错误放大了类片段 在android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:713) 在android.view.LayoutInflater.rInflate(LayoutInflater.java:755) 在android.view.LayoutInflater.rInflate(LayoutInflater.java:758) 在android.view.LayoutInflater.inflate(LayoutInflater.java:492) 在android.view.LayoutInflater.inflate(LayoutInflater.java:397) 在android.view.LayoutInflater.inflate(LayoutInflater.java:353) 在com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:343) 在android.app.Activity.setContentView(Activity.java:1929) 在com.Main.app.Main.onCreate(Main.java:42) 在android.app.Activity.performCreate(Activity.java:5231) 在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087) 在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2169) 在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2271) 在android.app.ActivityThread.access $ 800(ActivityThread.java:144) 在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1205) 在android.os.Handler.dispatchMessage(Handler.java:102) 在android.os.Looper.loop(Looper.java:136) 在android.app.ActivityThread.main(ActivityThread.java:5146) 在java.lang.reflect.Method.invokeNative(本机方法) 在java.lang.reflect.Method.invoke(Method.java:515) 在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:732) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:566) 在dalvik.system.NativeStart.main(本机方法)

E/AndroidRuntime﹕ FATAL EXCEPTION: main Process: com.Main.app, PID: 16681 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.Main.app/com.Main.app.Main}: android.view.InflateException: Binary XML file line #30: Error inflating class fragment Caused by: android.view.InflateException: Binary XML file line #30: Error inflating class fragment at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:713) at android.view.LayoutInflater.rInflate(LayoutInflater.java:755) at android.view.LayoutInflater.rInflate(LayoutInflater.java:758) at android.view.LayoutInflater.inflate(LayoutInflater.java:492) at android.view.LayoutInflater.inflate(LayoutInflater.java:397) at android.view.LayoutInflater.inflate(LayoutInflater.java:353) at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:343) at android.app.Activity.setContentView(Activity.java:1929) at com.Main.app.Main.onCreate(Main.java:42) at android.app.Activity.performCreate(Activity.java:5231) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2169)             at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2271)             at android.app.ActivityThread.access$800(ActivityThread.java:144)             at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1205)             at android.os.Handler.dispatchMessage(Handler.java:102)             at android.os.Looper.loop(Looper.java:136)             at android.app.ActivityThread.main(ActivityThread.java:5146)             at java.lang.reflect.Method.invokeNative(Native Method)             at java.lang.reflect.Method.invoke(Method.java:515)             at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:732)             at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:566)             at dalvik.system.NativeStart.main(Native Method)

        **Caused by: java.lang.NullPointerException**
        at com.Main.app.Main.onNavigationDrawerItemSelected(Main.java:65)
        at com.Main.app.NavigationDrawerFragment.selectItem(NavigationDrawerFragment.java:205)
        at com.Main.app.NavigationDrawerFragment.onCreate(NavigationDrawerFragment.java:79)
        at android.app.Fragment.performCreate(Fragment.java:1678)
        at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:859)
        at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1040)
        at android.app.FragmentManagerImpl.addFragment(FragmentManager.java:1142)
        at android.app.Activity.onCreateView(Activity.java:4786)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:689)

在android.view.LayoutInflater.rInflate(LayoutInflater.java:755) 在android.view.LayoutInflater.rInflate(LayoutInflater.java:758) 在android.view.LayoutInflater.inflate(LayoutInflater.java:492) 在android.view.LayoutInflater.inflate(LayoutInflater.java:397) 在android.view.LayoutInflater.inflate(LayoutInflater.java:353) 在com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:343) 在android.app.Activity.setContentView(Activity.java:1929) 在com.Main.app.Main.onCreate(Main.java:42) 在android.app.Activity.performCreate(Activity.java:5231) 在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087) 在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2169) 在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2271) 在android.app.ActivityThread.access $ 800(ActivityThread.java:144) 在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1205) 在android.os.Handler.dispatchMessage(Handler.java:102) 在android.os.Looper.loop(Looper.java:136) 在android.app.ActivityThread.main(ActivityThread.java:5146) 在java.lang.reflect.Method.invokeNative(本机方法) 在java.lang.reflect.Method.invoke(Method.java:515) 在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:732) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:566) 在dalvik.system.NativeStart.main(本机方法)

            at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)             at android.view.LayoutInflater.rInflate(LayoutInflater.java:758)             at android.view.LayoutInflater.inflate(LayoutInflater.java:492)             at android.view.LayoutInflater.inflate(LayoutInflater.java:397)             at android.view.LayoutInflater.inflate(LayoutInflater.java:353)             at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:343)             at android.app.Activity.setContentView(Activity.java:1929)             at com.Main.app.Main.onCreate(Main.java:42)             at android.app.Activity.performCreate(Activity.java:5231)             at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)             at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2169)             at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2271)             at android.app.ActivityThread.access$800(ActivityThread.java:144)             at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1205)             at android.os.Handler.dispatchMessage(Handler.java:102)             at android.os.Looper.loop(Looper.java:136)             at android.app.ActivityThread.main(ActivityThread.java:5146)             at java.lang.reflect.Method.invokeNative(Native Method)             at java.lang.reflect.Method.invoke(Method.java:515)             at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:732)             at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:566)             at dalvik.system.NativeStart.main(Native Method)

如何解决它,并将字符串从一个活动发送到另一个活动的片段?

How can I get it fixed and send the string from one activity to a fragment on another activity?

这是XML:

<!-- A DrawerLayout is intended to be used as the top-level content view using match_parent for both width and height to consume the full space available. -->
<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">
    <android.support.v4.widget.DrawerLayout
        android:id="@+id/drawer_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#ffffff"
        tools:context=".Stackerz">

        <!-- As the main content view, the view below consumes the entire
             space available using match_parent in both dimensions. -->
        <FrameLayout
            android:id="@+id/container"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            />

        <!-- android:layout_gravity="start" tells DrawerLayout to treat
             this as a sliding drawer on the left side for left-to-right
             languages and on the right side for right-to-left languages.
             If you're not building against API 17 or higher, use
             android:layout_gravity="left" instead. -->
        <!-- The drawer is given a fixed width in dp and extends the full height of
             the container. -->
        <fragment android:id="@+id/navigation_drawer"
            android:layout_width="@dimen/navigation_drawer_width"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            android:name="com.xxxxxx.app.NavigationDrawerFragment"
            tools:layout="@layout/fragment_navigation_drawer" />

    </android.support.v4.widget.DrawerLayout>
</FrameLayout>

推荐答案

好的,这就是您的Constants类的外观.

Ok, this is how your Constants class should look like.

public class Constants {

public static Constants constants=null;

public static Constants shared(){
    if (constants==null){
        constants = new Constants();
    }
    return constants;
}

public static String endpoint;

public static String getEndpoint() {
    return endpoint;
}

public static void setEndpoint(String endpoint) {
    Constants.endpoint = endpoint;
}

}

现在,在第二个活动中,执行以下操作:

Now, in the second activity, do this:

    Constants.shared().setEndpoint(endpoint);
    Intent intent = new Intent(Secondary.this,Main.class);
    startActivity(intent);

然后在您的Fragment类中,执行以下操作:

And in your Fragment class, do this:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                     Bundle savedInstanceState) {
String endpoint = Constants.shared().getEndpoint;
View rootView = inflater.inflate(R.layout.fragment_overview, container, false);
TextView textView = (TextView) rootView.findViewById(R.id.overviewTV);
textView.setText(endpoint);
return rootView;
}

这篇关于将数据从一个活动发送到第二个活动的片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆