簡體   English   中英

設置來自另一個片段onClick事件的一個片段中的textview的文本

[英]Set text for textview that is in a fragment from another fragments onClick event

我是新來的,請多多包涵。

我正在嘗試通過片段的onClick方法在textview中設置文本,但我不斷收到此錯誤:

無法從靜態上下文引用非靜態字段“ textView”

我嘗試過的所有方法都無效,因此我需要幫助。

我有這個片段,其中有一個按鈕,我想設置textView的文本:

PullFragment.java

public class PullFragment extends Fragment implements View.OnClickListener {

public PullFragment() {
    // Required empty public constructor
}

@Override
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View view =  inflater.inflate(R.layout.fragment_pull, container, false);

    //The button
    ImageButton toolbarButtonPull;
    toolbarButtonPull = view.findViewById(R.id.toolbarButtonPull);
    toolbarButtonPull.setOnClickListener(this);

    return view;
}

@Override
public void onClick (View v) {
    switch (v.getId()) {
        case R.id.toolbarButtonPull:
            // Here is the button onClick event
            break;
    }
}
}

這是另一個具有textView的片段:

PlaceholderFragment.java

public class PlaceholderFragment extends Fragment {

public TextView textView;

public PlaceholderFragment() {}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.tab_pull, container, false);
    // Here is the textView I want to change
    textView = rootView.findViewById(R.id.textView);
    return rootView;
}
}

我在onClick事件中嘗試了以下方法:

PlaceholderFragment.textView.setText("Test");

//Or trying to call public void Test which just does "textView.setText("Text");"

PlaceholderFragment.Test();

無法從靜態上下文引用非靜態字段“ textView”

PlaceholderFragment placeholderFragment = new PlaceholderFragment();
placeholderFragment.textView.setText("Text");

//Or trying to call public void Test which just does "textView.setText("Text");"

PlaceholderFragment placeholderFragment = new PlaceholderFragment();
placeholderFragment.Test();

嘗試在空對象引用上調用虛擬方法'void android.widget.EditText.setText(java.lang.CharSequence)'

也許我缺少一些簡單的東西,但是在這方面我沒有取得任何進展。

Google建議使用接口在片段之間進行通信,請參閱文檔: https : //developer.android.com/training/basics/fragments/communication

另外,我認為是一個很好的教程,使用活動的共享ViewModel在片段之間傳輸數據。

第一個問題

我在onClick事件中嘗試了以下方法:

 PlaceholderFragment.textView.setText("Test"); //Or trying to call public void Test which just does "textView.setText("Text");" PlaceholderFragment.Test(); 

無法從靜態上下文引用非靜態字段“ textView”

Non-static field 'textView' cannot be referenced from a static context錯誤Non-static field 'textView' cannot be referenced from a static context因為您必須在不實例化對象的情況下才能訪問變量或類的方法。 除非您將其設為靜態變量或靜態方法。 因此,您需要像這樣將它們設為靜態:

public class PlaceholderFragment extends Fragment {

  public static TextView textView;

  public PlaceholderFragment() {}

  ...

  public static void test() {
    ...
  }
}

第二個問題

 PlaceholderFragment placeholderFragment = new PlaceholderFragment(); placeholderFragment.textView.setText("Text"); //Or trying to call public void Test which just does "textView.setText("Text");" PlaceholderFragment placeholderFragment = new PlaceholderFragment(); placeholderFragment.Test(); 

嘗試在空對象引用上調用虛擬方法'void android.widget.EditText.setText(java.lang.CharSequence)'

錯誤: Attempt to invoke virtual method 'void android.widget.EditText.setText(java.lang.CharSequence)' on a null object reference 這是因為在您撥打以下電話時:

PlaceholderFragment placeholderFragment = new PlaceholderFragment();    
placeholderFragment.textView.setText("Text");

placeholderFragment尚未完全實例化。 這是因為Fragment創建不是一個同步過程。 因此,當您調用placeholderFragment.textView.setText("Text"); 尚未創建textView並將其綁定到片段。 您需要通過參數設置文本,或者等待直到片段完全創建。

暫無
暫無

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

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