2016-05-19 19 views
0

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> 
+0

Sie können setContentView nicht im Fragment aufrufen. Sie sollten die onCreateView-Methode im Fragment überschreiben und Ihr Layout aufblasen. – USKMobility

+0

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

+0

fügen Sie Ihr komplettes Crash-Log – USKMobility

Antwort

1

Sie können setContentView nicht in Fragment aufrufen. Sie sollten die onCreatView Methode des Fragments außer Kraft setzen und Ihr Layout aufblasen wie folgt:

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

hatte ich genau das gleiche, aber ich bekomme einen Fehler (java.lang.NullPointerException: Versuch, virtuelle Methode 'android.view.View android.view.View.findViewById aufzurufen (int) 'auf einem Null-Objekt Referenz) –

+0

fügen Sie Ihren Code, in Ihrer Frage – USKMobility

+0

Ich bearbeite den Beitrag und fügte beide Codes –

1

Für eine Fragment Sie

  • Überschreibung onCreateView
  • eine Referenz
  • Verwendung findViewById auf der Ansicht Ansicht aufpumpen
  • geben Sie die Ansicht

    zurück
    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; 
    } 
    
+0

Ich tat das gleiche genau, aber ich bekomme einen Fehler (java.lang.NullPointerException: Versuch, virtuelle aufzurufen Methode 'android.view.View android.view.View.findViewById (int) 'für eine Null-Objektreferenz –

+0

Sie müssen sicherstellen, dass Ihr Layout-XML ein Widget enthält, das genau die gleiche ID wie das 'findViewById'-Argument hat. Wenn Sie eine 'NullPointerException' auf' findViewById' erhalten, bedeutet dies normalerweise, dass Sie keine Ansicht mit dieser ID haben. Sie sollten sowohl Ihren Code als auch Ihr XML-Layout in Ihrer Frage veröffentlichen. –

+0

Ich habe es mit beiden Codes bearbeitet .. Ich wünschte, Sie können das Problem finden –