我刚刚在我的应用程序中设置了“滑动视图+标题条”页面,但我发现onClick Listener在应用程序中不起作用。我已经将代码放在main.java页面中,但它仍然不起作用。我该如何解决?
main.java
package com.schkop.project;
import android.app.ActionBar;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.Button;
public class Swipe extends FragmentActivity {
Button bn0, bn1, bn2, bn3, bn101, bn102, bn103, bn104, bn105, bn106, bn700, bn701, bn801;
/**
33
* The {@link android.support.v4.view.PagerAdapter} that will provide
34
* fragments representing each object in a collection. We use a
35
* {@link android.support.v4.app.FragmentStatePagerAdapter} derivative,
36
* which will destroy and re-create fragments as needed, saving and
37
* restoring their state in the process. This is important to conserve
38
* memory and is a best practice when allowing navigation between objects in
39
* a potentially large collection.
40
*/
CollectionPagerAdapter mCollectionPagerAdapter;
/**
44
* The {@link android.support.v4.view.ViewPager} that will display the
45
* object collection.
46
*/
ViewPager mViewPager;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.swipe);
bn0 = (Button) findViewById(R.id.button0);
bn1 = (Button) findViewById(R.id.button1);
bn2 = (Button) findViewById(R.id.button2);
bn3 = (Button) findViewById(R.id.button3);
bn101 = (Button) findViewById(R.id.button101);
bn102 = (Button) findViewById(R.id.button102);
bn103 = (Button) findViewById(R.id.button103);
bn104 = (Button) findViewById(R.id.button104);
bn105 = (Button) findViewById(R.id.button105);
bn106 = (Button) findViewById(R.id.button106);
bn700 = (Button) findViewById(R.id.button700);
bn701 = (Button) findViewById(R.id.button701);
bn801 = (Button) findViewById(R.id.button801);
bn0.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent it1 = new Intent(getApplicationContext(), Main.class);
startActivity(it1);
}
});
bn1.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent it1 = new Intent(getApplicationContext(), Store.class);
startActivity(it1);
}
});
bn2.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent it2 = new Intent(getApplicationContext(), News_feed.class);
startActivity(it2);
}
});
bn3.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent it2 = new Intent(getApplicationContext(), About.class);
startActivity(it2);
}
});
bn101.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent it2 = new Intent(getApplicationContext(), sv_1213pj22.class);
startActivity(it2);
}
});
bn102.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent it2 = new Intent(getApplicationContext(), pg_anime.class);
startActivity(it2);
}
});
bn103.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent it2 = new Intent(getApplicationContext(), cv_1112pj09.class);
startActivity(it2);
}
});
bn104.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent it2 = new Intent(getApplicationContext(), sv_1213pj24.class);
startActivity(it2);
}
});
bn105.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent it2 = new Intent(getApplicationContext(), sv_1213pj25.class);
startActivity(it2);
}
});
bn106.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent it2 = new Intent(getApplicationContext(), sv_1213pj26.class);
startActivity(it2);
}
});
bn700.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent it2 = new Intent(getApplicationContext(), Main.class);
startActivity(it2);
}
});
bn701.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent it2 = new Intent(getApplicationContext(), programmes.class);
startActivity(it2);
}
});
bn801.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent it2 = new Intent(getApplicationContext(), cv_1213pj23.class);
startActivity(it2);
}
});
//
// Create an adapter that when requested, will return a fragment
// representing an object in
// the collection.
//
// ViewPager and its adapters use support library fragments, so we must
// use
// getSupportFragmentManager.
mCollectionPagerAdapter = new CollectionPagerAdapter(
getSupportFragmentManager());
// Set up action bar.
final ActionBar actionBar = getActionBar();
// Specify that the Home button should show an "Up" caret, indicating
// that touching the
// button will take the user one step up in the application's hierarchy.
actionBar.setDisplayHomeAsUpEnabled(true);
// Set up the ViewPager, attaching the adapter.
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mCollectionPagerAdapter);
}
/**
77
* A {@link android.support.v4.app.FragmentStatePagerAdapter} that returns a
78
* fragment representing an object in the collection.
79
*/
public class CollectionPagerAdapter extends FragmentStatePagerAdapter {
final int NUM_ITEMS = 3; // number of tabs
public CollectionPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int i) {
Fragment fragment = new TabFragment();
Bundle args = new Bundle();
args.putInt(TabFragment.ARG_OBJECT, i);
fragment.setArguments(args);
return fragment;
}
@Override
public int getCount() {
return NUM_ITEMS;
}
@Override
public CharSequence getPageTitle(int position) {
String tabLabel = null;
switch (position) {
case 0:
tabLabel = getString(R.string.tab1);
break;
case 1:
tabLabel = getString(R.string.tab2);
break;
case 2:
tabLabel = getString(R.string.tab3);
break;
}
return tabLabel;
}
}
/**
123
* A dummy fragment representing a section of the app, but that simply
124
* displays dummy text.
125
*/
public static class TabFragment extends Fragment {
public static final String ARG_OBJECT = "object";
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Bundle args = getArguments();
int position = args.getInt(ARG_OBJECT);
int tabLayout = 0;
switch (position) {
case 0:
tabLayout = R.layout.main;
break;
case 1:
tabLayout = R.layout.store;
break;
case 2:
tabLayout = R.layout.news_feed;
break;
}
View rootView = inflater.inflate(tabLayout, container, false);
return rootView;
}
}
}
main.xml中
<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v4.view.PagerTitleStrip android:id="@+id/pager_title_strip"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:background="#33b5e5"
android:textColor="#fff"
android:paddingTop="4dp"
android:paddingBottom="4dp" />
</android.support.v4.view.ViewPager>