2013-02-08 237 views
0

我正在創建我的應用程序中的選項卡活動。我創建了tabHost活動,但它不起作用。由於此活動應用程序正在崩潰。請幫幫我。 這裏是代碼。選項卡活動崩潰

package com.examples.deepak.locationcloaker; 

import android.app.Activity; 
import android.app.TabActivity; 
import android.content.Intent; 
import android.os.Bundle; 
import android.widget.TabHost; 
import android.widget.TabHost.TabSpec; 

public class LocationCloakerActivity extends TabActivity { 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     TabHost tabHost = getTabHost();//(TabHost)findViewById(id); 

     TabSpec tabSpec = tabHost.newTabSpec("tab1"); 
     tabSpec.setIndicator("Home"); 
     tabSpec.setContent(new Intent(this, HomeActivity.class)); 
     tabHost.addTab(tabSpec); 

     tabSpec = tabHost.newTabSpec("tab2"); 
     tabSpec.setIndicator("Search"); 
     tabSpec.setContent(new Intent(this, SearchActivity.class)); 
     tabHost.addTab(tabSpec); 
    } 
} 

和我的XML文件

<?xml version="1.0" encoding="utf-8"?> 
<TabHost xmlns:android="htp://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    android:id="@android:id/tabhost" > 

    <LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:orientation="vertical" > 


     <TabWidget 
      android:id="@android:id/tabs" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content"/> 
     <FrameLayout 
      android:id="@android:id/tabcontent" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content"/> 
     <TextView 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:text="@string/hello" /> 

    </LinearLayout> 

</TabHost> 

我的manifest.xml

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.examples.deepak.locationcloaker" 
    android:versionCode="1" 
    android:versionName="1.0" > 

    <uses-sdk android:minSdkVersion="10" /> 

    <application 
     android:icon="@drawable/ic_launcher" 
     android:label="@string/app_name" > 
     <activity 
      android:label="@string/app_name" 
      android:name=".LocationCloakerActivity" > 
      <intent-filter > 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
     <activity android:name=".HomeActivity"></activity> 
     <activity android:name=".SearchActivity"></activity> 
    </application> 

</manifest> 

登錄貓顯示

02-08 16:22:09.208: I/Process(752): Sending signal. PID: 752 SIG: 9 
02-08 16:31:28.168: D/AndroidRuntime(786): Shutting down VM 
02-08 16:31:28.168: W/dalvikvm(786): threadid=1: thread exiting with uncaught exception (group=0x40015560) 
02-08 16:31:28.198: E/AndroidRuntime(786): FATAL EXCEPTION: main 
02-08 16:31:28.198: E/AndroidRuntime(786): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.examples.deepak.locationcloaker/com.examples.deepak.locationcloaker.LocationCloakerActivity}: java.lang.RuntimeException: Binary XML file line #2: You must supply a layout_width attribute. 
02-08 16:31:28.198: E/AndroidRuntime(786): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647) 
02-08 16:31:28.198: E/AndroidRuntime(786): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663) 
02-08 16:31:28.198: E/AndroidRuntime(786): at android.app.ActivityThread.access$1500(ActivityThread.java:117) 
02-08 16:31:28.198: E/AndroidRuntime(786): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931) 
02-08 16:31:28.198: E/AndroidRuntime(786): at android.os.Handler.dispatchMessage(Handler.java:99) 
02-08 16:31:28.198: E/AndroidRuntime(786): at android.os.Looper.loop(Looper.java:130) 
02-08 16:31:28.198: E/AndroidRuntime(786): at android.app.ActivityThread.main(ActivityThread.java:3683) 
02-08 16:31:28.198: E/AndroidRuntime(786): at java.lang.reflect.Method.invokeNative(Native Method) 
02-08 16:31:28.198: E/AndroidRuntime(786): at java.lang.reflect.Method.invoke(Method.java:507) 
02-08 16:31:28.198: E/AndroidRuntime(786): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839) 
02-08 16:31:28.198: E/AndroidRuntime(786): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597) 
02-08 16:31:28.198: E/AndroidRuntime(786): at dalvik.system.NativeStart.main(Native Method) 
02-08 16:31:28.198: E/AndroidRuntime(786): Caused by: java.lang.RuntimeException: Binary XML file line #2: You must supply a layout_width attribute. 
02-08 16:31:28.198: E/AndroidRuntime(786): at android.content.res.TypedArray.getLayoutDimension(TypedArray.java:491) 
02-08 16:31:28.198: E/AndroidRuntime(786): at android.view.ViewGroup$LayoutParams.setBaseAttributes(ViewGroup.java:3598) 
02-08 16:31:28.198: E/AndroidRuntime(786): at android.view.ViewGroup$MarginLayoutParams.<init>(ViewGroup.java:3678) 
02-08 16:31:28.198: E/AndroidRuntime(786): at android.widget.FrameLayout$LayoutParams.<init>(FrameLayout.java:457) 
02-08 16:31:28.198: E/AndroidRuntime(786): at android.widget.FrameLayout.generateLayoutParams(FrameLayout.java:423) 
02-08 16:31:28.198: E/AndroidRuntime(786): at android.widget.FrameLayout.generateLayoutParams(FrameLayout.java:47) 
02-08 16:31:28.198: E/AndroidRuntime(786): at android.view.LayoutInflater.inflate(LayoutInflater.java:396) 
02-08 16:31:28.198: E/AndroidRuntime(786): at android.view.LayoutInflater.inflate(LayoutInflater.java:320) 
02-08 16:31:28.198: E/AndroidRuntime(786): at android.view.LayoutInflater.inflate(LayoutInflater.java:276) 
02-08 16:31:28.198: E/AndroidRuntime(786): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:207) 
02-08 16:31:28.198: E/AndroidRuntime(786): at android.app.Activity.setContentView(Activity.java:1657) 
02-08 16:31:28.198: E/AndroidRuntime(786): at com.examples.deepak.locationcloaker.LocationCloakerActivity.onCreate(LocationCloakerActivity.java:15) 
02-08 16:31:28.198: E/AndroidRuntime(786): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 
02-08 16:31:28.198: E/AndroidRuntime(786): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611) 
02-08 16:31:28.198: E/AndroidRuntime(786): ... 11 more 
+1

發表您的logcat與異常 – Pasha

+0

嘛!沒有logCat信息就不能說什麼。看起來你還沒有在'AndroidMainfest.xml' –

回答

0

刪除

<TextView 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:text="@string/hello" /> 

從XML文件

TabHost tabHost = getTabHost();

// Android tab 
Intent intentAndroid = new Intent().setClass(this, 
     AndroidActivity.class); 
TabSpec tabSpecAndroid = tabHost.newTabSpec("Android").setContent(
     intentAndroid); 

Intent intentApple = new Intent().setClass(this, AppleActivity.class); 
TabSpec tabSpecApple = tabHost.newTabSpec("Ae").setContent(intentApple); 

tabHost.addTab(tabSpecAndroid); 
tabHost.addTab(tabSpecApple); 
tabHost.setCurrentTab(1); 

xml文檔

<?xml version="1.0" encoding="utf-8"?> 
<TabHost xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@android:id/tabhost" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 
    <LinearLayout 
     android:orientation="vertical" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:padding="5dp"> 
     <TabWidget 
      android:id="@android:id/tabs" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" /> 
     <FrameLayout 
      android:id="@android:id/tabcontent" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:padding="5dp" /> 
    </LinearLayout> 
</TabHost> 
+0

中聲明你的一些活動謝謝,但它仍然顯示相同的錯誤。 – Deepak

+0

這是工作代碼。 – kyogs