我在下面的Progress_Limits
中调用Fragment
方法时遇到此错误,
如何正确初始化Fragment?
logcat的
09-05 06:37:30.075: E/AndroidRuntime(8676): FATAL EXCEPTION: main
09-05 06:37:30.075: E/AndroidRuntime(8676): Process: developer.mohab.gymee, PID: 8676
09-05 06:37:30.075: E/AndroidRuntime(8676): java.lang.RuntimeException: Unable to start activity ComponentInfo{developer.mohab.gymee/developer.mohab.gymee.Cardio.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.ContentResolver android.support.v4.app.FragmentActivity.getContentResolver()' on a null object reference
09-05 06:37:30.075: E/AndroidRuntime(8676): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2345)
09-05 06:37:30.075: E/AndroidRuntime(8676): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2405)
09-05 06:37:30.075: E/AndroidRuntime(8676): at android.app.ActivityThread.access$800(ActivityThread.java:149)
09-05 06:37:30.075: E/AndroidRuntime(8676): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1324)
09-05 06:37:30.075: E/AndroidRuntime(8676): at android.os.Handler.dispatchMessage(Handler.java:102)
09-05 06:37:30.075: E/AndroidRuntime(8676): at android.os.Looper.loop(Looper.java:211)
09-05 06:37:30.075: E/AndroidRuntime(8676): at android.app.ActivityThread.main(ActivityThread.java:5333)
09-05 06:37:30.075: E/AndroidRuntime(8676): at java.lang.reflect.Method.invoke(Native Method)
09-05 06:37:30.075: E/AndroidRuntime(8676): at java.lang.reflect.Method.invoke(Method.java:372)
09-05 06:37:30.075: E/AndroidRuntime(8676): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1016)
09-05 06:37:30.075: E/AndroidRuntime(8676): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:811)
09-05 06:37:30.075: E/AndroidRuntime(8676): Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.ContentResolver android.support.v4.app.FragmentActivity.getContentResolver()' on a null object reference
09-05 06:37:30.075: E/AndroidRuntime(8676): at developer.mohab.gymee.Cardio.Progress.getCursor(Progress.java:70)
09-05 06:37:30.075: E/AndroidRuntime(8676): at developer.mohab.gymee.Cardio.Progress.Prgress_Limits(Progress.java:43)
09-05 06:37:30.075: E/AndroidRuntime(8676): at developer.mohab.gymee.Cardio.MainActivity.onCreate(MainActivity.java:55)
09-05 06:37:30.075: E/AndroidRuntime(8676): at android.app.Activity.performCreate(Activity.java:5933)
09-05 06:37:30.075: E/AndroidRuntime(8676): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
09-05 06:37:30.075: E/AndroidRuntime(8676): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
09-05 06:37:30.075: E/AndroidRuntime(8676): ... 10 more
片段包含我想要调用的方法
public class Progress extends Fragment {
TextView Abs ;
TextView Per ;
MainActivity mainActivity ;
Database database ;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mainActivity = (MainActivity) getActivity();
database = new Database(getActivity());
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.progress,container,false);
Abs = (TextView) view.findViewById(R.id.above);
Per = (TextView) view.findViewById(R.id.below);
Prgress_Limits();
return view ;
}
public void Prgress_Limits(){
Cursor cursor ;
cursor = getCursor();
int LAST_INPUT ;
int FIRST_INPUT ;
if(cursor!=null&&cursor.moveToFirst()) {
Log.e("mohab", "cursor not null");
int VALUE_COLUMN = cursor.getColumnIndex(Contract.Cardio_chart.Values.VALUE);
FIRST_INPUT = cursor.getInt(VALUE_COLUMN);
cursor.moveToLast();
LAST_INPUT = cursor.getInt(VALUE_COLUMN);
int abs = LAST_INPUT-FIRST_INPUT ;
Abs.setText(""+abs);
int per = (abs*100)/FIRST_INPUT ;
Per.setText(""+per+" %");
}
else {
Abs.setText("null");
Per.setText("null");
}
}
public Cursor getCursor() {
Cursor c;
String Selection;
Cursor check;
ContentResolver hesolver = getActivity().getContentResolver();
check = hesolver.query(DatabaseFilter.ValueTable(), null, null, null, null);
Cursor check2;
ContentResolver resolver = getActivity().getContentResolver();
check2 = resolver.query(DatabaseFilter.LimitTable(), null, null, null, null);
if (check != null && check.moveToFirst() && check2 != null && check2.moveToFirst()) {
int Offset = offset();
int Limit = limit();
Selection = "SELECT * FROM " + DatabaseFilter.valueTable_Name() + " LIMIT " + Limit + " OFFSET " + Offset + ";";
SQLiteDatabase db = database.getReadableDatabase();
c = db.rawQuery(Selection, null);
} else {
ContentResolver sesolver = getActivity().getContentResolver();
c = sesolver.query(DatabaseFilter.ValueTable(), null, null, null, null);
}
return c;
}
public int offset() {
Cursor save;
ContentResolver resolver = getActivity().getContentResolver();
save = resolver.query(DatabaseFilter.LimitTable(), null, null, null, null);
save.moveToLast();
int high = save.getColumnIndex(Contract.Cardio_chart.Limits.HIGH);
int Offset = save.getInt(high);
save.close();
return Offset;
}
public int limit() {
Cursor h;
ContentResolver resolver = getActivity().getContentResolver();
h = resolver.query(DatabaseFilter.ValueTable(), null, null, null, null);
h.moveToLast();
int id = h.getColumnIndex(Contract.Cardio_chart.Values._ID);
int last_chart_input = h.getInt(id);
int Offset = offset();
int Limit = last_chart_input - Offset;
h.close();
return Limit;
}
}
如何实例化片段并调用Progress_Limits Methode From Activity
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
p = new Progress();
getSupportFragmentManager().beginTransaction()
.add(R.id.group, p)
.commit();
p.Prgress_Limits();
database = new Database(this) ;
setSpinner();
addEntries(0, 0, true);
addPlus();
}
答案 0 :(得分:1)
下面:
p.Prgress_Limits(); //<<<<
database = new Database(this) ;
p.Prgress_Limits()
导致问题,因为在调用commit()
方法之后调用此方法,这意味着可能Fragment尚未就绪。所以请getActivity()
回复null
。
要解决此问题,请在活动的p.Prgress_Limits();
中致电onResume
,或使用Handler
延迟从实时片段访问方法
答案 1 :(得分:0)
与片段进行通信的推荐和最简单的方法是使用接口。
在您的活动中定义一个接口,让您的片段实现该接口。
示例:
申报界面
Pack
在您的活动中,为此接口声明一个变量
var time = ("" + n).replace(/(\d\d)$/,":$1");
现在将侦听器绑定到片段
var time = "" + n;
time = time.slice(0,-2) + ":" + time.slice(-2);
现在在片段中实现接口
public interface ActivityToFragment{ void executeMethodInFragment(); }