2011-08-21 37 views
1

我想我已經完全按照它在android選項卡布局教程中所述添加了代碼,並更正了所有問題。該應用程序似乎編譯好,但是當我在模擬器上運行它時,它說'應用程序*意外停止,請重試'。這裏是我的文件,請讓我知道,如果你看到任何代碼的問題。Android教程'HelloTabWidget'

有4個單獨的活動文件。

package com.example.HelloTabWidget; 


import android.app.TabActivity; 
import android.content.Intent; 
import android.content.res.Resources; 
import android.os.Bundle; 
import android.widget.*; 

public class AlbumsActivity extends TabActivity { 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    Resources res = getResources(); // Resource object to get Drawables 
    TabHost tabHost = getTabHost(); // The activity TabHost 
    TabHost.TabSpec spec; // Resusable TabSpec for each tab 
    Intent intent; // Reusable Intent for each tab 

    // Create an Intent to launch an Activity for the tab (to be reused) 
    intent = new Intent().setClass(this, ArtistsActivity.class); 

    // Initialize a TabSpec for each tab and add it to the TabHost 
    spec = tabHost.newTabSpec("artists").setIndicator("Artists", 
         res.getDrawable(R.drawable.ic_tab_artists)) 
        .setContent(intent); 
    tabHost.addTab(spec); 

    // Do the same for the other tabs 
    intent = new Intent().setClass(this, AlbumsActivity.class); 
    spec = tabHost.newTabSpec("albums").setIndicator("Albums", 
         res.getDrawable(R.drawable.ic_tab_artists_grey)) 
        .setContent(intent); 
    tabHost.addTab(spec); 

    intent = new Intent().setClass(this, SongsActivity.class); 
    spec = tabHost.newTabSpec("songs").setIndicator("Songs", 
         res.getDrawable(R.drawable.ic_tab_artists_white)) 
        .setContent(intent); 
    tabHost.addTab(spec); 

    tabHost.setCurrentTab(2); 
} 



} 

package com.example.HelloTabWidget; 

import android.app.TabActivity; 
import android.content.Intent; 
import android.content.res.Resources; 
import android.os.Bundle; 
import android.widget.TabHost; 

public class ArtistsActivity extends TabActivity { 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    Resources res = getResources(); // Resource object to get Drawables 
    TabHost tabHost = getTabHost(); // The activity TabHost 
    TabHost.TabSpec spec; // Resusable TabSpec for each tab 
    Intent intent; // Reusable Intent for each tab 

    // Create an Intent to launch an Activity for the tab (to be reused) 
    intent = new Intent().setClass(this, ArtistsActivity.class); 

    // Initialize a TabSpec for each tab and add it to the TabHost 
    spec = tabHost.newTabSpec("artists").setIndicator("Artists", 
         res.getDrawable(R.drawable.ic_tab_artists)) 
        .setContent(intent); 
    tabHost.addTab(spec); 

    // Do the same for the other tabs 
    intent = new Intent().setClass(this, AlbumsActivity.class); 
    spec = tabHost.newTabSpec("albums").setIndicator("Albums", 
         res.getDrawable(R.drawable.ic_tab_artists_grey)) 
        .setContent(intent); 
    tabHost.addTab(spec); 

    intent = new Intent().setClass(this, SongsActivity.class); 
    spec = tabHost.newTabSpec("songs").setIndicator("Songs", 
         res.getDrawable(R.drawable.ic_tab_artists_white)) 
        .setContent(intent); 
    tabHost.addTab(spec); 

    tabHost.setCurrentTab(2); 
} 

} 


package com.example.HelloTabWidget; 

import android.app.TabActivity; 
import android.content.Intent; 
import android.content.res.Resources; 
import android.os.Bundle; 
import android.widget.TabHost; 

public class HelloTabWidget extends TabActivity { 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    Resources res = getResources(); // Resource object to get Drawables 
    TabHost tabHost = getTabHost(); // The activity TabHost 
    TabHost.TabSpec spec; // Resusable TabSpec for each tab 
    Intent intent; // Reusable Intent for each tab 

    // Create an Intent to launch an Activity for the tab (to be reused) 
    intent = new Intent().setClass(this, ArtistsActivity.class); 

    // Initialize a TabSpec for each tab and add it to the TabHost 
    spec = tabHost.newTabSpec("artists").setIndicator("Artists", 
         res.getDrawable(R.drawable.ic_tab_artists)) 
        .setContent(intent); 
    tabHost.addTab(spec); 

    // Do the same for the other tabs 
    intent = new Intent().setClass(this, AlbumsActivity.class); 
    spec = tabHost.newTabSpec("albums").setIndicator("Albums", 
         res.getDrawable(R.drawable.ic_tab_artists_grey)) 
        .setContent(intent); 
    tabHost.addTab(spec); 

    intent = new Intent().setClass(this, SongsActivity.class); 
    spec = tabHost.newTabSpec("songs").setIndicator("Songs", 
         res.getDrawable(R.drawable.ic_tab_artists_white)) 
        .setContent(intent); 
    tabHost.addTab(spec); 

    tabHost.setCurrentTab(2); 
}} 




package com.example.HelloTabWidget; 


import android.app.TabActivity; 
import android.content.Intent; 
import android.content.res.Resources; 
import android.os.Bundle; 
import android.widget.TabHost; 

public class SongsActivity extends TabActivity { 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    Resources res = getResources(); // Resource object to get Drawables 
    TabHost tabHost = getTabHost(); // The activity TabHost 
    TabHost.TabSpec spec; // Resusable TabSpec for each tab 
    Intent intent; // Reusable Intent for each tab 

