【发布时间】:2015-11-26 16:28:54
【问题描述】:
我在从服务器加载图像并使用 Picasso 库显示它时出错,我有一个 JSON 字符串,它有一个 "img" => "whateverimageofanarticle.jpg" 字段,其中在 JSON 字符串上该列的值可能会根据数据库的内容而有所不同。我可以获取 JSON 字符串给出的图像路径,并将其显示在文本视图上,但是当我将它放在毕加索库中 .load() 方法的参数上时,它不起作用并停止了我的应用程序的执行:(
无论如何,我使用片段而不是库来更轻松和更快地执行应用程序(我不知道片段是否对我的问题很重要),代码也在 asynctask 的 postExecute 方法中,其中在onPostExecute() 中被覆盖。这是我的确切代码和我的整个代码。
ImageView banner = new ImageView(getActivity().getApplicationContext());
banner.getLayoutParams().height = 50;
banner.getLayoutParams().width = 80;
banner.requestLayout();
Picasso.with(getActivity().getApplicationContext())
.load("http://rtu-astronet.com/emag/"+_jsonObject.optString("img").toString())
.into(banner);
完整代码:
public class CollegeBulletinListFragment extends Fragment implements OnClickListener, Connector.OnPostExecuteListener{
public CollegeBulletinListFragment(){
}
TextView kem;
TextView headlinehead;
TextView shortdesc;
TextView headlinesender;
LinearLayout llist;
ImageButton img;
ProgressBar pbColBul;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_collegebulletinlist, container, false);
kem = (TextView) rootView.findViewById(R.id.mon_title);
img = (ImageButton) rootView.findViewById(R.id.imageButton1);
pbColBul = (ProgressBar) rootView.findViewById(R.id.pBcollegebul);
img.setOnClickListener(this);
headlinehead = (TextView) rootView.findViewById(R.id.mon_head_title);
shortdesc = (TextView) rootView.findViewById(R.id.mon_head_desc);
headlinesender = (TextView) rootView.findViewById(R.id.mon_head_sender);
String c = getArguments().getString("passingWord");
llist = (LinearLayout) rootView.findViewById(R.id.lllist);
int colval = this.setTextAndColorsToHead(c);
pbColBul.setVisibility(ProgressBar.VISIBLE);
//Execute items on scroll view
String secondFlag = "list";
Connector connect= new Connector(getActivity().getApplicationContext(),colval,secondFlag,0,0,0,"","");
connect.setOnPostExecuteListener(this);
connect.execute("");
return rootView;
}
public int setTextAndColorsToHead(String arg){
int collegeval = 0;
if (arg.equals("GN")){
kem.setText("General News");
} else if (arg.equals("CCS")){
kem.setText("College of Computer Studies");
collegeval = 1;
} else if (arg.equals("COE")){
kem.setText("College of Engineering");
collegeval = 2;
} else if (arg.equals("COED")){
kem.setText("College of Education");
collegeval = 4;
} else if (arg.equals("CON")){
kem.setText("College of Nursing");
collegeval = 3;
} else if (arg.equals("CBA")){
kem.setText("College of Business and Accountancy");
collegeval = 5;
} else if (arg.equals("CAS")){
kem.setText("College of Arts and Sciences");
collegeval = 6;
} else if (arg.equals("CIHM")){
kem.setText("College of International and Hospitality Management");
collegeval = 7;
}
return collegeval;
}
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
SpecificArticleFragment spcf = new SpecificArticleFragment();
Bundle args = new Bundle();
String tag = arg0.getTag().toString();
args.putString("art_id",tag);
spcf.setArguments(args);
FragmentManager fm = getFragmentManager();
fm.beginTransaction().replace(R.id.content_frame, spcf).commit();
}
@Override
public void onPostExecute(String result) {
//headlinehead.setText(result);
try{
JSONObject jsonRootObj = new JSONObject(result);
JSONArray jsonArray = jsonRootObj.optJSONArray("Data");
JSONObject jsonObject = jsonArray.getJSONObject(0);
//code for headline
this.headlinehead.setText(jsonObject.optString("title").toString());
this.shortdesc.setText(jsonObject.optString("shortdesc").toString());
this.headlinesender.setText(jsonObject.optString("penname").toString());
this.img.setTag(jsonObject.optString("id").toString());
//code for headline
for (int p = 1; p < jsonArray.length(); p++){
JSONObject _jsonObject = jsonArray.getJSONObject(p);
LinearLayout conteach = new LinearLayout(getActivity().getApplicationContext());
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
conteach.setLayoutParams(lp);
conteach.setOrientation(LinearLayout.VERTICAL);
ImageView banner = new ImageView(getActivity().getApplicationContext());
banner.getLayoutParams().height = 50;
banner.getLayoutParams().width = 80;
banner.requestLayout();
Picasso.with(getActivity().getApplicationContext())
.load("http://rtu-astronet.com/emag/"+_jsonObject.optString("img").toString())
.into(banner);
TextView art_title = new TextView(getActivity().getApplicationContext());
art_title.setText(_jsonObject.optString("title").toString());
art_title.setTextSize(20);
art_title.setLayoutParams(new LayoutParams(350,LayoutParams.WRAP_CONTENT));
TextView art_shortdesc = new TextView(getActivity().getApplicationContext());
art_shortdesc.setText(_jsonObject.optString("shortdesc").toString());
art_shortdesc.setTextSize(14);
art_shortdesc.setTypeface(null, Typeface.ITALIC);
art_shortdesc.setLayoutParams(new LayoutParams(350,LayoutParams.WRAP_CONTENT));
TextView art_sender = new TextView(getActivity().getApplicationContext());
art_sender.setText("Written by: " + _jsonObject.optString("sender").toString());
art_sender.setTextSize(14);
art_sender.setLayoutParams(new LayoutParams(350,LayoutParams.WRAP_CONTENT));
art_sender.setPadding(0, 0, 0, 15);
conteach.addView(art_title);
conteach.addView(art_shortdesc);
conteach.addView(art_sender);
conteach.setTag(_jsonObject.optString("id").toString());
conteach.setClickable(true);
conteach.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
SpecificArticleFragment spcf = new SpecificArticleFragment();
Bundle args = new Bundle();
args.putString("art_id", v.getTag().toString());
spcf.setArguments(args);
FragmentManager ft = getFragmentManager();
ft.beginTransaction().replace(R.id.content_frame, spcf).commit();
}
});
this.llist.addView(conteach);
}
this.pbColBul.setVisibility(ProgressBar.GONE);
} catch(JSONException e){
}
}
}
【问题讨论】:
-
你能发布错误吗?
-
我已经尝试删除毕加索的代码,仍然有同样的错误:(它在 logcat 中说有一个空指针异常
标签: android json android-imageview android-image picasso