2014-07-03 27 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文件中,现在它不是静态的。我尝试了来自这里的所有建议,没有任何工作。 –