这是我的代码,其中m试图从服务器提取aal数据但是错误来自" setImageView()"方法。任何人都可以尽快帮助我。我的代码是关于从服务器显示信息和图像。
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.widget.ImageView;
import android.widget.TextView;
public class PlantDetails extends Activity {
String whichPlantId;
String whichPlantName;
String whichFamily;
String whichSystemOfMedicineUsed;
String whichHabit;
String whichMedUses;
String whichCultStatus;
String whichExplanation;
String whichimageurl;
ImageView tvTemp8;
Activity convertView;
//finding all textView and ImageView
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_plant_details);
TextView tvTemp = (TextView) findViewById(R.id.dpplant_id);
TextView tvTemp1 = (TextView) findViewById(R.id.dpfullplantname);
TextView tvTemp2 = (TextView) findViewById(R.id.dpfamily);
TextView tvTemp3 = (TextView) findViewById(R.id.dpdep_ref);
TextView tvTemp4 = (TextView) findViewById(R.id.dphabit);
TextView tvTemp5 = (TextView) findViewById(R.id.dpmeduses);
TextView tvTemp6 = (TextView) findViewById(R.id.dpcult_status);
TextView tvTemp7 = (TextView) findViewById(R.id.dpexplanation);
tvTemp8 = (ImageView) convertView.findViewById(R.id.dpimageurl);
Intent iGetDetails = getIntent();
Bundle b = iGetDetails.getExtras();
whichPlantId = b.getString("plant_id");
whichPlantName = b.getString("fullplantname");
whichFamily = b.getString("plantfamily");
String whichDptRef = b.getString("dptRef");
whichSystemOfMedicineUsed = "SysMed";
if(whichDptRef.contains("A"))
{
whichSystemOfMedicineUsed += ", Ayurveda";
}
if(whichDptRef.contains("S"))
{
whichSystemOfMedicineUsed += ", Siddha";
}
if(whichDptRef.contains("U"))
{
whichSystemOfMedicineUsed += ", Unani";
}
if(whichDptRef.contains("H"))
{
whichSystemOfMedicineUsed += ", Homeopathy";
}
if(whichDptRef.contains("F"))
{
whichSystemOfMedicineUsed += ", Folk";
}
if(whichDptRef.contains("T"))
{
whichSystemOfMedicineUsed += ", Sowa-Rigpa";
}
if(whichDptRef.contains("M"))
{
whichSystemOfMedicineUsed += ", Modern";
}
if(whichDptRef.contains("C"))
{
whichSystemOfMedicineUsed += ", Chinese";
}
whichSystemOfMedicineUsed = whichSystemOfMedicineUsed.replace("SysMed, ", "");
whichHabit = b.getString("habit");
whichMedUses = b.getString("meduses");
whichExplanation = b.getString("explanation");
whichCultStatus = b.getString("cultStatus");
whichimageurl = b.getString("imageurl");
//seting Textview and imageview
tvTemp.setText("Plant Id is " + whichPlantId + " Image url is " + whichimageurl);
tvTemp1.setText("Plant Name: " + whichPlantName);
tvTemp2.setText("Family:" + whichFamily);
tvTemp3.setText("System of Medicines: " + whichSystemOfMedicineUsed );
tvTemp4.setText("Habit:" + whichHabit );
tvTemp5.setText("Medicinal Uses:" + whichMedUses);
tvTemp6.setText("Cultivated Status:" + whichCultStatus);
tvTemp7.setText("Explanation:" + whichExplanation);
tvTemp8.setImageResource(whichimageurl);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_plant_details, menu);
return true;
}
}
答案 0 :(得分:1)
tvTemp8 = (ImageView) convertView.findViewById(R.id.dpimageurl);
convertView
不需要。如果你想在当前活动的视图层次中搜索,
convertView
未初始化。
内部onCreate()
,
convertView=this;
或
convertView=PlantDetails.this;
。
答案 1 :(得分:0)
public static Bitmap getBitmapFromURL(String src) {
try {
Log.e("src",src);
URL url = new URL(src);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
Bitmap myBitmap = BitmapFactory.decodeStream(input);
Log.e("Bitmap","returned");
return myBitmap;
} catch (IOException e) {
e.printStackTrace();
Log.e("Exception",e.getMessage());
return null;
}
}
tvTemp8.setImageResource(whichimageurl);change this line to this line
tvTemp8.setImageBitmap(getBitmapFromURL(whichimageurl));