Android 片段不接受 setcontentview
Android Fragment isn't accepting setcontentview
我正在尝试使用片段通过 tablayouts 制作滑动标签(一个 activity 有三个标签),我为树标签和相应的 xml 制作了三个 Java 文件文件如下教程,它正在工作,我有三个选项卡。但我现在坚持如何为这些选项卡内的任何对象应用任何代码......它应该在选项卡 java 文件中吗?
如果是,onCreateView 方法不接受 (findviewbyid),我尝试添加 onCreate 方法但它不接受 (setContentView)
我也尝试在主 activity class 中添加代码,但是当我添加 returns 时出现以下错误:
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>
不能在Fragment中调用setContentView。您应该覆盖 fragment 的 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;
}
}.
为了Fragment
你
- 覆盖
onCreateView
- 膨胀视图
- 在视图引用上使用
findViewById
return 观点
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;
}
我正在尝试使用片段通过 tablayouts 制作滑动标签(一个 activity 有三个标签),我为树标签和相应的 xml 制作了三个 Java 文件文件如下教程,它正在工作,我有三个选项卡。但我现在坚持如何为这些选项卡内的任何对象应用任何代码......它应该在选项卡 java 文件中吗?
如果是,onCreateView 方法不接受 (findviewbyid),我尝试添加 onCreate 方法但它不接受 (setContentView)
我也尝试在主 activity class 中添加代码,但是当我添加 returns 时出现以下错误:
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>
不能在Fragment中调用setContentView。您应该覆盖 fragment 的 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;
}
}.
为了Fragment
你
- 覆盖
onCreateView
- 膨胀视图
- 在视图引用上使用
findViewById
return 观点
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; }