简体   繁体   English

从活动中调用片段方法的问题

[英]Issues with calling fragment method from activity

I have issues with fragment method called form activity. 我有称为表单活动的片段方法的问题。 For creating the fragment and calling the method I use this method situated in MainActivity.java: 为了创建片段并调用方法,我使用MainActivity.java中的以下方法:

public void createFragment (int select, String string) {
    switch (select) {
        case 1:
            ErrorFragment errorFragment = new ErrorFragment();
            getSupportFragmentManager().beginTransaction()
                    .replace(R.id.fragError, errorFragment)
                    .commit();

            errorFragment = (ErrorFragment) getSupportFragmentManager().findFragmentById(R.id.fragError);

            errorFragment.setText(string);

            break;
        case 2:
            StringFragment stringFragment = new StringFragment();
            getSupportFragmentManager().beginTransaction()
                    .replace(R.id.fragString, stringFragment)
                    .commit();

            stringFragment = (StringFragment) getSupportFragmentManager().findFragmentById(R.id.fragString);

            stringFragment.setText(string);

            break;
    }
}

The fragments have the same layout and class except for the name of textViews and layout. 片段具有相同的布局和类,但textViews和布局的名称除外。

ErrorFragment.java: ErrorFragment.java:

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class ErrorFragment extends Fragment {

    public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_error,container,false);
    }

    public void setText(String text) {
        TextView textView = (TextView) getActivity().findViewById(R.id.errorOut);
        textView.setText(text);
    }
}

fragment_error.xml fragment_error.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <TextView
        android:id="@+id/errorTitle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/error"
        android:textColor="#FF0000"
        android:textSize="25sp"
        android:gravity="center|top"/>

    <TextView
        android:id="@+id/errorOut"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:textSize="20sp"
        android:text=""
        android:gravity="center|top"/>
</LinearLayout>

createFragment() is called by some buttons createFragment()由某些按钮调用

After some tests I noticed that the fragment is created but when I call "errorFragment.setText;" 经过一些测试,我注意到该片段已创建,但是当我调用“ errorFragment.setText;”时, the app crash. 应用程序崩溃。 In the case 2 the app doesn't crash but the textview is not displayed (I noticed that if I click fast the button the textView appear for a few milliseconds) 在情况2中,应用程序不会崩溃,但不会显示textview(我注意到,如果我快速单击按钮,textView会显示几毫秒)

EDIT: I tried to do that but the app still crash 编辑:我试图这样做,但该应用程序仍然崩溃

Seem that the TextView you are trying to set text into is in the fragment layout. 似乎您要设置文本的TextView在片段布局中。 You should do it like this in both of your fragments. 您应该在两个片段中都这样做。

The way you are trying to set text into the text view, android is trying to find it in the layout of activity not in the fragment's layout. 您尝试在文本视图中设置文本的方式是,android试图在活动布局中而不是在片段布局中找到它。 That's why your app is crashing (probably NullPointerException ) 这就是您的应用崩溃的原因(可能是NullPointerException

private View view = null;
public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState) {
    view = inflater.inflate(R.layout.fragment_error,container,false);
    return view;
}

public void setText(String text) {
    TextView textView = (TextView) view.findViewById(R.id.errorOut);
    textView.setText(text);
}

I think @Rohit5k2 is right in his explanation but instead of inflating the fragment layout in a View, you should use the same layout that you have in your XML file, which is a LinearLayout in your case. 我认为@ Rohit5k2在他的解释中是正确的,但不要在View中夸大片段布局,而应使用与XML文件相同的布局,在您的情况下为LinearLayout。

So try this: 所以试试这个:

private LinearLayout layout = null;

public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState) {
    layout = (LinearLayout) inflater.inflate(R.layout.fragment_error,container,false);
    return layout;
}

public void setText(String text) {
    TextView textView = (TextView) layout.findViewById(R.id.errorOut);
    textView.setText(text);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM