簡體   English   中英

來自 Fragment 的 GetText 返回 null 但 setText 有效

[英]GetText from Fragment returns null but setText works

在我的MainActivity我調用一個 Fragment 並在其活動時將布爾mFragment設置為 true。 現在我在 Fragment 中聲明的TextViews設置了一些 Text 並且它工作正常。 但是,當 Fragment 中聲明的Button調用MainActivity的方法時,突然mFragment為 false,我無法使用TextViews getText ,因為它們為空。 我不知道為什么會這樣。

主要活動

public class MainActivity extends AppCompatActivity implements SensorEventListener {

MainFragment mainFragment;
HistoryActivity historyFragment;
boolean mFragment = false;

//....

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

    mainFragment = new MainFragment();
    historyFragment = new HistoryActivity();

 if(!mFragment) {
        getMainFragment();
    }

//...SenserManager and so on

 @Override
public void onSensorChanged(SensorEvent event){
    if (mFragment) {
        float x = event.values[0];
        float y = event.values[1];
        float z = event.values[2];

        DecimalFormat df = new DecimalFormat("0.000");

        //This works constantly:
        mainFragment.textViewX.setText("X = " + df.format(x));
        mainFragment.textViewY.setText("Y = " + df.format(y));
        mainFragment.textViewZ.setText("Z = " + df.format(z));
    }
}

public void writeEntry(){ //called in Fragment
    if(mFragment) {
        //This doesn't work (isn't even called because mFragment is false)
        String x = (String) mainFragment.textViewX.getText();
        String y = (String) mainFragment.textViewY.getText();
        String z = (String) mainFragment.textViewZ.getText();
    }
}

public void getMainFragment() {
    FragmentTransaction transaction =   getFragmentManager().beginTransaction();
    transaction.replace(R.id.fragment_container, mainFragment);
    transaction.addToBackStack(null);
    transaction.commit();
    mFragment = true;
}

分段

public class MainFragment extends Fragment {

TextView textViewX;
TextView textViewY;
TextView textViewZ;
Button button;

MainActivity mainActivity = new MainActivity();

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    View view = inflater.inflate(R.layout.fragment_main, container, false);

    textViewX = (TextView) view.findViewById(R.id.textViewX);
    textViewY = (TextView) view.findViewById(R.id.textViewY);
    textViewZ = (TextView) view.findViewById(R.id.textViewZ);
    button = (Button) view.findViewById(R.id.button);

    button.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            mainActivity.writeEntry();
        }});

    return view;
    }
}

使用回調接口從片段交互到活動。 請參考。 這是一個非常推薦的方法。 http://developer.android.com/training/basics/fragments/communicating.html#Implement

你的架構有很多缺陷。 您創建一個片段實例,然后在其上調用方法。 您不能在 android 中為活動、片段、服務等組件執行此操作(您只能對 Java 類執行此操作)。 你不能像這樣簡單地創建一個片段對象。 Android 中的所有 Fragment、Activity 和 Services 都必須經過各自的生命周期,以便它們具有附加的有效上下文。

片段活動交互也應該通過接口完成。 不是通過簡單地調用不能首先創建的對象的方法。 您可能會使其正常工作,但最終它會不斷給您帶來問題。 我最近回答了這個這個有同樣問題的兩個問題。 好像很多人都有這個疑惑。

你的溝通和幾點是錯誤的。

使用以下用法,您可能想訪問當前MainAcitivity 的方法或字段。 但這不是 MainAcitivity 的當前實例。 您剛剛實例化了一個新的(也沒有活動的生命周期)

MainActivity mainActivity = new MainActivity();

所以這就是為什么 mFragment boolen 變量是假的,而其他項目也是空的,比如 textviews。

你可以參考以下鏈接

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM