问题
I am trying to use fragments to make swiped tabs by tablayouts (one activity with three tabs), I made three Java files for the tree tabs and the corresponding xml files as the below tutorial and it is working and I had the three tabs. but I stuck now in how to apply any code for any object inside these tabs .. should it be in the tab java file?
if yes, the onCreateView method isn't accepting (findviewbyid) and I tried to add onCreate method but it is not accepting (setContentView)
I also tried to add the code in the main activity class but when I it returns the below error:
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
where should I set my objects and apply my code for any tab?
the tutorial:
http://www.truiton.com/2015/06/android-tabs-example-fragments-viewpager/
Edit Adding codes:
Fragment Java Code:
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TextView;
import android.widget.Toast;
import java.util.Calendar;
public class DCOGeneralReports extends Fragment {
private DatePicker datePicker;
private Calendar calendar;
private TextView EndDate;
private int year, month, day;
private Button DailyReportGenerate, CustomReportGenerate, StartDateSelect, EndDateSelect;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.dco_general_report, container, false);
TextView StartDate = (TextView) view.findViewById(R.id.SDate);
return view;
}
Fragment XML Code:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Daily Report"
android:textColor="#1B5E20"
android:id="@+id/textView6"
android:layout_marginTop="27dp"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:layout_marginStart="25dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Press Generate to get DCO Today Report"
android:id="@+id/textView9"
android:layout_below="@+id/textView6"
android:layout_marginTop="10dp"
android:layout_centerHorizontal="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Generate"
android:textColor="#1B5E20"
android:id="@+id/DailyReport"
android:layout_below="@+id/textView9"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Custom Report"
android:textColor="#1B5E20"
android:id="@+id/textView10"
android:layout_below="@+id/DailyReport"
android:layout_alignStart="@+id/textView6"
android:layout_marginTop="23dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Select Start And End Date Then Press Select"
android:id="@+id/textView11"
android:layout_below="@+id/textView10"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Start Date: "
android:id="@+id/textView12"
android:layout_below="@+id/textView11"
android:layout_alignStart="@+id/textView10"
android:layout_marginTop="25dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="End Date"
android:id="@+id/textView13"
android:layout_below="@+id/textView12"
android:layout_alignStart="@+id/textView12"
android:layout_marginTop="20dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="SDate"
android:id="@+id/SDate"
android:layout_alignTop="@+id/textView12"
android:layout_toEndOf="@+id/textView6" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="EDate"
android:id="@+id/EDate"
android:layout_centerVertical="true"
android:layout_below="@+id/textView12"
android:layout_marginStart="143dp"
android:layout_marginTop="20dp"/>
<Button
android:layout_width="75dp"
android:layout_height="40dp"
android:text="Select"
android:id="@+id/SelectStartDate"
android:layout_below="@+id/textView11"
android:layout_marginTop="16dp"
android:layout_toEndOf="@+id/DailyReport" />
<Button
android:layout_width="75dp"
android:layout_height="40dp"
android:text="Select"
android:id="@+id/SelectEndDate"
android:layout_below="@+id/SelectStartDate"
android:layout_alignStart="@+id/SelectStartDate" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Generate"
android:textColor="#1B5E20"
android:id="@+id/CustomReport"
android:layout_marginTop="24dp"
android:layout_below="@+id/SelectEndDate"
android:layout_margin="10dp"
android:layout_centerHorizontal="true"
/>
</RelativeLayout>
回答1:
You can not call setContentView in Fragment. You should override the onCreatView method of fragment and inflate your layout as follows:
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v= inflater.inflate(R.layout.tab_fragment_2, container, false);
//to get view like textview, button etc use v.findViewById(yourid)
TextView textView = v.findViewById(R.id.textView2);
return v;
}
}.
回答2:
For a Fragment
you
- override
onCreateView
- Inflate a view
- use
findViewById
on the view reference return the view
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.my_fragment, container, false); Toolbar toolbar = (Toolbar) view.findViewById(R.id.toolbar); ... return view; }
来源:https://stackoverflow.com/questions/37326999/android-fragment-isnt-accepting-setcontentview