gpt4 book ai didi

android - Fragment 中空对象引用上的进度条

转载 作者:行者123 更新时间:2023-11-29 17:22:52 25 4
gpt4 key购买 nike

<分区>

在我的 fragment 中,当我将我的进度条放在 ListView fragment 中时出现错误,我的应用程序崩溃了,当我加载我的 fragment 时,进度条没有消失请帮助我如何解决这个问题

fragment .java

ListAdapter adapter;
public static ArrayList<Pojo> gridData;
GridView grd;
private ProgressBar mProgressBar;
private String Sam_URL = "http://example.com/rest/Main/document?name=kids@_$example";


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.gridlayout_progressbar, container, false);
System.out.println("Servce Called");
gridData=new ArrayList<Pojo>();

grd =(GridView)rootView.findViewById(R.id.gridview);
Async as=new Async(getActivity(),grd);
as.execute(Sam_URL);

mProgressBar = (ProgressBar)rootView.findViewById(R.id.progressBar);
// grd.setBackgroundColor(Color.CYAN);
grd.setVerticalSpacing(7);
grd.setHorizontalSpacing(7);



grd.setOnItemClickListener(new AdapterView.OnItemClickListener() {

@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
// TODO Auto-generated method stub

Intent intent=new Intent(getActivity(),KidsWear.class);
intent.putExtra("pos", position+"");
startActivity(intent);

}
});

return rootView;
}







class Async extends AsyncTask<String, Void, Integer>{
Context context;
GridView gridView;


public Async(Context context,GridView gridView) {
// TODO Auto-generated constructor stub

this.context=context;
this.gridView=gridView;
}



@Override
protected Integer doInBackground(String... params) {
// TODO Auto-generated method stub


Integer result = 0;
try {
// Create Apache HttpClient
//HttpClient httpclient = new DefaultHttpClient();
URL url = new URL(Sam_URL);
URLConnection urlConnection = url.openConnection();
InputStream in = new BufferedInputStream(
urlConnection.getInputStream());

// int statusCode =
// httpResponse.getStatusLine().getStatusCode();

// 200 represents HTTP OK
if (true) {
String response = streamToString(in);
parseResult(response);
result = 1; // Successful
} else {
result = 0; // "Failed
}
} catch (Exception e) {

}

return result;



}
String streamToString(InputStream stream) throws IOException {
BufferedReader bufferedReader = new BufferedReader(
new InputStreamReader(stream));
String line;
String result = "";
while ((line = bufferedReader.readLine()) != null) {
result += line;
}

// Close stream
if (null != stream) {
stream.close();
}
return result;
}

@Override
protected void onPostExecute(Integer result) {
// TODO Auto-generated method stub
super.onPostExecute(result);


if (result == 1) {

gridView.setAdapter(new MyAdapter(context,gridData));

gridView.setVisibility(View.VISIBLE);



}mProgressBar.setVisibility(View.GONE);

}



private void parseResult(String result) {
try {

Log.d("MainActivity", "JSON Result : " + result);
JSONArray response = new JSONArray(result);

for (int i = 0; i < response.length(); i++)

{

JSONObject obj = response.getJSONObject(i);

String Doc_name = obj.getString("documentName");
Log.d("documentName",Doc_name);

String Doc_file = obj.getString("documentFile");
String Doc_content = obj.getString("documentContent");

String Doc_offer=obj.getString("offer");
String Doc_address=obj.getString("address");

//Log.d("documentName","JSON Result : " + result);

Pojo gd = new Pojo();



gd.setDocumentName(Doc_name);

gd.setDocumentFile(Doc_file);
gd.setOffer(Doc_offer);

gd.setDocumentContent(Doc_content);
gd.setAddress(Doc_address);

gridData.add(gd);


}


} catch (JSONException e) {
e.printStackTrace();
}


}
}

在我的日志 cat 错误下面的地方请任何人帮助我

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ProgressBar.setVisibility(int)' on a null object reference

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