2014-06-13 5 views
11

है, मैं शायद शीर्ष मेनू के साथ एक सरल एप्लिकेशन बनाने और नीचे एक परिवर्तनीय दृश्य बनाने की कोशिश कर रहा हूं (मेनू खंड में बटन दबाकर हम खंड के दृश्य को बदलते हैं नीचे)। तो, मैं मुख्य दृश्य के अंदर 2 टुकड़े है, लेकिन जब एमुलेटर में आवेदन मैं की तरह एक त्रुटि प्राप्त भागने की कोशिश कर:एंड्रॉइड एसडीके त्रुटि: एक वर्ग को तुरंत चालू करने का प्रयास करें जो

main.xml:

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 

तो, ये मेरी एक्सएमएल लेआउट हैं

<?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> 

और इन मुख्य गतिविधि के लिए वर्गों और टुकड़े

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

हैं 0
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; 

आयात android.app.Activity; आयात android.os.Bundle; आयात android.support.v4.app.Fragment; आयात android.view.LayoutInflater; आयात android.view.View; आयात 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

धन्यवाद इस किया था।एपीआई, ट्यूटोरियल, उदाहरण, सबकुछ इतना गड़बड़ दिखता है – ali

+0

बीमार इसे उत्तर के रूप में रखता है ताकि भविष्य के googlers इसे देख सकें। –

उत्तर

48

आप Activity है कि क्योंकि आप अपनी गतिविधि मुख्य में समर्थन टुकड़े और कई टुकड़े का उपयोग कर रहे


संपादित

अब आप Appcompat उपयोग कर सकते हैं के बजाय FragmentActivity का उपयोग करना चाहिए समर्थन पुस्तकालय और कम एपीआई के लिए टूलबार और खंड का समर्थन करने के लिए AppCompatActivity बढ़ाता है।

+1

या 'एक्शनबैरएक्टिविटी' जैसे किसी अन्य डेरिवेटिव का उपयोग करें। – faizal

+0

धन्यवाद। मैं कल्पना नहीं कर सकता कि मुझे यह पता लगाने में कितना समय लगेगा। – Dabbler

1

जैसा कि आप अपने लेआउट में टुकड़े का उपयोग कर रहे हैं और मेरा सुझाव है कि आप अपनी कक्षा को टुकड़े या टुकड़े की गतिविधि से विस्तारित करें।

5

मेरे मामले में यह पता चला मैं गलत क्रम में onCreate में सामान कर रहा था,:,

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

बजाय

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

महान पकड़ .. धन्यवाद –

संबंधित मुद्दे