gpt4 book ai didi

java - Android Fragment 不接受 setcontentview

转载 作者:行者123 更新时间:2023-12-02 03:37:03 35 4
gpt4 key购买 nike

我正在尝试使用 fragment 通过选项卡布局制作滑动选项卡(一个 Activity 包含三个选项卡),我为树选项卡制作了三个 Java 文件和相应的 xml 文件,如下例所示,它正在工作,我有三个选项卡。但我现在陷入了如何为这些选项卡内的任何对象应用任何代码..它应该在选项卡java文件中吗?

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

我还尝试在主 Activity 类中添加代码,但是当我返回以下错误时:

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 代码 fragment :

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 代码:

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

关于java - Android Fragment 不接受 setcontentview,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37326999/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com