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