getActivity 从片段返回 null
我正在为学校制作一个 Android 应用程序。对于这个应用程序,我有一个片段,它显示了来自数据库的仅包含字符串的网格视图。为此我需要一个片段。当我调用 getActivity() 时,它返回 null。 onAttach 方法,如此处建议的那样,在应用程序崩溃之前不会被调用。我该如何解决这个问题?
weekFragmetn.xml:
package nl.siebeh.schoolmate;
import java.util.ArrayList;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridView;
public class weekFragment extends Fragment {
dbLayer db;
Context mContext;
@Override
public void onAttach(Activity activity) {
mContext = getActivity();
Log.i("Schoolmate","onAttach called");
super.onAttach(activity);
}
public weekFragment newInstance(String title) {
Log.i("Schoolmate","newInstance called");
db = new dbLayer(mContext);
weekFragment fragment = new weekFragment();
ArrayList<ArrayList<Object>> result = null;
if(title == "leraren"){
result = db.getAllRowsAsArrays("weekTeacher", null);
}else if(title == "locatie"){
result = db.getAllRowsAsArrays("weekLocation", null);
}else if(title == "vakken"){
result = db.getAllRowsAsArrays("weekSubjects", null);
}
String[] array = new String[60];
for(int i = 0; i < 60; i++){
array[i] = "";
}
array[1] = getResources().getStringArray(R.array.days)[0];
array[2] = getResources().getStringArray(R.array.days)[1];
array[3] = getResources().getStringArray(R.array.days)[2];
array[4] = getResources().getStringArray(R.array.days)[3];
array[5] = getResources().getStringArray(R.array.days)[4];
for(int i = 1; i < 10; i++){
array[i] = Integer.toString(i);
}
for(int position = 0; position < result.size(); position++){
ArrayList<Object> row = result.get(position);
int hour = Integer.valueOf(row.get(1).toString()).intValue();
int day = Integer.valueOf(row.get(0).toString()).intValue();
int pos = 6 * hour + day;
array[pos] = row.get(position).toString();
}
fragment.mContent = array;
return fragment;
}
private String[] mContent = {""};
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
if (container == null) {
return null;
}
View v = inflater.inflate(R.layout.week_fragment, container, false);
GridView gridview = (GridView) v.findViewById(R.id.gridview);
gridview.setAdapter(new weekAdapter(mContext, mContent));
return gridview;
}
@Override
public boolean onContextItemSelected(MenuItem item) {
return super.onContextItemSelected(item);
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
}
}
logcat:(因为单击启动活动的列表中的项目,片段在其中启动/嵌入)
09-29 19:10:56.611: INFO/ActivityManager(70): Starting: Intent { cmp=nl.siebeh.schoolmate/.overviewWeek } from pid 536
09-29 19:10:56.970: INFO/Schoolmate(536): newInstance called
09-29 19:11:06.631: WARN/ActivityManager(70): Launch timeout has expired, giving up wake lock!
09-29 19:11:06.671: WARN/ActivityManager(70): Activity idle timeout for HistoryRecord{40770188 nl.siebeh.schoolmate/.overviewWeek}
09-29 19:12:20.661: DEBUG/SntpClient(70): request time failed: java.net.SocketException: Address family not supported by protocol
09-29 19:14:11.811: DEBUG/dalvikvm(536): Debugger has detached; object registry had 413 entries
09-29 19:14:11.811: ERROR/SchoolMate(536): java.lang.NullPointerException
09-29 19:14:11.811: INFO/SchoolMate(536): Context is null
09-29 19:14:11.820: WARN/System.err(536): java.lang.NullPointerException
09-29 19:14:11.820: WARN/System.err(536): at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:118)
09-29 19:14:11.820: WARN/System.err(536): at nl.siebeh.schoolmate.dbLayer.<init>(dbLayer.java:50)
09-29 19:14:11.820: WARN/System.err(536): at nl.siebeh.schoolmate.weekFragment.newInstance(weekFragment.java:31)
09-29 19:14:11.820: WARN/System.err(536): at nl.siebeh.schoolmate.fragmentAdapter.getItem(fragmentAdaptar.java:21)
09-29 19:14:11.820: WARN/System.err(536): at nl.siebeh.schoolmate.titleFragmentAdapter.getItem(titleFragmentAdapter.java:1)
09-29 19:14:11.820: WARN/System.err(536): at android.support.v4.app.FragmentPagerAdapter.instantiateItem(FragmentPagerAdapter.java:62)
09-29 19:14:11.820: WARN/System.err(536): at android.support.v4.view.ViewPager.addNewItem(ViewPager.java:321)
09-29 19:14:11.820: WARN/System.err(536): at android.support.v4.view.ViewPager.populate(ViewPager.java:441)
09-29 19:14:11.820: WARN/System.err(536): at android.support.v4.view.ViewPager.onAttachedToWindow(ViewPager.java:563)
09-29 19:14:11.820: WARN/System.err(536): at android.view.View.dispatchAttachedToWindow(View.java:6156)
09-29 19:14:11.820: WARN/System.err(536): at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:1122)
09-29 19:14:11.820: WARN/System.err(536): at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:1127)
09-29 19:14:11.820: WARN/System.err(536): at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:1127)
09-29 19:14:11.820: WARN/System.err(536): at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:1127)
09-29 19:14:11.820: WARN/System.err(536): at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:1127)
09-29 19:14:11.820: WARN/System.err(536): at android.view.ViewRoot.performTraversals(ViewRoot.java:765)
09-29 19:14:11.830: WARN/System.err(536): at android.view.ViewRoot.handleMessage(ViewRoot.java:1859)
09-29 19:14:11.840: WARN/System.err(536): at android.os.Handler.dispatchMessage(Handler.java:99)
09-29 19:14:11.840: WARN/System.err(536): at android.os.Looper.loop(Looper.java:123)
09-29 19:14:11.840: WARN/System.err(536): at android.app.ActivityThread.main(ActivityThread.java:3683)
09-29 19:14:11.840: WARN/System.err(536): at java.lang.reflect.Method.invokeNative(Native Method)
09-29 19:14:11.840: WARN/System.err(536): at java.lang.reflect.Method.invoke(Method.java:507)
09-29 19:14:11.850: WARN/System.err(536): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
09-29 19:14:11.850: WARN/System.err(536): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
09-29 19:14:11.850: WARN/System.err(536): at dalvik.system.NativeStart.main(Native Method)
09-29 19:14:11.850: DEBUG/AndroidRuntime(536): Shutting down VM
09-29 19:14:11.850: WARN/dalvikvm(536): threadid=1: thread exiting with uncaught exception (group=0x40015560)
09-29 19:14:11.870: ERROR/AndroidRuntime(536): FATAL EXCEPTION: main
09-29 19:14:11.870: ERROR/AndroidRuntime(536): java.lang.NullPointerException
09-29 19:14:11.870: ERROR/AndroidRuntime(536): at nl.siebeh.schoolmate.weekFragment.newInstance(weekFragment.java:49)
09-29 19:14:11.870: ERROR/AndroidRuntime(536): at nl.siebeh.schoolmate.fragmentAdapter.getItem(fragmentAdaptar.java:21)
09-29 19:14:11.870: ERROR/AndroidRuntime(536): at nl.siebeh.schoolmate.titleFragmentAdapter.getItem(titleFragmentAdapter.java:1)
09-29 19:14:11.870: ERROR/AndroidRuntime(536): at android.support.v4.app.FragmentPagerAdapter.instantiateItem(FragmentPagerAdapter.java:62)
09-29 19:14:11.870: ERROR/AndroidRuntime(536): at android.support.v4.view.ViewPager.addNewItem(ViewPager.java:321)
09-29 19:14:11.870: ERROR/AndroidRuntime(536): at android.support.v4.view.ViewPager.populate(ViewPager.java:441)
09-29 19:14:11.870: ERROR/AndroidRuntime(536): at android.support.v4.view.ViewPager.onAttachedToWindow(ViewPager.java:563)
09-29 19:14:11.870: ERROR/AndroidRuntime(536): at android.view.View.dispatchAttachedToWindow(View.java:6156)
09-29 19:14:11.870: ERROR/AndroidRuntime(536): at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:1122)
09-29 19:14:11.870: ERROR/AndroidRuntime(536): at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:1127)
09-29 19:14:11.870: ERROR/AndroidRuntime(536): at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:1127)
09-29 19:14:11.870: ERROR/AndroidRuntime(536): at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:1127)
09-29 19:14:11.870: ERROR/AndroidRuntime(536): at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:1127)
09-29 19:14:11.870: ERROR/AndroidRuntime(536): at android.view.ViewRoot.performTraversals(ViewRoot.java:765)
09-29 19:14:11.870: ERROR/AndroidRuntime(536): at android.view.ViewRoot.handleMessage(ViewRoot.java:1859)
09-29 19:14:11.870: ERROR/AndroidRuntime(536): at android.os.Handler.dispatchMessage(Handler.java:99)
09-29 19:14:11.870: ERROR/AndroidRuntime(536): at android.os.Looper.loop(Looper.java:123)
09-29 19:14:11.870: ERROR/AndroidRuntime(536): at android.app.ActivityThread.main(ActivityThread.java:3683)
09-29 19:14:11.870: ERROR/AndroidRuntime(536): at java.lang.reflect.Method.invokeNative(Native Method)
09-29 19:14:11.870: ERROR/AndroidRuntime(536): at java.lang.reflect.Method.invoke(Method.java:507)
09-29 19:14:11.870: ERROR/AndroidRuntime(536): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
09-29 19:14:11.870: ERROR/AndroidRuntime(536): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
09-29 19:14:11.870: ERROR/AndroidRuntime(536): at dalvik.system.NativeStart.main(Native Method)
09-29 19:14:11.890: WARN/ActivityManager(70): Force finishing activity nl.siebeh.schoolmate/.overviewWeek
09-29 19:14:12.413: WARN/ActivityManager(70): Activity pause timeout for HistoryRecord{40770188 nl.siebeh.schoolmate/.overviewWeek}
09-29 19:14:21.910: WARN/ActivityManager(70): Launch timeout has expired, giving up wake lock!
09-29 19:14:22.441: WARN/ActivityManager(70): Activity idle timeout for HistoryRecord{4070c7a0 nl.siebeh.schoolmate/.schoolmate}
09-29 19:14:32.451: WARN/ActivityManager(70): Activity destroy timeout for HistoryRecord{40770188 nl.siebeh.schoolmate/.overviewWeek}
For school I am making an android app. For this app I have a fragment, which shows a gridview with just strings, from a database. For this I need a fragment. When I call getActivity(), it returns null. The onAttach method, as suggested here doesn't get called before the app crashes. How should I solve this?
weekFragmetn.xml:
package nl.siebeh.schoolmate;
import java.util.ArrayList;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridView;
public class weekFragment extends Fragment {
dbLayer db;
Context mContext;
@Override
public void onAttach(Activity activity) {
mContext = getActivity();
Log.i("Schoolmate","onAttach called");
super.onAttach(activity);
}
public weekFragment newInstance(String title) {
Log.i("Schoolmate","newInstance called");
db = new dbLayer(mContext);
weekFragment fragment = new weekFragment();
ArrayList<ArrayList<Object>> result = null;
if(title == "leraren"){
result = db.getAllRowsAsArrays("weekTeacher", null);
}else if(title == "locatie"){
result = db.getAllRowsAsArrays("weekLocation", null);
}else if(title == "vakken"){
result = db.getAllRowsAsArrays("weekSubjects", null);
}
String[] array = new String[60];
for(int i = 0; i < 60; i++){
array[i] = "";
}
array[1] = getResources().getStringArray(R.array.days)[0];
array[2] = getResources().getStringArray(R.array.days)[1];
array[3] = getResources().getStringArray(R.array.days)[2];
array[4] = getResources().getStringArray(R.array.days)[3];
array[5] = getResources().getStringArray(R.array.days)[4];
for(int i = 1; i < 10; i++){
array[i] = Integer.toString(i);
}
for(int position = 0; position < result.size(); position++){
ArrayList<Object> row = result.get(position);
int hour = Integer.valueOf(row.get(1).toString()).intValue();
int day = Integer.valueOf(row.get(0).toString()).intValue();
int pos = 6 * hour + day;
array[pos] = row.get(position).toString();
}
fragment.mContent = array;
return fragment;
}
private String[] mContent = {""};
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
if (container == null) {
return null;
}
View v = inflater.inflate(R.layout.week_fragment, container, false);
GridView gridview = (GridView) v.findViewById(R.id.gridview);
gridview.setAdapter(new weekAdapter(mContext, mContent));
return gridview;
}
@Override
public boolean onContextItemSelected(MenuItem item) {
return super.onContextItemSelected(item);
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
}
}
logcat: (since click on the item the item in a list that starts the activity, where the fragment is started/embedded)
09-29 19:10:56.611: INFO/ActivityManager(70): Starting: Intent { cmp=nl.siebeh.schoolmate/.overviewWeek } from pid 536
09-29 19:10:56.970: INFO/Schoolmate(536): newInstance called
09-29 19:11:06.631: WARN/ActivityManager(70): Launch timeout has expired, giving up wake lock!
09-29 19:11:06.671: WARN/ActivityManager(70): Activity idle timeout for HistoryRecord{40770188 nl.siebeh.schoolmate/.overviewWeek}
09-29 19:12:20.661: DEBUG/SntpClient(70): request time failed: java.net.SocketException: Address family not supported by protocol
09-29 19:14:11.811: DEBUG/dalvikvm(536): Debugger has detached; object registry had 413 entries
09-29 19:14:11.811: ERROR/SchoolMate(536): java.lang.NullPointerException
09-29 19:14:11.811: INFO/SchoolMate(536): Context is null
09-29 19:14:11.820: WARN/System.err(536): java.lang.NullPointerException
09-29 19:14:11.820: WARN/System.err(536): at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:118)
09-29 19:14:11.820: WARN/System.err(536): at nl.siebeh.schoolmate.dbLayer.<init>(dbLayer.java:50)
09-29 19:14:11.820: WARN/System.err(536): at nl.siebeh.schoolmate.weekFragment.newInstance(weekFragment.java:31)
09-29 19:14:11.820: WARN/System.err(536): at nl.siebeh.schoolmate.fragmentAdapter.getItem(fragmentAdaptar.java:21)
09-29 19:14:11.820: WARN/System.err(536): at nl.siebeh.schoolmate.titleFragmentAdapter.getItem(titleFragmentAdapter.java:1)
09-29 19:14:11.820: WARN/System.err(536): at android.support.v4.app.FragmentPagerAdapter.instantiateItem(FragmentPagerAdapter.java:62)
09-29 19:14:11.820: WARN/System.err(536): at android.support.v4.view.ViewPager.addNewItem(ViewPager.java:321)
09-29 19:14:11.820: WARN/System.err(536): at android.support.v4.view.ViewPager.populate(ViewPager.java:441)
09-29 19:14:11.820: WARN/System.err(536): at android.support.v4.view.ViewPager.onAttachedToWindow(ViewPager.java:563)
09-29 19:14:11.820: WARN/System.err(536): at android.view.View.dispatchAttachedToWindow(View.java:6156)
09-29 19:14:11.820: WARN/System.err(536): at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:1122)
09-29 19:14:11.820: WARN/System.err(536): at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:1127)
09-29 19:14:11.820: WARN/System.err(536): at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:1127)
09-29 19:14:11.820: WARN/System.err(536): at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:1127)
09-29 19:14:11.820: WARN/System.err(536): at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:1127)
09-29 19:14:11.820: WARN/System.err(536): at android.view.ViewRoot.performTraversals(ViewRoot.java:765)
09-29 19:14:11.830: WARN/System.err(536): at android.view.ViewRoot.handleMessage(ViewRoot.java:1859)
09-29 19:14:11.840: WARN/System.err(536): at android.os.Handler.dispatchMessage(Handler.java:99)
09-29 19:14:11.840: WARN/System.err(536): at android.os.Looper.loop(Looper.java:123)
09-29 19:14:11.840: WARN/System.err(536): at android.app.ActivityThread.main(ActivityThread.java:3683)
09-29 19:14:11.840: WARN/System.err(536): at java.lang.reflect.Method.invokeNative(Native Method)
09-29 19:14:11.840: WARN/System.err(536): at java.lang.reflect.Method.invoke(Method.java:507)
09-29 19:14:11.850: WARN/System.err(536): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
09-29 19:14:11.850: WARN/System.err(536): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
09-29 19:14:11.850: WARN/System.err(536): at dalvik.system.NativeStart.main(Native Method)
09-29 19:14:11.850: DEBUG/AndroidRuntime(536): Shutting down VM
09-29 19:14:11.850: WARN/dalvikvm(536): threadid=1: thread exiting with uncaught exception (group=0x40015560)
09-29 19:14:11.870: ERROR/AndroidRuntime(536): FATAL EXCEPTION: main
09-29 19:14:11.870: ERROR/AndroidRuntime(536): java.lang.NullPointerException
09-29 19:14:11.870: ERROR/AndroidRuntime(536): at nl.siebeh.schoolmate.weekFragment.newInstance(weekFragment.java:49)
09-29 19:14:11.870: ERROR/AndroidRuntime(536): at nl.siebeh.schoolmate.fragmentAdapter.getItem(fragmentAdaptar.java:21)
09-29 19:14:11.870: ERROR/AndroidRuntime(536): at nl.siebeh.schoolmate.titleFragmentAdapter.getItem(titleFragmentAdapter.java:1)
09-29 19:14:11.870: ERROR/AndroidRuntime(536): at android.support.v4.app.FragmentPagerAdapter.instantiateItem(FragmentPagerAdapter.java:62)
09-29 19:14:11.870: ERROR/AndroidRuntime(536): at android.support.v4.view.ViewPager.addNewItem(ViewPager.java:321)
09-29 19:14:11.870: ERROR/AndroidRuntime(536): at android.support.v4.view.ViewPager.populate(ViewPager.java:441)
09-29 19:14:11.870: ERROR/AndroidRuntime(536): at android.support.v4.view.ViewPager.onAttachedToWindow(ViewPager.java:563)
09-29 19:14:11.870: ERROR/AndroidRuntime(536): at android.view.View.dispatchAttachedToWindow(View.java:6156)
09-29 19:14:11.870: ERROR/AndroidRuntime(536): at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:1122)
09-29 19:14:11.870: ERROR/AndroidRuntime(536): at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:1127)
09-29 19:14:11.870: ERROR/AndroidRuntime(536): at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:1127)
09-29 19:14:11.870: ERROR/AndroidRuntime(536): at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:1127)
09-29 19:14:11.870: ERROR/AndroidRuntime(536): at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:1127)
09-29 19:14:11.870: ERROR/AndroidRuntime(536): at android.view.ViewRoot.performTraversals(ViewRoot.java:765)
09-29 19:14:11.870: ERROR/AndroidRuntime(536): at android.view.ViewRoot.handleMessage(ViewRoot.java:1859)
09-29 19:14:11.870: ERROR/AndroidRuntime(536): at android.os.Handler.dispatchMessage(Handler.java:99)
09-29 19:14:11.870: ERROR/AndroidRuntime(536): at android.os.Looper.loop(Looper.java:123)
09-29 19:14:11.870: ERROR/AndroidRuntime(536): at android.app.ActivityThread.main(ActivityThread.java:3683)
09-29 19:14:11.870: ERROR/AndroidRuntime(536): at java.lang.reflect.Method.invokeNative(Native Method)
09-29 19:14:11.870: ERROR/AndroidRuntime(536): at java.lang.reflect.Method.invoke(Method.java:507)
09-29 19:14:11.870: ERROR/AndroidRuntime(536): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
09-29 19:14:11.870: ERROR/AndroidRuntime(536): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
09-29 19:14:11.870: ERROR/AndroidRuntime(536): at dalvik.system.NativeStart.main(Native Method)
09-29 19:14:11.890: WARN/ActivityManager(70): Force finishing activity nl.siebeh.schoolmate/.overviewWeek
09-29 19:14:12.413: WARN/ActivityManager(70): Activity pause timeout for HistoryRecord{40770188 nl.siebeh.schoolmate/.overviewWeek}
09-29 19:14:21.910: WARN/ActivityManager(70): Launch timeout has expired, giving up wake lock!
09-29 19:14:22.441: WARN/ActivityManager(70): Activity idle timeout for HistoryRecord{4070c7a0 nl.siebeh.schoolmate/.schoolmate}
09-29 19:14:32.451: WARN/ActivityManager(70): Activity destroy timeout for HistoryRecord{40770188 nl.siebeh.schoolmate/.overviewWeek}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您没有调用
getActivity()
,这使得回答问题变得困难。您将收到
NullPointerException
,因为mContext
为null
。我的猜测是mContext
为null
因为在fragmentAdapter
的代码中,您正在调用new weekFragment()
,然后对其调用newInstance()
。您可能认为自己遵循Fragment
JavaDocs 中概述的一些模式,但其中newInstance()
是一个static
方法,而不是实例方法就像你在这里一样。将几乎所有代码从
newInstance()
中取出,并将其移至onActivityCreated()
或在片段附加到活动时的某个时间,以便getActivity( )
有效。在此过程中,删除您的onAttach()
实现和mContext
数据成员,这两个成员都不需要(并且mContext
填充在非常可怕的方式)。You do not have a
getActivity()
call, which makes it difficult to answer the question.You are getting a
NullPointerException
becausemContext
isnull
. My guess is thatmContext
isnull
because, in the code tofragmentAdapter
, you are callingnew weekFragment()
, then callingnewInstance()
on it. You presumably thought you were following some patterns outlined in theFragment
JavaDocs, but in there,newInstance()
is astatic
method, not an instance method as you have it here.Take just about all of the code out of
newInstance()
and move it toonActivityCreated()
or some time when the fragment is attached to the activity sogetActivity()
works. Along the way, delete youronAttach()
implementation and themContext
data member, neither of which are needed (and themContext
is populated in a very scary way).