2014-07-03 26 views
1

解決如何從片段編輯活動TextView的

我想新的文本設置爲TextView。我在MainActivity中有TextView的新值,但此活動不知道此TextViewTextView在文件fragment_main.xml。片段類包含在類似內部靜態類的MainActivity.java文件中。它是Eclipse中帶有片段的默認生成活動。你可以幫我嗎?

public class MainActivity extends FragmentActivity { 

    ... 

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

     ... 

     if (savedInstanceState == null) { 
      getSupportFragmentManager().beginTransaction() 
       .add(R.id.container, new PlaceholderFragment()).commit(); 
     } 
    } 

    public static class PlaceholderFragment extends Fragment { 
     public PlaceholderFragment() { } 

     @Override 
     public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
      View rootView = inflater.inflate(R.layout.fragment_main, container, false); 
      return rootView; 
     } 
    } 
} 

activity_main.xml中

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/container" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context="com.zonemedia.skener.MainActivity" 
    tools:ignore="MergeRootFrame" /> 

fragment_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/viewmoje" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:baselineAligned="false" 
    android:orientation="vertical" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    tools:context="com.zonemedia.skener.MainActivity$PlaceholderFragment" > 

    <TextView 
     android:id="@+id/texturl" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:text="@string/text_url" /> 
</LinearLayout> 

EDIT 1: 我試圖使公共方法(=設定器)的片段,並從FragmentActivity調用它。 我也嘗試直接從片段改變文本: TextView textUrl = (TextView) getView().findViewById(R.id.texturl); textCode.setText("random");

我試過了@ masmic_87寫道。在我定義的片段中 TextView fragmentTextView = (TextView)getView().findViewById(R.id.texturl);

然後在活動中我放了你的代碼。應用程序仍然會崩潰:

java.lang.RuntimeException: Unable to start activity ComponentInfo{.....MainActivity}: java.lang.NullPointerException caused by line 86: ((PlaceholderFragment) fragment).fragmentTextView.setText("something");

最後我直接嘗試調用靜態字符串從靜態的片段,但MainActivity.this是理由強調:靜磁場MainActivity.this應以靜態的方式進行訪問,並沒有外圍實例可以在範圍內訪問MainActivity類型。

我也嘗試將內部類PlaceholderFragment移動到新的.java文件,現在它不是靜態的。我嘗試了從這裏的所有建議,並沒有任何工作

求助 我不知道我的頭在哪裏。在片段我打電話(的TextView)getView() ....代替

View rootView = inflater.inflate(R.layout.fragment_main, container,false); 
    mTextView = (TextView) rootView.findViewById(R.id.texturl); 
    mTextView.setText("new text"); 
+0

你有什麼可讀的嗎? –

+0

你應該在你的活動中引用你的片段。 – Gilson

+0

我試圖在Fragment中創建公共方法(= setter),並從FragmentActivity調用它。我也嘗試直接從片段改變文本: 'TextView textUrl =(TextView)getView()。findViewById(R.id.texturl); \t \t \t textCode.setText(「random」);' –

回答

1

看一看,如果這對你的作品。

提及在活動的片段:

PlaceholderFragment fragment = new PlaceholderFragment(); 

然後定義你的片斷的TextView的你想要的文字:

((PlaceholderFragment)fragment).fragmentTextView.setText(text you want); 

這將是TextView的價值傳遞給在片段任何時候。如果你想通過這個值此刻的你撥打片段交易,這將是更好地做這種方式:

Bundle bundle = new Bundle(); 
bundle.putString("textview", "new_value"); 
PlaceholderFragment fragment= new PlaceholderFragment(); 
fragment.setArguments(bundle); 
transaction.add(R.id.container, fragment); 
transaction.commit(); 
+0

它不起作用。在片段中,我定義了'TextView fragmentTextView =(TextView)getView()。findViewById(R.id.texturl);'然後在活動中放置了您的代碼。應用仍然崩潰:'java.lang.RuntimeException:無法啓動活動ComponentInfo {..... MainActivity}:java.lang.NullPointerException' 由行86引起:((PlaceholderFragment)fragment).fragmentTextView.setText(「東西「);' –

1

如果片段是一個靜態的腸子類的活動,那麼我們爲什麼不您只需將文本存儲在活動中的靜態字符串中,然後片段將通過MainActivity.this.stringName直接訪問它。

這個解決方案會讓你的代碼具有非常高的耦合度,但是如果它的靜態內部類需要像這樣傳遞數據,那麼你並不需要擔心這一點。

+0

它不起作用。 MainActivity.this與原因一致:應該以靜態方式訪問靜態字段MainActivity.this,並且範圍內不能包含MainActivity類型的封閉實例。 我也嘗試將內部類移到新的.java文件中,現在它不是靜態的。我嘗試了來自這裏的所有建議,沒有任何工作。 –