2014-06-13 3 views
11

Ich versuche kaum, eine einfache Anwendung mit einem oberen Menü und einer veränderbaren Ansicht unten zu erstellen (durch Drücken der Schaltflächen im Menüfragment ändern wir die Ansicht des Fragments unten). Also, ich habe zwei Fragmente innerhalb der Hauptansicht aber beim Versuch, die Anwendung im Emulator ich wie einen Fehler auszuführen:Android SDK-Fehler: Probieren Sie eine Klasse aus, die kein Fragment ist

Cause by android.app (bla bla bla, piece of crap Eclipse doesn't even allow copying the errors): 
Trying to instantiate a class com.example.android.topmenu that is not a fragment 

Also, das ist mein XML-Layout:

main.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" > 

    <fragment 
     android:id="@+id/menuFragment" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:name="com.example.android.topmenu" > 
    </fragment> 

    <fragment 
     android:id="@+id/contentFragment" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:name="com.example.android.bottomcontent" > 
    </fragment> 

</LinearLayout> 

topmenu.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="horizontal" > 

    <Button 
     android:id="@+id/Button1" 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" /> 

</LinearLayout> 

bottom_content.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:padding="10dp" 
    android:orientation="vertical"> 

    <TextView 
     android:id="@+id/textView1" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text="@+string/content_text" /> 

</LinearLayout> 

und diese sind die Klassen für die Haupttätigkeit und die Fragmente

main_activity

package com.example.android; 

import com.example.android.R; 

import android.app.Activity; 
import android.content.Context; 
import android.os.Bundle; 

public class OLife extends Activity { 
@Override 
    public void onCreate(Bundle savedInstanceState) { 
     // The activity is being created 
     super.onCreate(savedInstanceState); 
     // Set view 
     setContentView(R.layout.main); 
    } 

    @Override 
    protected void onDestroy() { 
     super.onDestroy(); 
     // The activity is about to be destroyed. 
     super.onDestroy(); 

     // Stop method tracing that the activity started during onCreate() 
     android.os.Debug.stopMethodTracing(); 
    } 
} 

topmenu

package com.example.android; 

import android.os.Bundle; 
import android.support.v4.app.Fragment; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 

public class OLifeMenu extends Fragment { 
    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
     View view = inflater.inflate(R.layout.topmenu, container, false); 
     return view; 
    } 
} 

bottomcontent

package com.example.android; 

import android.app.Activity; importieren android.os.Bundle; importieren android.support.v4.app.Fragment; importieren android.view.LayoutInflater; importieren android.view.View; importieren android.view.ViewGroup;

public class OLifeMain extends Fragment { 
    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
     View view = inflater.inflate(R.layout.bottom_content, container, false); 
     return view; 
    } 

    @Override 
    public void onAttach(Activity activity) { 
     super.onAttach(activity); 
    } 
} 
+0

Danke, das hat es geschafft.Die API, Tutorials, Beispiele, alles sieht so durcheinander – ali

+0

ich sage es als Antwort, so dass die zukünftigen googlers es sehen werden. –

Antwort

48

Sie sollten FragmentActivity statt Activity verwenden, der, weil Sie Unterstützung verwenden Fragmente und mehrere Fragmente in Ihrer Aktivität Haupt


bearbeiten

Sie jetzt die Appcompat ist verwenden können Support-Bibliothek und erweitert AppCompatActivity, um Symbolleiste und Fragment für niedrigere API zu unterstützen.

+1

Oder verwenden Sie eines der anderen Derivate wie 'ActionBarActivity'. – faizal

+0

Danke. Ich kann mir nicht vorstellen, wie lange ich gebraucht hätte, um das zu finden. – Dabbler

1

Da Sie Fragmente in Ihrem Layout verwenden, empfehle ich Ihnen, Ihre Klasse von Fragment- oder Fragmentaktivität zu erweitern.

5

In meinem Fall ist es stellte sich heraus, war ich in onCreate in der falschen Reihenfolge Sachen tun:

setContentView(R.layout.activity_qr_code_scan); 
super.onCreate(savedInstanceState); 

statt

super.onCreate(savedInstanceState); 
setContentView(R.layout.activity_qr_code_scan); 
+0

toller Fang .. danke –

Verwandte Themen