我花了这么多时间在这上面,我只想要最快的解决方案而不改变我的整个项目。 我有一个Fragment1调用另一个fragment2用于数据获取目的。在fragment2获取了所需的数据之后,然后将其自身替换为片段3,片段3显示所有收集的数据。 我的fragment1包含2个片段,搜索片段,另一个片段是前面提到的fragment2和fragment3。我的fragment2显示的是一个对话框和一个textview,如果没有获取数据,则包含错误。
我的问题是我不能以某种方式用fragment3替换fragment2,它继续在FragmentTransaction ft = getFragmentManager()上弹出错误Null指针异常.startTransaction()行..请帮忙,我是android编程的新手,如果有的话我错过的东西请告诉我..非常感谢你..
这是我的fragment1代码
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final View rootView = inflater.inflate(R.layout.fragment_grade_layout, container, false);
main_spnr = (Spinner) rootView.findViewById(R.id.spinner_gradecategory);
main_spnr.setSelection(0);
db = new mycsu_database(rootView.getContext());
main_spnr.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?>arg0, View view, int arg2, long arg3) {
Log.v(TAG, "gradecount= " + db.getgradecount(idnumber));
if(db.getgradecount(idnumber)!=0){
Log.v(TAG, "gradecount= " + db.getgradecount(idnumber));
InterfaceMode = main_spnr.getSelectedItemPosition();
getfragments();
}
else{
Fragment fr = new DataGetterClass(idnumber,InterfaceMode);
FragmentManager fm = getFragmentManager();
FragmentTransaction fragmentTransaction = fm.beginTransaction();
fragmentTransaction.replace(R.id.frame_gradeListParent, fr);
fragmentTransaction.commit();
}
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
InterfaceMode = 0;
prvInterfaceMode = -1;
if(db.getgradecount(idnumber)!=0){
Log.v(TAG, "gradecount= " + db.getgradecount(idnumber));
InterfaceMode = main_spnr.getSelectedItemPosition();
getfragments();
}
else{
Fragment fr = new DataGetterClass(idnumber,InterfaceMode);
FragmentManager fm = getFragmentManager();
FragmentTransaction fragmentTransaction = fm.beginTransaction();
fragmentTransaction.replace(R.id.frame_gradeListParent, fr);
fragmentTransaction.commit();
}
return rootView;
}
private void getfragments(){
Fragment fr = null;
if(prvInterfaceMode != InterfaceMode){
Log.v(TAG,"InterfaceMode: " + InterfaceMode);
switch(InterfaceMode){
case 0:
Log.v(TAG,"here in 0");
fr = new ListofGrades();break;
case 1:
fr = new fragment_selectsem();break;
case 2:
fr = new ListofallGrades();break;
default:break;
}
if(fr!=null){
Log.v(TAG,"calling " + InterfaceMode);
FragmentManager fm = getFragmentManager();
FragmentTransaction fragmentTransaction = fm.beginTransaction();
fragmentTransaction.setCustomAnimations(R.anim.slide_in_left, R.anim.slide_out_right);
fragmentTransaction.replace(R.id.frame_gradeListParent, fr);
fragmentTransaction.commit();
}
prvInterfaceMode = InterfaceMode;
}
}
我的Fragment2是DataGetterClass()
public class DataGetterClass extends Fragment {
private static final String TAG = DataGetterClass.class.getSimpleName();
private static String _idnum="";
private static mycsu_database db;
private static ProgressBar progress;
private static TextView tvr;
private static int _callnxt;
FrameLayout frm;
int progressBarStatus = 0;
boolean done = false;
public DataGetterClass(String idnum, int callnxt){
DataGetterClass._idnum = idnum;
DataGetterClass._callnxt = callnxt;
}
@Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
final View rootView = inflater.inflate(R.layout.fragment_loading, container, false);
db = new mycsu_database(rootView.getContext());
progress = (ProgressBar) rootView.findViewById(R.id.progressBar_getter);
progress.setIndeterminate(true);
tvr = (TextView) rootView.findViewById(R.id.textView1_error);
frm = (FrameLayout) rootView.findViewById(R.id.frame_gradeListParent);
tvr.setVisibility(View.INVISIBLE);
new RecieveAsyncTask().execute();
new Thread(new Runnable() {
public void run() {
while(true){
if(done){
Log.v(TAG, "loading done");
FragmentTransaction ft = getFragmentManager().beginTransaction();
Fragment wvf = new ListofGrades();
ft.replace(frm.getId(), wvf);
ft.commit();
break;
}
}
}
}).start();
return rootView;
}
private class RecieveAsyncTask extends AsyncTask<Void, Void, Void> {
String _Response;
@Override
protected void onPreExecute() {
super.onPreExecute();
progress.setVisibility(View.VISIBLE);
}
@Override
protected Void doInBackground(Void... params) {
// TODO Auto-generated method stub
_Response = postData();
return null;
}
@Override
protected void onPostExecute(Void result) {
progress.setVisibility(View.INVISIBLE);
if(_Response!=""){
if(ParseJson(_Response))
done=true;//getfragments();
else
tvr.setVisibility(View.VISIBLE);
}
else{
Log.v("json", "unable to connect to server");
tvr.setVisibility(View.VISIBLE);
}
}
}
public String postData() {
// Create a new HttpClient and Post Header
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://192.168.5.159/mycsu/mycsu.php");
String _response = "";
Log.v("trytry", " 11 ");
try {
// Add your data
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("studid", _idnum));
nameValuePairs.add(new BasicNameValuePair("Mode","1"));
nameValuePairs.add(new BasicNameValuePair("studyr",""));
nameValuePairs.add(new BasicNameValuePair("studsem",""));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
InputStream inputStream = response.getEntity().getContent();
InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
StringBuilder stringBuilder = new StringBuilder();
String bufferedStrChunk = null;
while((bufferedStrChunk = bufferedReader.readLine()) != null){
stringBuilder.append(bufferedStrChunk);
}
_response = stringBuilder.toString();
} catch (ClientProtocolException e) {
Log.d(TAG,e.toString());
// TODO Auto-generated catch block
} catch (IOException e) {
Log.d(TAG,e.toString());
// TODO Auto-generated catch block
}
//Log.v(TAG, _response);
//Log.v("trytry", " 22 ");
// Log.v("trytry",_response);
return _response;
}
boolean ParseJson(String JsonStr){
boolean returnbool = true;
String Jsontstr = "{ \"Android\":" + JsonStr + "}";
GradeClass gc = new GradeClass();
GpaClass gpac = new GpaClass();
JSONObject jsonResponse;
try{
jsonResponse = new JSONObject(Jsontstr);
JSONArray jsonMainNode = jsonResponse.optJSONArray("Android");
int lengthJsonArr = jsonMainNode.length();
for( int i=0; i< lengthJsonArr; i++){
JSONObject jsonChildNode = jsonMainNode.getJSONObject(i);
String coursecode = jsonChildNode.optString("courseno");
String coursedesc = jsonChildNode.optString("description");
String grade =jsonChildNode.optString("grade");
int unit = Integer.parseInt(jsonChildNode.optString("unit"));
String compgrade = jsonChildNode.optString("gcompl");
String sem = jsonChildNode.optString("sem");
String yr = jsonChildNode.optString("sy");
String idnumber = jsonChildNode.optString("studid");
float gpa =Float.parseFloat( jsonChildNode.optString("gpa"));
String standing = jsonChildNode.optString("standing");
/*
Log.d(TAG, "coursecode: " + coursecode
+" coursedesc: " + coursedesc
+" grade: " + grade
+" unit: " + unit
+" compgrade: " + compgrade
+" sem: " + sem
+" yr: " + yr
+" idnumber: " + idnumber
+" gpa: " + gpa
+" standing: " + standing);
*/
float compgradef, gradeint;
if(grade.equals("INC"))
gradeint = 4;
else if(grade.equals("PASSED"))
gradeint = (float) 4.5;
else if(grade.equals("FAILED"))
gradeint = (float) 6;
else if(grade.equals("IN PROG"))
gradeint = (float) 7;
else
gradeint = Float.parseFloat(grade);
if(compgrade.equals("null")){
compgradef = 0;
}else
compgradef = Float.parseFloat(compgrade);
gc.setcoursecode(coursecode);
gc.setcoursedesc(coursedesc);
gc.setgrade(gradeint);
gc.setunits(unit);
gc.setcompgrade(compgradef);
gc.setcoursesem(sem);
gc.setcourseyr(yr);
gc.setidnumber(idnumber);
gpac.setcoursesem(sem);
gpac.setcourseyr(yr);
gpac.setgpa(gpa);
gpac.setidnumber(idnumber);
gpac.setstanding(standing);
if(db.findifgradeexist(gc)==false){
db.add_grade(gc);
//Log.v(TAG,"data GRADE inserted in db");
}else;
//Log.v(TAG,"data GRADE exist NOT inserted in db");
if(!db.findifgpaexist(gpac)==false){
db.add_gpa(gpac);
//Log.v(TAG,"data GPA inserted in db");
}else;
//Log.v(TAG,"data GPA exist NOT inserted in db");
}
}catch (JSONException e) {
returnbool = false;
e.printStackTrace();
}
return returnbool;
}
我在fragment2中做了很多事情,只是为了让它按照我想要的方式运行,直到我创建了一个侦听asynctask完成的线程......但是它再次无效..请帮助如果你有一个解决方案..将不胜感激..
哦!!,这是LogCat
02-17 18:59:38.064: E/AndroidRuntime(3069): FATAL EXCEPTION: Thread-3298
02-17 18:59:38.064: E/AndroidRuntime(3069): java.lang.NullPointerException
02-17 18:59:38.064: E/AndroidRuntime(3069): at com.csu.mycsu_proto1_DataGetter.DataGetterClass$1.run(DataGetterClass.java:82)
02-17 18:59:38.064: E/AndroidRuntime(3069): at java.lang.Thread.run(Thread.java:856)
答案 0 :(得分:1)
我认为,这里的问题是你在AsyncTask中使用局部变量_Response。我不相信它是线程安全的。 doInBackground在主线程中的异步线程onPostExecute中运行。 AsyncTask类使您可以使用标头中的Void安全地在线程之间传递信息。第三个Void用于将信息从doInBackground传递给onPostExecute。
尝试将doInBackground的最后两个语句更改为
return postData();
并将doInBackground更改为受保护的字符串
并将标题更改为onPostExecute
protected void onPostExecute(String _Response){
并将标题更改为ReceiveAsyncTask,以便将第三个Void更改为String(抱歉,我无法在答案中输入尖括号或 我会发表声明)。
并消除String _Response声明。