我正在尝试在导航抽屉中创建可滚动的选项卡,但viewPager.setAdapter(新的MyAdapter(fragmentManager))返回null。
总结了这种情况。
这是我的代码
tab_container.java
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
public class tab_container extends Fragment {
public static tab_container newInstance() {
tab_container fragment = new tab_container();
return fragment;
}
public tab_container() {
// Required empty public constructor
}
ViewPager viewPager = null;
ArrayList<String> lessonList = new ArrayList<>();
FragmentManager fragmentManager;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.fragment_tab_container, container, false);
viewPager = (ViewPager) getActivity().findViewById(R.id.lessonPager);
fragmentManager = getActivity().getSupportFragmentManager();
viewPager.setAdapter(new MyAdapter(fragmentManager));
new lessonTab().execute();
return rootView;
}
class MyAdapter extends FragmentPagerAdapter {
public MyAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
Fragment objFragment = null;
switch (position) {
case 0:
objFragment = tab1.newInstance();
break;
case 1:
objFragment = tab2.newInstance();
break;
case 2:
objFragment = tab3.newInstance();
break;
}
return objFragment;
}
@Override
public int getCount() {
return 3;
}
@Override
public CharSequence getPageTitle(int position) {
String title = new String();
if (position == 0) {
title = "Lesson";
}
return title;
}
}
class lessonTab extends AsyncTask<String, String, String> {
@Override
protected void onPreExecute() {
}
@Override
protected String doInBackground(String... strings) {
BufferedReader reader = null;
try {
URL user_login_url = new URL("http://192.168.1.110/thermodynamics/index.php/users/get_lessons");
HttpURLConnection con = (HttpURLConnection)user_login_url.openConnection();
StringBuilder sb = new StringBuilder();
reader = new BufferedReader(new InputStreamReader(con.getInputStream()));
String lessonResponse;
while ((lessonResponse = reader.readLine()) != null) {
return lessonResponse;
}
return sb.toString();
} catch (IOException e) {
e.printStackTrace();
return null;
} finally {
if(reader != null) {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
}
}
@Override
protected void onPostExecute(String lessonResponse) {
try {
JSONObject lessonObject = new JSONObject(lessonResponse);
JSONArray lessonArray = lessonObject.getJSONArray("data");
for (int i = 0; i < lessonArray.length(); i++) {
JSONObject dataLessons = lessonArray.getJSONObject(i);
lessonList.add(dataLessons.getString("lesson_title"));
}
} catch (JSONException e) {
e.printStackTrace();
}
}
} // end of asynctask
} // end of fragment
fragment_tab_container.xml
<android.support.v4.view.ViewPager
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:id="@+id/lessonPager" >
<android.support.v4.view.PagerTitleStrip
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:id="@+id/title"
android:background="#33B5E5"
android:layout_gravity="top"
android:paddingTop="4dp"
android:paddingBottom="4dp">
</android.support.v4.view.PagerTitleStrip>
</android.support.v4.view.ViewPager>
错误日志
09-29 05:11:22.453 21461-21461/thesis.thermodynamics.tip.app E/AndroidRuntime: FATAL EXCEPTION: main
09-29 05:11:22.453 21461-21461/thesis.thermodynamics.tip.app E/AndroidRuntime: Process: thesis.thermodynamics.tip.app, PID: 21461
09-29 05:11:22.453 21461-21461/thesis.thermodynamics.tip.app E/AndroidRuntime: java.lang.NullPointerException
09-29 05:11:22.453 21461-21461/thesis.thermodynamics.tip.app E/AndroidRuntime: at thesis.thermodynamics.tip.app.tab_container.onCreateView(tab_container.java:67)
09-29 05:11:22.453 21461-21461/thesis.thermodynamics.tip.app E/AndroidRuntime: at android.support.v4.app.Fragment.performCreateView(Fragment.java:1789)
09-29 05:11:22.453 21461-21461/thesis.thermodynamics.tip.app E/AndroidRuntime: at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:955)
09-29 05:11:22.453 21461-21461/thesis.thermodynamics.tip.app E/AndroidRuntime: at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1138)
09-29 05:11:22.453 21461-21461/thesis.thermodynamics.tip.app E/AndroidRuntime: at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:740)
09-29 05:11:22.453 21461-21461/thesis.thermodynamics.tip.app E/AndroidRuntime: at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1501)
09-29 05:11:22.453 21461-21461/thesis.thermodynamics.tip.app E/AndroidRuntime: at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:458)
09-29 05:11:22.453 21461-21461/thesis.thermodynamics.tip.app E/AndroidRuntime: at android.os.Handler.handleCallback(Handler.java:733)
09-29 05:11:22.453 21461-21461/thesis.thermodynamics.tip.app E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:95)
09-29 05:11:22.453 21461-21461/thesis.thermodynamics.tip.app E/AndroidRuntime: at android.os.Looper.loop(Looper.java:136)
09-29 05:11:22.453 21461-21461/thesis.thermodynamics.tip.app E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5001)
09-29 05:11:22.453 21461-21461/thesis.thermodynamics.tip.app E/AndroidRuntime: at java.lang.reflect.Method.invokeNative(Native Method)
09-29 05:11:22.453 21461-21461/thesis.thermodynamics.tip.app E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:515)
09-29 05:11:22.453 21461-21461/thesis.thermodynamics.tip.app E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
09-29 05:11:22.453 21461-21461/thesis.thermodynamics.tip.app E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
09-29 05:11:22.453 21461-21461/thesis.thermodynamics.tip.app E/AndroidRuntime: at dalvik.system.NativeStart.main(Native Method)
非常感谢!
答案 0 :(得分:2)
您的viewpager为null。您无法使用活动作为上下文找到它,因为您当前正在创建视图。
而不是
viewPager = (ViewPager) getActivity().findViewById(R.id.lessonPager);
尝试在充气之前找到视图:
viewPager = (ViewPager) rootView.findViewById(R.id.lessonPager);
答案 1 :(得分:1)
ViewPager属于您在片段中充气的视图。用这个来寻找它。
更改
end
与
viewPager = (ViewPager) getActivity().findViewById(R.id.lessonPager);
fragmentManager = getActivity().getSupportFragmentManager();
viewPager.setAdapter(new MyAdapter(fragmentManager));
您不能使用viewPager = (ViewPager) view.findViewById(R.id.lessonPager);
viewPager.setAdapter(new MyAdapter(getFragmentManager()));
来检索getActivity()
。