gpt4 book ai didi

android - 从 Activity 调用 Fragment 中的方法返回 java.lang.NullPointerException 错误

转载 作者:行者123 更新时间:2023-11-30 01:54:50 25 4
gpt4 key购买 nike

在下面的 Fragment 中调用 Progress_Limits 方法时出现此错误,如何正确初始化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

fragment 包含我要调用的方法

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;
}

我如何实例化 Fragment 并从 Activity 调用 Progress_Limits 方法

  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();


}

最佳答案

这里:

p.Prgress_Limits();  //<<<<
database = new Database(this) ;

p.Prgress_Limits() 导致问题,因为此方法是在调用 commit() 方法之后调用的,这意味着 Fragment 可能尚未准备好。所以调用 getActivity() 返回 null

要修复此问题,请在 Activity 的 onResume 中调用 p.Prgress_Limits(); 或使用 Handler 延迟访问方法直播 fragment

关于android - 从 Activity 调用 Fragment 中的方法返回 java.lang.NullPointerException 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32409611/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com