Java 安卓碎片是';不接受setcontentview

Java 安卓碎片是';不接受setcontentview,java,android,android-fragments,android-tablayout,Java,Android,Android Fragments,Android Tablayout,我尝试使用片段通过tablayouts(一个活动有三个选项卡)创建滑动选项卡,我为树选项卡创建了三个Java文件,并在下面的教程中创建了相应的xml文件,它正在工作,我有三个选项卡。但我现在专注于如何为这些选项卡中的任何对象应用任何代码。。它应该在选项卡java文件中吗 如果是,onCreateView方法不接受(findviewbyid),我尝试添加onCreate方法,但它不接受(setContentView) 我还尝试在主活动类中添加代码,但当我添加代码时,它返回以下错误: java.la

我尝试使用片段通过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
我应该在哪里设置对象并为任何选项卡应用代码

本教程:

编辑并添加代码:

片段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>


不能在片段中调用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;
    }
}.

不能在片段中调用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;
    }
}.

对于
片段

  • 覆盖创建视图上的
    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;
    }
    

    • 对于
      片段

      • 覆盖创建视图上的
        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;
        }
        


      不能在片段中调用setContentView。您应该重写片段中的onCreateView方法并扩大布局。您的xml文件错误。使用视图组,如具有垂直方向的线性布局。并将所有视图作为线性布局的子视图。添加完整的崩溃日志我刚刚发现问题,似乎不同活动的ID中存在冲突(我在两个代码中都将其更改为ti SDate1,并已解决)。你能告诉我为什么我的XML是错误的吗?为什么有必要使其线性布局?我将其设置为相对布局。您第一次添加布局时没有任何视图组(没有相对布局),但后来添加了正确的布局。现在您的布局看起来正常,您无法在片段中调用setContentView。您应该重写片段中的onCreateView方法并扩大布局。您的xml文件错误。使用视图组,如具有垂直方向的线性布局。并将所有视图作为线性布局的子视图。添加完整的崩溃日志我刚刚发现问题,似乎不同活动的ID中存在冲突(我在两个代码中都将其更改为ti SDate1,并已解决)。你能告诉我为什么我的XML是错误的吗?为什么有必要使其线性布局?我将其设置为相对布局。您第一次添加布局时没有任何视图组(没有相对布局),但后来添加了正确的布局。现在,您的布局看起来正常。我做了完全相同的操作,但我遇到了一个错误(java.lang.NullPointerException:尝试调用虚拟方法'android.view.view android.view.view.findViewById(int))'在空对象引用上)我做了完全相同的操作,但我得到了一个错误(java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法'android.view.view android.view.view.findViewById(int)')我做了完全相同的操作,但我得到了一个错误(java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法“android.view.view android.view.view.findViewById(int)”您必须确保布局XML包含一个id与
      findViewById
      参数完全相同的小部件。当您在
      findViewById
      上获得
      NullPointerException
      时,通常意味着您没有具有该id的视图。您应该在问题中发布代码和布局XML。我编辑了它使用这两个代码..我希望您能找到问题我也这么做了,但我得到了一个错误(java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法“android.view.view android.view.view.findViewById(int)”)您必须确保布局XML包含一个id与
      findViewById
      参数完全相同的小部件。当您在
      findViewById
      上获得
      NullPointerException
      时,通常意味着您没有具有该id的视图。您应该在问题中发布代码和布局XML。我编辑了它有了这两种代码,我希望你能找到问题所在