我正在尝试使用片段通过tablayouts制作滑动标签(一个活动有三个标签),我为树标签创建了三个Java文件,并且相应的xml文件作为下面的教程,它正在工作,我有三个标签。但我现在坚持如何为这些选项卡中的任何对象应用任何代码..它应该在选项卡java文件中吗?
如果是,onCreateView方法不接受(findviewbyid),我试图添加onCreate方法,但它不接受(setContentView)
我还尝试在主活动类中添加代码,但是当它返回以下错误时:
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
我应该在哪里设置对象并将代码应用于任何标签?
教程:
http://www.truiton.com/2015/06/android-tabs-example-fragments-viewpager/
编辑添加代码:
片段Java代码:
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;
}
片段XML代码:
<?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>
答案 0 :(得分:1)
您无法在Fragment中调用setContentView。您应该覆盖片段的onCreatView方法并按如下方式扩充布局:
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;
}
}.
答案 1 :(得分:1)
对于Fragment
你
onCreateView
findViewById
返回视图
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;
}