Android Fragment不接受setcontentview [英] Android Fragment isn't accepting setcontentview

查看:132
本文介绍了Android Fragment不接受setcontentview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用片段通过tablayouts(一个活动包含三个标签)制作滑动标签,我为树形标签制作了三个Java文件,并为以下教程制作了相应的xml文件,如下所示,它可以正常工作,我有三个标签.但我现在停留在如何对这些选项卡中的任何对象应用任何代码..它应该在选项卡java文件中吗?

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?

如果是,则onCreateView方法不接受(findviewbyid),我尝试添加onCreate方法,但不接受(setContentView)

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?

教程:

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方法,并按如下所示填充布局:

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;
    }
}.

这篇关于Android Fragment不接受setcontentview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