片段不膨胀和应用程序关闭

时间:2017-03-23 20:34:46

标签: java android android-fragments tabs

我已经实现了Android标签布局。我创建了五个名为one_fragment的片段,两个片段....五个片段。

我的XML名称five_fragment.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.unss.pa.bdapp.one_fragment"
    android:background="#FFFFFF">

    <ScrollView
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">

        <LinearLayout
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:weightSum="1"
            android:paddingRight="15dp"
            android:paddingLeft="15dp">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Give your feedback to improve this app better."
                android:textSize="20dp"
                android:textStyle="bold"
                android:textColor="#800000"
                android:typeface="serif"
                android:layout_marginTop="20dp"
                android:id="@+id/textView05"
                android:textAlignment="center"/>

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Your name"
                android:paddingLeft="10dp"
                android:paddingRight="10dp"
                android:textSize="20dp"
                android:typeface="serif"
                android:textColor="#000000"
                android:layout_marginTop="20dp"
                android:textAlignment="center"
                android:id="@+id/textView01" />

            <EditText
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:inputType="textPersonName"
                android:text=""
                android:ems="10"
                android:background="@drawable/edit_text"
                android:id="@+id/editText01"/>

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="\nYour email*"
                android:paddingLeft="10dp"
                android:paddingRight="10dp"
                android:textSize="20dp"
                android:typeface="serif"
                android:textColor="#000000"
                android:layout_marginTop="20dp"
                android:textAlignment="center"
                android:id="@+id/textView02" />

            <EditText
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:inputType="textEmailAddress"
                android:ems="10"
                android:background="@drawable/edit_text"
                android:id="@+id/editText2" />

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="\nYour feedback"
                android:paddingLeft="10dp"
                android:paddingRight="10dp"
                android:textSize="20dp"
                android:typeface="serif"
                android:textColor="#000000"
                android:layout_marginTop="20dp"
                android:id="@+id/textView03"
                android:textAlignment="center"/>

            <EditText
                android:layout_width="match_parent"
                android:layout_height="175dp"
                android:inputType="textMultiLine"
                android:ems="10"
                android:id="@+id/editText3"
                android:layout_weight="0.23"
                android:background="@drawable/edit_text"/>

            <Button
                android:layout_width="100dp"
                android:layout_height="100dp"
                android:background="@drawable/send"
                android:id="@+id/button01f"
                android:layout_gravity="center_horizontal"/>

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="\n"
                android:paddingLeft="10dp"
                android:paddingRight="10dp"
                android:textSize="2dp"
                android:typeface="serif"
                android:textColor="#000000"    
                android:layout_marginTop="20dp"
                android:id="@+id/textView07" />

        </LinearLayout>
    </ScrollView>
</RelativeLayout>

我的主要活动中的适配器

private void setupViewPager(ViewPager viewPager) {
        Adapter adapter = new Adapter(getSupportFragmentManager());
        adapter.addFragment(new ListContentFragment(), "Screen1");
        adapter.addFragment(new one_fragment(), "Screen2");
        adapter.addFragment(new CardContentFragment(), "Screen3");
        adapter.addFragment(new two_fragment(), "Screen4");

        adapter.addFragment(new CardContentFragment(), "Screen5");
        adapter.addFragment(new TileContentFragment(), "Screen6");
        adapter.addFragment(new three_fragment(), "Screen7");
        adapter.addFragment(new five_fragment(), "Feedback");

        viewPager.setAdapter(adapter);
}

我的five_fragment.java

public class five_feedback extends Fragment implements View.OnClickListener{
  // TODO: Rename parameter arguments, choose names that match
  // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER

  public Button btn;
  public EditText t1;
  public EditText t2;
  public EditText t3;

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


  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
  }

  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container,
                           Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    btn = (Button) getView().findViewById(R.id.button01f);
    t1 =  (EditText) getView().findViewById(R.id.editText01) ;
    t2 =  (EditText) getView().findViewById(R.id.editText2) ;
    t3 =  (EditText) getView().findViewById(R.id.editText3) ;

    btn.setOnClickListener(this);

    return inflater.inflate(R.layout.five_fragment, container, false);

  }

  @Override
  public void onClick(View v) {
    if( t2.getText().toString().trim().equals("")){

      Snackbar.make(v, "Email Required",
              Snackbar.LENGTH_LONG).show();

    } else {
      String to = "pbharadwaj777@gmail.com";
      String message = t2.getText().toString();
      String subject = "Feedback";
      Intent mEmail = new Intent(Intent.ACTION_SEND);
      mEmail.putExtra(Intent.EXTRA_EMAIL, new String[]{ to});
      mEmail.putExtra(Intent.EXTRA_SUBJECT, subject);
      mEmail.putExtra(Intent.EXTRA_TEXT, message);

      // prompts to choose email client
      mEmail.setType("message/rfc822");

      startActivity(Intent.createChooser(mEmail, "Choose an email client to send your feedback!"));
    }
  }
}

当我安装open时,我可以打开应用程序。我可以从屏幕1滑动到屏幕7.然而,当我打开反馈时,应用程序崩溃了。有什么问题?

1 个答案:

答案 0 :(得分:0)

问题是您在实际充气之前尝试访问该视图。将您的onCreateView更改为以下内容,或者您​​也可以将findViewByIdsetOnClickListener代码移至Fragment的{​​{3}}功能。

  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    // Inflate the layout for this fragment
    View view = inflater.inflate(R.layout.five_fragment, container, false);

    btn = (Button) view.findViewById(R.id.button01f);
    t1 =  (EditText) view.findViewById(R.id.editText01) ;
    t2 =  (EditText) view.findViewById(R.id.editText2) ;
    t3 =  (EditText) view.findViewById(R.id.editText3) ;

    btn.setOnClickListener(this);

    return view;

  }