gpt4 book ai didi

java - ImageView.setImageBitmap 始终为 null

转载 作者:行者123 更新时间:2023-12-01 12:37:17 25 4
gpt4 key购买 nike

我正在尝试从 interwebz 加载图像,但出现空指针异常。这是我的代码:

public class Search extends ActionBarActivity {
TableLayout tableScrollView;
String[] JSONExceptions = { "type", "em", "user_id", "id", "profilepic",
"bg" };
String value;
JSONObject jObject;

private float mx, my;
private float curX, curY;

private ScrollView vScroll;
private HorizontalScrollView hScroll;
private Bitmap profilepic;
private ImageView iv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search);

vScroll = (ScrollView) findViewById(R.id.vScroll);
hScroll = (HorizontalScrollView) findViewById(R.id.hScroll);
iv = (ImageView) findViewById(R.id.imageView);
Bundle extras = getIntent().getExtras();
if (extras != null) {
value = extras.getString("id");
}
System.out.println(value);
tableScrollView = (TableLayout) findViewById(R.id.tableScrollView);
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
jObject = getJson("http://www.tabcards.com/req/androidapi/L2o30H8JlFMtFYHW3KLxkts20ztc5Be6Z6m6v315/json/"
+ value);
try {
profilepic = BitmapFactory.decodeStream(new URL(jObject.getString("profilepic")).openConnection().getInputStream());
iv.setImageBitmap(profilepic);
} catch (MalformedURLException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
} catch (Exception e1) {
e1.printStackTrace();
}
runOnUiThread(new Runnable() {

@Override
public void run() {
try {

System.out.println("awaking");
createUI(jObject);
System.out.println("done");

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

});

}
});
thread.start();

System.out.println("complete");

}

private void createUI(JSONObject jObject) throws JSONException {
int absIndex = 0;
String value = jObject.getString("name");
if (value != "" && !jObject.getString("profilepic").equals("")) {
System.out.println(jObject.getString("profilepic"));

insertElement(value, absIndex++, true);
}


}



public static Bitmap drawableToBitmap (Drawable drawable) {
Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
drawable.draw(canvas);

return bitmap;
}

public static Bitmap getclip(Bitmap bitmap) {
Bitmap output = Bitmap.createBitmap(bitmap.getWidth(),
bitmap.getHeight(), Config.ARGB_8888);
Canvas canvas = new Canvas(output);

final Paint paint = new Paint();
final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());

paint.setAntiAlias(true);
canvas.drawARGB(0, 0, 0, 0);
canvas.drawCircle(bitmap.getWidth() / 2, bitmap.getHeight() / 2,
bitmap.getWidth() / 2, paint);
paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
canvas.drawBitmap(bitmap, rect, rect, paint);
return output;
}


public Bitmap getCroppedBitmap(Bitmap bitmap) {
Bitmap output = Bitmap.createBitmap(bitmap.getWidth(),
bitmap.getHeight(), Config.ARGB_8888);
Canvas canvas = new Canvas(output);

final int color = 0xff424242;
final Paint paint = new Paint();
final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());

paint.setAntiAlias(true);
canvas.drawARGB(0, 0, 0, 0);
paint.setColor(color);
// canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
canvas.drawCircle(bitmap.getWidth() / 2, bitmap.getHeight() / 2,
bitmap.getWidth() / 2, paint);
paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
canvas.drawBitmap(bitmap, rect, rect, paint);
//Bitmap _bmp = Bitmap.createScaledBitmap(output, 60, 60, false);
//return _bmp;
return output;
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.search, menu);
return true;
}



private void insertElement(String data, int i, boolean flag) {
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View newRow = inflater.inflate(R.layout.row, null, false);
newRow.setLayoutParams(new TableRow.LayoutParams(
TableRow.LayoutParams.MATCH_PARENT,
TableRow.LayoutParams.WRAP_CONTENT));

TextView dataTextView = (TextView) newRow
.findViewById(R.id.rowTextView);
dataTextView.setText(data);

if(!flag) {
iv.setVisibility(View.GONE);
}
System.out.println(dataTextView.getText().toString());
tableScrollView.addView(newRow, i);
}

public static boolean useLoop(String[] arr, String targetValue) {
for(String s: arr){
if(s.equals(targetValue))
return true;
}
return false;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}

public static <T> boolean contains2(final T[] array, final T v) {
if (v == null) {
for (final T e : array)
if (e == null)
return true;
} else {
for (final T e : array)
if (e == v || v.equals(e))
return true;
}

return false;
}

public static Drawable LoadImageFromWeb(String url) {
try {
InputStream is = (InputStream) new URL(url).getContent();
Drawable d = Drawable.createFromStream(is, "src name");
return d;
} catch (Exception e) {
return null;
}
}

// Given a string representation of a URL, sets up a connection and gets
// an input stream.
private InputStream downloadUrl(String urlString) throws IOException {
URL url = new URL(urlString);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setReadTimeout(10000 /* milliseconds */);
conn.setConnectTimeout(15000 /* milliseconds */);
conn.setRequestMethod("GET");
conn.setDoInput(true);
// Starts the query
conn.connect();
return conn.getInputStream();
}

static InputStream is = null;
static JSONObject jObj = null;
static String json = "";

public static JSONObject getJson(String url) {

InputStream is = null;
String result = "";
JSONObject jsonObject = null;

// HTTP
try {
HttpClient httpclient = new DefaultHttpClient(); // for port 80
// requests!
HttpPost httppost = new HttpPost(url);
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
} catch (Exception e) {
return null;
}

// Read response to string
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "utf-8"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
System.out.println(line);
}
is.close();
result = sb.toString().replace("[", "");
System.out.println("done getting obj : " + result);

} catch (Exception e) {
return null;
}

// Convert string to object
try {
jsonObject = new JSONObject(result.replace("]", ""));
} catch (JSONException e) {
return null;
}

return jsonObject;

}

@Override
public boolean onTouchEvent(MotionEvent event) {
float curX, curY;

switch (event.getAction()) {

case MotionEvent.ACTION_DOWN:
mx = event.getX();
my = event.getY();
break;
case MotionEvent.ACTION_MOVE:
curX = event.getX();
curY = event.getY();
vScroll.scrollBy((int) (mx - curX), (int) (my - curY));
hScroll.scrollBy((int) (mx - curX), (int) (my - curY));
mx = curX;
my = curY;
break;
case MotionEvent.ACTION_UP:
curX = event.getX();
curY = event.getY();
vScroll.scrollBy((int) (mx - curX), (int) (my - curY));
hScroll.scrollBy((int) (mx - curX), (int) (my - curY));
break;
}

return true;
}
}

它主要在我处理它的线程中。我不知道发生了什么事,我尝试查看其他问题,但没有一个解决我的问题。我检索到的图像没问题,我的意思是,我执行了 profilepic.getWidth() 并且它返回了实际的准确值。

最佳答案

问题是您的 imageview 不存在于您在 Activity 中使用的 xml 中,因此给您带来了 NPE。您不能使用 Activity 布局中不存在的 View

解决方案

在 Activity_search.xml 中添加 xml,并确保在 runonuithread 中设置 imagebackground。

关于java - ImageView.setImageBitmap 始终为 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25475306/

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