    // Create an Intent to launch an Activity for the tab (to be reused) 
    intent = new Intent().setClass(this, ArtistsActivity.class); 

    // Initialize a TabSpec for each tab and add it to the TabHost 
    spec = tabHost.newTabSpec("artists").setIndicator("Artists", 
         res.getDrawable(R.drawable.ic_tab_artists)) 
        .setContent(intent); 
    tabHost.addTab(spec); 

    // Do the same for the other tabs 
    intent = new Intent().setClass(this, AlbumsActivity.class); 
    spec = tabHost.newTabSpec("albums").setIndicator("Albums", 
         res.getDrawable(R.drawable.ic_tab_artists_grey)) 
        .setContent(intent); 
    tabHost.addTab(spec); 

    intent = new Intent().setClass(this, SongsActivity.class); 
    spec = tabHost.newTabSpec("songs").setIndicator("Songs", 
         res.getDrawable(R.drawable.ic_tab_artists_white)) 
        .setContent(intent); 
    tabHost.addTab(spec); 

    tabHost.setCurrentTab(2); 
}} 

這是在res藝術家xml文件/可繪(圖像是存在於RES /抽拉以及)

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
<!-- When selected, use grey --> 
<item android:drawable="@drawable/ic_tab_artists_grey" 
     android:state_selected="true" /> 
<!-- When not selected, use white--> 
<item android:drawable="@drawable/ic_tab_artists_white" /> 
</selector> 

這是main.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> 

此是Android清單文件

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
package="com.example.HelloTabWidget" android:versionCode="1" 
android:versionName="1.0"> 
<uses-sdk android:minSdkVersion="7" /> 
<application android:icon="@drawable/icon" android:label="@string/app_name"> 
<activity android:name=".HelloTabWidget" android:label="@string/app_name" 
android:theme="@android:style/Theme.NoTitleBar"> 
    <intent-filter> 
     <action android:name="android.intent.action.MAIN" /> 
     <category android:name="android.intent.category.LAUNCHER" /> 
    </intent-filter> 
</activity> 
<activity android:name=".AlbumsActivity" android:label="@string/app_name" 
    android:theme="@android:style/Theme.NoTitleBar"> 

</activity> 
<activity android:name=".ArtistsActivity" android:label="@string/app_name" 
    android:theme="@android:style/Theme.NoTitleBar"> 

</activity> 
<activity android:name=".SongsActivity" android:label="@string/app_name" 
    android:theme="@android:style/Theme.NoTitleBar"> 

</activity> 
</application> 


</manifest> 

我已經花了很多時間去尋找錯別字,什麼也沒找到,我也花了很多年在論壇上看,沒有什麼似乎足以解決爲什麼這不起作用 - 我是新來的,雖然這可能是錯誤的,也許沒有理解答案,但我不認爲這是它。

+0

最好的辦法是給logcat輸出以及給你例外的行。 –

回答

1

目前所有的Activity.java類看起來完全一樣的HelloTabWidget.java他們可能是不應該

嘗試:

HelloTabWidget.java

package com.example.HelloTabWidget; 

import android.app.TabActivity; 
import android.content.Intent; 
import android.content.res.Resources; 
import android.os.Bundle; 
import android.widget.TabHost; 

public class HelloTabWidget extends TabActivity { 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    Resources res = getResources(); 
    TabHost tabHost = getTabHost(); 

    tabHost.addTab(tabHost.newTabSpec("artists").setIndicator("Artists",res.getDrawable(R.drawable.ic_tab_artists)).setContent(new Intent(this, ArtistsActivity.class))); 
    tabHost.addTab(tabHost.newTabSpec("albums").setIndicator("Albums",res.getDrawable(R.drawable.ic_tab_artists)).setContent(new Intent(this, AlbumsActivity.class))); 
    tabHost.addTab(tabHost.newTabSpec("songs").setIndicator("Songs",res.getDrawable(R.drawable.ic_tab_artists)).setContent(new Intent(this, SongsActivity.class))); 

    tabHost.setCurrentTab(2); 
    } 
} 

ArtistActivity.java

package com.example.HelloTabWidget; 

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


public class ArtistsActivity extends Activity { 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.artistslayout); 

    // your code for this activity ... (not the same as HelloTabWidget.java) 


    } 
} 

和其他2個Activity類一樣。

+0

添加說明:HelloTabWidget.java在您的應用程序啓動時運行,如您的AndroidManifest.xml中所定義。然後,這個HelloTabWidget.java將初始化這些選項卡並創建在不同選項卡中運行的活動(意圖)。此步驟不得在您的每項活動中重複。 – dirkboye

+0

嗨,感謝您的建議,但它沒有奏效。我爲你改變了HelloTabWIdget.java文件的建議,並將其他活動中的代碼更改爲textview,但消息仍然存在錯誤,應用程序必須強制關閉。我還用HelloTabWidget.java文件中的前一個代碼嘗試了它,並且僅用於其他活動的onCreate方法的textview,但仍然沒有運氣。你有任何進一步的建議?對不起,我是新來的。 – Wes3985

+0

我的答案中有一個小的複製和粘貼錯誤。 ArtistActivity(+2個人)必須擴展標準活動,您必須使用與主佈局不同的佈局。我已將完整的工作示例上傳到http://www.boye.ch/HelloTabExample.zip – dirkboye