2016-12-31 11 views
2

まだAndroid Studioとそのすべての作業にはまだかなり新しいです。私はフラグメントが互いに直接通信するようにしようとしているのです。ここでは、単に私のフラグメントの1つにTextViewというテキスト要素を設定しようとしています。私は何時間も探していましたが、何をすべきか分かりません。また、コードを使ってフラグメントを実装しています(FrameLayout)。ここでフラグメント内のTextViewの編集に問題があります

は、そのテキスト値Iを編集しようとしている私の断片である:

public class ReceivingFrag extends Fragment { 

TextView sender; 

public void updateText(String text) { 
    sender.setText(text); 
} 

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
    View v = inflater.inflate(R.layout.frag_sender, container, false); 
    sender = (TextView) v.findViewById(R.id.sender); 
    return v; 
} 

}

私は私の根本的な問題がgetView()と送信者の両方がNullを返すことであると信じています。フラグメントは技術的なビューではなく、ビューのレイアウトや、ViewGroupsの助けとなることも理解しています。どんな助けもありがとうございます。

役に立つかどうかわかりませんが、これはReceivingFragクラス内のupdateText()メソッドを呼び出すメソッドです。

public void sendText(String text){ 
    ReceivingFrag frag = new ReceivingFrag(); 
    getSupportFragmentManager().beginTransaction().add(R.id.receiving_container, frag).commit(); 
    getSupportFragmentManager().executePendingTransactions() 
    frag.updateText(text); 
} 

**編集: これはフラグメントを呼び出して、作成された私のMainActivityクラスです:

public class MainActivity extends AppCompatActivity implements SendingFragment.TextClicked { 

private static final String TAG = MainActivity.class.getSimpleName(); 

public final static String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE"; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    String[] myStringArray = {"Hello", "Nice To See You", "Bye"}; 

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, 
      android.R.layout.simple_list_item_1, myStringArray); 

    ListView listView = (ListView) findViewById(R.id.mobile_list); 
    listView.setAdapter(adapter); 

    sendText("Hello"); 

} 

@Override 
public void sendText(String text){ 
    ReceivingFrag frag = new ReceivingFrag(); 
    getSupportFragmentManager().beginTransaction().add(R.id.receiving_container, frag).commit(); 
    getSupportFragmentManager().executePendingTransactions(); 
    frag.updateText(text); 
}} 

**編集2:

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

<EditText android:id="@+id/edit_message" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:hint="@string/edit_message" 
    android:layout_weight="1" /> 

<Button 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:onClick="sendMessage" 
    android:text="@string/button_send"/> 

</LinearLayout> 

<ListView android:id="@+id/mobile_list" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" /> 

<FrameLayout 
    android:id="@+id/receiving_container" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"></FrameLayout></LinearLayout> 
: これはMainActivityレイアウトファイルであります

これはフラグメントのレイアウトです。

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

<TextView 
    android:id="@+id/sender" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/frag_sender" 
    android:background="@color/gray" 
    android:layout_marginLeft="10dp" 
    android:layout_marginRight="10dp"/></LinearLayout> 

ソリューション:下記のよう だから、ランタイムエラーがMainActivityクラスに

@Override 
protected void onResume() { 
    super.onResume(); 
    sendText("hello"); 
} 

を追加することで修正されました。 https://developer.android.com/guide/components/fragments.html#Lifecycle から読んだ後、私は活動が再開状態に達すると、あなたは自由に活動を再開した状態にフラグメントのライフサイクルができている間だけ、このように。活動に断片を追加したり削除することができます」文に

を考えます独立して変化する。

は、状況が原因で、エラーが最初に発生した理由を最もよく説明しています。

答えて

0

を:フラグメントトランザクションを実行して実行していることを確認し、updateTextメソッドを呼び出す前に、

public class ReceivingFrag extends Fragment { 

    private TextView sender; 

    public void updateText(String text) { 
     sender.setText(text); 
    } 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
     View v = inflater.inflate(R.layout.frag_sender, container, false); 
     sender = (TextView) v.findViewById(R.id.sender); 
     return v; 
    } 
} 

とあなたの活動でonResume()sendText()のように、

@Override 
protected void onResume() { 
    super.onResume(); 

    sendText("Hello"); 

} 

Null Pointer Exceptionを与えません。あなたがonCreate()からそれを呼び出すと、フラグメントはまだnullです。

+0

ああスナップ!それはうまくいった!ありがとう!この背後にある論理を説明できますか?私はちょうどそれぞれの呼び出しで断片の状態と、それが具体的に変わる場所についてちょっと混乱しています!私は、 'sendText()'の呼び出しを 'onCreate()'メソッド本体から削除し、あなたが言ったように 'onResume() 'に終わりました。 –

+0

私は、フラグメント/アクティビティメソッドが呼び出される特定の順序と、ビューが 'onCreateView()'で返されたときに関係していると信じています.. 'onResume()'は ' onCreate() '正直言って、私はそれを100%理解しておらず、あなたに虚偽の情報を伝えたくありませんが、私はチェックアウトしています。私が「クリック」しているものがあればお知らせします。https:/ /developer.android.com/guide/components/fragments.html#Lifecycle http://stackoverflow.com/questions/28929637/difference-and-uses-of-oncreate-oncreateview-and-onactivitycreated-in-fra –

0
これにあなたのフラグメントを変更し

:あなたの代わりに置く場合

public void sendText(String text){ 
    ReceivingFrag frag = new ReceivingFrag(); 
    getSupportFragmentManager().beginTransaction().add(R.id.receiving_container, frag).commit(); 
    getSupportFragmentManager().executePendingTransactions(); 
    frag.updateText(text); 
} 
+0

フラグメントトランザクションが実行されなかった場合にエラーが発生するのは理にかなっていますが、これは 'sender'がまだヌルであるために役立ちませんでした。 –

+0

私の電話でコードをチェックし、getSupportFragmentManager()を追加しました。executePendingTransactions();問題を解決します。 –

+0

@ KobeM.Also、Fragmentのレイアウトコードを追加してください。 –

関連する問題