Ich versuche Fragmente zu verwenden, um über Tablayouts (eine Aktivität mit drei Tabs) Swiped Tabs zu machen, habe ich drei Java-Dateien für die Baumregisterkarten und die entsprechenden XML-Dateien wie das unten Tutorial und es funktioniert und ich hatte die drei Tabs. aber ich steckte jetzt darin, wie man irgendeinen Code für irgendein Objekt innerhalb dieser Tabs anwendet. Sollte es in der Tab-Java-Datei sein?Android Fragment akzeptiert keine setcontentview
wenn ja, die onCreateView Methode ist nicht zu akzeptieren (findViewById) und ich versuchte Methode hinzuzufügen onCreate aber es akzeptiert nicht (setContentView)
Ich habe auch versucht, den Code in der Hauptaktivitätsklasse hinzufügen, aber wenn I es gibt den folgenden Fehler zurück:
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
Wo sollte ich meine Objekte festlegen und meinen Code für eine Registerkarte anwenden?
das Tutorial:
http://www.truiton.com/2015/06/android-tabs-example-fragments-viewpager/
Bearbeiten Hinzufügen von Codes:
Fragment Java-Code:
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-Code:
<?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>
Sie können setContentView nicht im Fragment aufrufen. Sie sollten die onCreateView-Methode im Fragment überschreiben und Ihr Layout aufblasen. – USKMobility
Ihre XML-Datei ist falsch. Verwenden Sie eine Ansichtsgruppe wie LinearLayout mit vertikaler Ausrichtung. und legen Sie alle Ihre Ansichten als Kind des linearen Layouts. – USKMobility
fügen Sie Ihr komplettes Crash-Log – USKMobility