我尝试在Android中编写代码,通过点击按钮从一个标签切换到另一个标签。
我知道通过点击标签我们可以从一个标签切换到另一个标签但是可以通过点击一个按钮从一个标签切换到另一个标签。
我尝试了以下教程。
http://www.androidhive.info/2013/10/android-tab-layout-with-swipeable-views-1/
在本教程中,我已按以下方式编辑了MovieFragment.java文件。
MovieFragment.java
package info.androidhive.tabsswipe;
import info.androidhive.tabsswipe.R;
import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
public class MoviesFragment extends Fragment {
ViewPager viewPager;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState)
{
View rootView = inflater.inflate(R.layout.fragment_movies, container, false);
Button btn = (Button) rootView.findViewById(R.id.btn);
btn.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
viewPager.setCurrentItem(0);
}
});
return rootView;
}}
XML代码:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#17df0d">
<android.support.v4.view.ViewPager
android:id="@+id/myfivepanelpager"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Design Movies Screen"
android:textSize="20dp"
android:layout_centerInParent="true"/>
<Button
android:id="@+id/btn"
android:layout_width="100dp"
android:layout_height="80dp"
android:text="Click"
/>
</RelativeLayout>
我在xml布局中创建了一个按钮,并尝试切换到标签-1,但我得到了空指针异常。
错误代码:
05-20 09:07:10.995: E/AndroidRuntime(1117): FATAL EXCEPTION: main
05-20 09:07:10.995: E/AndroidRuntime(1117): java.lang.NullPointerException
05-20 09:07:10.995: E/AndroidRuntime(1117): at info.androidhive.tabsswipe.MoviesFragment$1.onClick(MoviesFragment.java:33)
05-20 09:07:10.995: E/AndroidRuntime(1117): at android.view.View.performClick(View.java:4240)
05-20 09:07:10.995: E/AndroidRuntime(1117): at android.view.View$PerformClick.run(View.java:17721)
05-20 09:07:10.995: E/AndroidRuntime(1117): at android.os.Handler.handleCallback(Handler.java:730)
05-20 09:07:10.995: E/AndroidRuntime(1117): at android.os.Handler.dispatchMessage(Handler.java:92)
05-20 09:07:10.995: E/AndroidRuntime(1117): at android.os.Looper.loop(Looper.java:137)
05-20 09:07:10.995: E/AndroidRuntime(1117): at android.app.ActivityThread.main(ActivityThread.java:5103)
05-20 09:07:10.995: E/AndroidRuntime(1117): at java.lang.reflect.Method.invokeNative(Native Method)
05-20 09:07:10.995: E/AndroidRuntime(1117): at java.lang.reflect.Method.invoke(Method.java:525)
05-20 09:07:10.995: E/AndroidRuntime(1117): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
05-20 09:07:10.995: E/AndroidRuntime(1117): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
05-20 09:07:10.995: E/AndroidRuntime(1117): at dalvik.system.NativeStart.main(Native Method)
请给我一些解决方案或一些好的技巧。
答案 0 :(得分:23)
您必须在片段中调用父视图。
public class MoviesFragment extends Fragment {
ViewPager viewPager;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_movies, container, false);
Button btn = (Button) rootView.findViewById(R.id.btn);
viewPager = (ViewPager) getActivity().findViewById(R.id.pager);
btn.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
viewPager.setCurrentItem(0);
}
});
return rootView;
}
}
答案 1 :(得分:0)
您的ViewPager为null,因为您没有初始化变量。
你需要做
public class MoviesFragment extends Fragment {
ViewPager viewPager;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState)
{
View rootView = inflater.inflate(R.layout.fragment_movies, container, false);
Button btn = (Button) rootView.findViewById(R.id.btn);
viewPager = (ViewPager) rootView.findViewById(R.id.some_viewpager);//Initializing
btn.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
viewPager.setCurrentItem(0);
}
});
return rootView;
}}
答案 2 :(得分:-1)
viewPager = (ViewPager) getActivity().findViewById(R.id.pager);
viewPager.setCurrentItem(0);
片段内的