1

我想在我的活动中使用底部选项卡,在我的旧活动本身具有一些功能,而不为选项卡使用单独的活动,我可以在我的活动本身中使用选项卡功能,我试过这样。

public class GinfyActivity extends Activity {


    /** Called when the activity is first created. */

    public GridviewAdapter mAdapter;
    private Spinner spinner1;
    public String selectprayer;


    ArrayList<GridviewAdapter> results = new ArrayList<GridviewAdapter>();

    private ArrayList<String> listginfy;
    private ArrayList<Integer> listimage;

    private GridView gridView;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.home_ginfy);

        TabHost mTabHost = (TabHost)findViewById(android.R.id.tabhost);

        mTabHost.addTab(mTabHost.newTabSpec("tab_test1").setIndicator("TAB 1").setContent(new Intent(GinfyActivity.this,Audioprayer.class)));
        mTabHost.addTab(mTabHost.newTabSpec("tab_test2").setIndicator("TAB 2").setContent(new Intent(GinfyActivity.this,TTSMeditation.class)));
        mTabHost.addTab(mTabHost.newTabSpec("tab_test3").setIndicator("TAB 3").setContent(new Intent(GinfyActivity.this,TTSList.class)));

        mTabHost.setCurrentTab(0);

        addListenerOnButton();
        addListenerOnSpinnerItemSelection();






        prepareList();


        // prepared arraylist and passed it to the Adapter class
   mAdapter = new GridviewAdapter(this,listginfy, listimage);

        // Set custom adapter to gridview
        gridView = (GridView) findViewById(R.id.gridView1);
        gridView.setAdapter(mAdapter);

        // Implement On Item click listener
        gridView.setOnItemClickListener(new OnItemClickListener() 
        {
            @Override
            public void onItemClick(AdapterView<?> a, View v, int position, long id) {


                if (selectprayer.equals("www.ginfy.com"))
                {

                switch(position)
                { 
                case 0:
                Intent newActivity = new Intent(GinfyActivity.this,MainActivity.class);
                startActivity(newActivity);
                break;

                case 2:
                Intent new1Activity = new Intent(GinfyActivity.this,AndroidTabLayoutActivity.class);
                startActivity(new1Activity);
                break;
                default:
                    Toast.makeText(GinfyActivity.this, "Coming Soon", Toast.LENGTH_LONG).show();
                    }
                }
                else
                {

                    switch(position)
                    {
                    case 0:
                    Intent new2Activity = new Intent(GinfyActivity.this,YourPrayerActivity.class);
                    startActivity(new2Activity);
                    break;

                    case 1:
                    Intent new3Activity = new Intent(GinfyActivity.this,AndroidTabLayoutActivity1.class);
                    startActivity(new3Activity);
                    break;

                    case 3:
                    Intent new4Activity = new Intent(GinfyActivity.this,ExperiencesActivity.class);
                    startActivity(new4Activity);
                    break;
                    default:
                        Toast.makeText(GinfyActivity.this, "Coming Soon", Toast.LENGTH_LONG).show();
                        }


                }
            }
        });
    }




    public void addListenerOnSpinnerItemSelection() {
        {
            spinner1 = (Spinner) findViewById(R.id.spinner1);
            spinner1.setOnItemSelectedListener(new OnItemSelectedListener() 
            {
                public void onItemSelected(AdapterView<?> parent, View view, int position,
                        long id) {

                    spinner1.setSelection(position);
                    //spinnerCapital.setSelection(position);

                    selectprayer = (String) spinner1.getSelectedItem();

                    //Toast.makeText(getBaseContext(), selectprayer, Toast.LENGTH_SHORT).show();
                    //String myStatesCapital = (String) spinnerCapital.getSelectedItem();



                    //tvCapital.setText("My State is " + myState + ".And its Capital is "
                            //+ myStatesCapital);

                }

                public void onNothingSelected(AdapterView<?> parent) {

                }});
        }
        }




    public void addListenerOnButton() {
        spinner1 = (Spinner) findViewById(R.id.spinner1);

    }

    public void prepareList()
    {
        listginfy = new ArrayList<String>();

        listginfy.add("Prayers");
        listginfy.add("Prayer room");
        listginfy.add("God gallery");
        listginfy.add("Experiences");


        listimage = new ArrayList<Integer>();
        listimage.add(R.drawable.ginfyprayer);
        listimage.add(R.drawable.poojaroom1);
        listimage.add(R.drawable.godsgallery);
        listimage.add(R.drawable.temp1);

    }


}

