【问题标题】:setText in fragment from another activity not related with fragment来自与片段无关的另一个活动的片段中的 setText
【发布时间】:2016-08-05 21:20:54
【问题描述】:

我想在另一个活动的片段中设置textView,该活动不是MainActivity 有片段transaction ..

已经尝试了其他相关文章中与我的问题相关的一些方法,但出现错误..

这是我从另一个活动到 recieve 的片段内的方法

片段 A

public class FragmentA extends Fragment {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


        ProgressDialog pDialog = new ProgressDialog(getContext());
        pDialog.setCancelable(false);

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflating view layout
        View layout = inflater.inflate(R.layout.fragment_A, container, false);

        //Put Data to id fragment
        valueName = (TextView) layout.findViewById(R.id.valueNameNav);
        valueStatus = (TextView) layout.findViewById(R.id.valueStatusNav);

    }

    public void setText(String name, String status){

            valueName = (TextView) getView().findViewById(R.id.valueNameNav);
            valueName.setText(name);
            valueStatus = (TextView) getView().findViewById(R.id.valueStatusNav);
            valueStatus.setText(status);
    }
}

这就是我在活动片段中调用setText 方法的方式

String editValueName= editName.getText().toString();
String lastStatus = valueStatus.getText().toString();

FragmentA mFragment = (FragmentA )
     getSupportFragmentManager().findFragmentById(R.id.fragment_A);

mFragment.setText(editValueName, lastStatus);

但出现这样的错误

java.lang.NullPointerException: 尝试调用虚方法 'void com.example.study.fragment.fragmentA.setText(java.lang.String, java.lang.String)' 在空对象引用上

100% 确定字符串 getText 上有数据字符串

【问题讨论】:

  • 您不能为另一个活动的视图设置文本,您必须在该活动中设置文本,您可以使用意图将字符串从一个活动传递到另一个活动
  • 您无法访问不属于该活动的片段。但如果你真的想执行这样的任务。使用应该BroadcastReceiver。如果您的片段附加到活动,您应该接口到这项工作。
  • 这里的片段为空(或)片段未处于添加状态。确保您的片段已添加。
  • 哦,谢谢,我的情况是我有一个活动,这个活动用于更新名称,保存后我希望所有片段都更改为新的名称值。有 1 个片段没有改变任何东西。我想把这个值放进去。你有没有推荐相关的文章来解决我的问题?
  • 尝试此链接以获取有关如何与来自活动developer.android.com/training/basics/fragments/…的片段交互的更多信息

标签: java android android-fragments


【解决方案1】:

在您的活动中创建一个 FrameLayout,其 ID 为 container,高度宽度为 MATCH_PARENT 然后像这样在你的活动中添加片段

FragmentA newFragment = new FragmentA ();
            FragmentTransaction ft = getFragmentManager().beginTransaction();
            ft.replace(R.id.container, newFragment).commit();

然后设置你的文本

String editValueName= editName.getText().toString();
String lastStatus = valueStatus.getText().toString();
newFragment .setText(editValueName, lastStatus);

【讨论】:

  • 该片段附加在MainActivity 上,我要传递给片段_A 的数据来自UpdateActivity
  • Wrong 2nd argument type: Found 'com.example.study.fragment.fragmentA, required 'android.app.fragment' replace (int, android.app.fragment) in fragment transaction to (int, com.example.study.fragment.fragmentA)ft.replace(R.id.container, newFragment).commit(); 上显示此错误newFragment 是问题
  • 您需要在导入中使用import android.app.Fragment 而不是这个import android.support.v4.app.Fragment
【解决方案2】:

您可以使用 store SharedPreferences 来保存您的字符串并将其加载到任何地方。当然,如果您想更改您的代码。

【讨论】:

  • 已经尝试过了,但这里仍然没有更新..数据来自服务器,服务器上已更新,fragment_B/C/D 上已更新..只有 fragment_A 未更新。但是sharedPreferences 不是很推荐
【解决方案3】:

setText方法中不需要findView你应该这样做,它会工作

public class FragmentA extends Fragment {

TextView valueName,valueStatus ;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);


    ProgressDialog pDialog = new ProgressDialog(getContext());
    pDialog.setCancelable(false);

}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflating view layout
    View layout = inflater.inflate(R.layout.fragment_A, container, false);

    //Put Data to id fragment
    valueName = (TextView) layout.findViewById(R.id.valueNameNav);
    valueStatus = (TextView) layout.findViewById(R.id.valueStatusNav);

 return layout;
}

public void setText(String name, String status){

        valueName.setText(name);
        valueStatus.setText(status);
}

}

【讨论】:

  • 仍然是空对象,我应该检查字符串的值吗?我很确定另一个片段的原因已更新。只有 fragment_A 不是 recieve 来自 UpdateActivity 的更新
【解决方案4】:

尝试这样做并确保您的片段附加了活动。

   @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_grupos);

    if(savedInstanceState == null)
    {
        fragment = new FragmentA();
        fragment.setTag(R.id.myfragmentId);
        getFragmentManager().beginTransaction()
            .add(R.id.container, fragment).commit();
    }
    else
    {
        if(fragment == null)
        {
            fragment = (FragmentA) getFragmentManager().findFragmentByTag(R.id.myfragmentId);
        }
    }
}

【讨论】:

  • 是的,已附上。让我知道,在这些代码中放入哪些活动?我的片段附在MainActivity .. 我的字符串来自UpdateActivity .. 这可能吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多