从fragment到MainActivity获取textView值

时间:2016-01-15 20:27:40

标签: java android android-fragments

我正在使用片段。我在片段中有一个textview,我希望在主要活动中获得价值。

这是我的片段布局

<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/GridLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginBottom="30dp"
    android:layout_marginEnd="50dp"
    android:layout_marginStart="55dp"
    android:layout_marginTop="10dp"
    android:columnCount="20"
    android:orientation="horizontal"
    android:rowCount="16"
    tools:context="com.lipi.worldofelements.MainFragment">

    <Space
        android:layout_width="25dp"
        android:layout_height="35dp"
        android:layout_column="2"
        android:layout_row="2"
        android:background="@color/colorButtonNobelGasses" />

    <LinearLayout
        android:layout_width="505dp"
        android:layout_height="165dp"
        android:layout_column="4"
        android:layout_columnSpan="13"
        android:layout_margin="5dp"
        android:layout_row="2"
        android:layout_rowSpan="5"
        android:orientation="horizontal"

        >

        <RelativeLayout
            android:id="@+id/btnElement"
            android:layout_width="165dp"
            android:layout_height="match_parent"
            android:background="@color/colorButtonNonmetals"
            android:clickable="true"


            >

            <TextView
                android:id="@+id/txtElemSym"
                style="@style/AppTheme"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_marginBottom="25dp"
                android:clickable="true"
                android:gravity="center"
                android:linksClickable="true"
                android:onClick="btnClikcedToast"
                android:text="@string/H"
                android:textColor="#ffff"
                android:textSize="@dimen/buttonXL"

                />

我在MainActivity.java中使用此函数加载片段:

    //Set the fragment activity

        MainFragment fragment = new MainFragment();
        FragmentTransaction fragmentTransaction =
                getSupportFragmentManager().beginTransaction();
        fragmentTransaction.replace(R.id.fragment_container, fragment);
        fragmentTransaction.commit();

这是我的片段类

  public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_main, container, false);
        databaseHelper = new ElementDatabaseAdapter(getActivity());

     txtElemSym = (TextView) rootView.findViewById(R.id.txtElemSym);


     return rootView;

当我点击textview(txtElemSym)时,主要活动会运行&#34; btnClikcedToast&#34;我的MainActivity中的函数。

  public void btnClikcedToast(View view) {

        txtSymbol = (TextView) findViewById(R.id.txtElemSym);
        String ElemSymbol = (String) txtSymbol.getText();

        Bundle b;
        Intent i = new Intent(this, SingleElementData.class);
        b = ActivityOptions.makeScaleUpAnimation(view, 0, 0, view.getWidth(), view.getHeight()).toBundle();
        i.putExtra("symbol", ElemSymbol);
        startActivity(i, b);

    }

我想在此函数中获取textvalue。我收到错误&#34; NullPointerException&#34;我跑完之后。

我该怎么做?

0 个答案:

没有答案