我也声明了 mylayout 中的选项卡功能,但我的活动没有打开,它显示我的活动已停止

显示 somelogcat 错误。

09-12 08:43:45.615: E/AndroidRuntime(2630): FATAL EXCEPTION: main
09-12 08:43:45.615: E/AndroidRuntime(2630): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.jsonandroid/com.example.jsonandroid.GinfyActivity}: java.lang.NullPointerException
09-12 08:43:45.615: E/AndroidRuntime(2630):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
09-12 08:43:45.615: E/AndroidRuntime(2630):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
09-12 08:43:45.615: E/AndroidRuntime(2630):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
09-12 08:43:45.615: E/AndroidRuntime(2630):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
09-12 08:43:45.615: E/AndroidRuntime(2630):     at android.os.Handler.dispatchMessage(Handler.java:99)
09-12 08:43:45.615: E/AndroidRuntime(2630):     at android.os.Looper.loop(Looper.java:137)
09-12 08:43:45.615: E/AndroidRuntime(2630):     at android.app.ActivityThread.main(ActivityThread.java:5039)
09-12 08:43:45.615: E/AndroidRuntime(2630):     at java.lang.reflect.Method.invokeNative(Native Method)
09-12 08:43:45.615: E/AndroidRuntime(2630):     at java.lang.reflect.Method.invoke(Method.java:511)
09-12 08:43:45.615: E/AndroidRuntime(2630):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
09-12 08:43:45.615: E/AndroidRuntime(2630):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
09-12 08:43:45.615: E/AndroidRuntime(2630):     at dalvik.system.NativeStart.main(Native Method)
09-12 08:43:45.615: E/AndroidRuntime(2630): Caused by: java.lang.NullPointerException
09-12 08:43:45.615: E/AndroidRuntime(2630):     at com.example.jsonandroid.GinfyActivity.onCreate(GinfyActivity.java:44)
09-12 08:43:45.615: E/AndroidRuntime(2630):     at android.app.Activity.performCreate(Activity.java:5104)
09-12 08:43:45.615: E/AndroidRuntime(2630):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
09-12 08:43:45.615: E/AndroidRuntime(2630):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
09-12 08:43:45.615: E/AndroidRuntime(2630):     ... 11 more
4

2 回答 2

2

替换这两行

public class GinfyActivity extends Activity
TabHost mTabHost = (TabHost)findViewById(android.R.id.tabhost);

有以下

public class GinfyActivity extends TabActivity
TabHost mTabHost = getTabHost();

编辑

更改要在下一行中显示的活动。

Intent intentAndroid = new Intent().setClass(this, Your_Activity.class);

如果你只想要Tab那么最好使用ActionBarTabHost 已弃用。

TabHost 的完整示例

此处使用ActionBar的是示例代码(使用 Sherlock 或 appcompat 库,用于 < API 11)

public class MainActivity extends Activity implements TabListener {
ActionBar actionBar;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    actionBar=getActionBar();
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
    actionBar.addTab(actionBar.newTab().setText("TAB 1").setTabListener(this));
    actionBar.addTab(actionBar.newTab().setText("TAB 2").setTabListener(this));
    actionBar.addTab(actionBar.newTab().setText("TAB 3").setTabListener(this));
}

@Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
    // TODO Auto-generated method stub

}

@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
    switch (tab.getPosition()) {
    case 0:
        //start activity here or toast
        Toast.makeText(getApplicationContext(), "Start First Activity", Toast.LENGTH_SHORT).show();
        break;
    case 1:
        //start activity here or toast
        Toast.makeText(getApplicationContext(), "Start Second Activity", Toast.LENGTH_SHORT).show();
        break;
    case 2:
        //start activity here or toast
        Toast.makeText(getApplicationContext(), "Start Third Activity", Toast.LENGTH_SHORT).show();
        break;

    default:
        break;
    }

}

@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
    // TODO Auto-generated method stub

}

 }
于 2013-09-12T09:01:50.823 回答
1

为了您的完整要求https://github.com/AdilSoomro/Iphone-Tab-in-Android

于 2013-09-12T13:06:48.013 回答