我试图从我的URL获取图像并在应用程序中显示,但引发错误, 原因如下:android.view.ViewRootImpl $
CalledFromWrongThreadException:只有创建视图层次结构的原始线程才能触摸其视图
。下面是我的代码
码
package com.smartag.bird.dev;
public class MainActivity extends Activity {
static String ndefMsg = null;
static String ndefMsg1 = null;
NfcAdapter mNfcAdapter;
PendingIntent mNfcPendingIntent;
IntentFilter[] mNdefExchangeFilters;
static final String TAG = "Read Tag";
TextView mTitle;
private static ImageView imageView;
static String url = "http://sposter.smartag.my/images/chicken_soup.jpg";
private static Bitmap downloadBitmap;
private static BitmapDrawable bitmapDrawable;
private static boolean largerImg = false;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.main);
mNfcAdapter = NfcAdapter.getDefaultAdapter(this);
mNfcPendingIntent = PendingIntent.getActivity(this, 0,
new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
IntentFilter ndefDetected = new IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED);
try {
ndefDetected.addDataType("text/plain");
} catch (MalformedMimeTypeException e) { }
mNdefExchangeFilters = new IntentFilter[] { ndefDetected };
if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(getIntent().getAction())) {
NdefMessage[] messages = getNdefMessages(getIntent());
byte[] payload = messages[0].getRecords()[0].getPayload();
ndefMsg = new String(payload);
setIntent(new Intent()); // Consume this intent.
}
ConnectivityManager connManager = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
NetworkInfo mWifi = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
if(ndefMsg == null || ndefMsg.length() == 0)
{
startActivity(new Intent(MainActivity.this, MainMenu.class));
}
else
{
setContentView(R.layout.main);
if (mWifi.isConnected()) {
ndefMsg1 = ndefMsg;
new DownloadFilesTask().execute();
ndefMsg = null;
}
else
{
AlertDialog.Builder dialog = new AlertDialog.Builder(this);
dialog.setTitle("Attention");
dialog.setMessage("No Internet Connection. Please enable the wifi.");
dialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton)
{
}
});
dialog.show();
}
}
}
private class DownloadFilesTask extends AsyncTask<Void, Void, Void> {
protected void onPostExecute(Void result) {
}
@Override
protected Void doInBackground(Void... params) {
try {
URL myFileUrl = new URL("http://sposter.smartag.my/images/chicken_soup.jpg");
HttpURLConnection conn= (HttpURLConnection)myFileUrl.openConnection();
conn.setDoInput(true);
conn.connect();
InputStream is = conn.getInputStream();
downloadBitmap = BitmapFactory.decodeStream(is);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
ImageView image = (ImageView) findViewById(R.id.imview);
image.setImageBitmap(downloadBitmap);
return null;
}
}
}
使用 AsyncTask的 错误做法,
您正在尝试从更新主UI线程,doInBackGround()
因为AsyncTask从未允许这样做。
切勿从doInBackGround()
AsyncTask的唯一工作线程中更新UI。因此onPostExecute()
,使用AsyncTask方法编写您的Main UI更新数据。
@Override
protected Void doInBackground(Void... params) {
try {
URL myFileUrl = new URL("http://sposter.smartag.my/images/chicken_soup.jpg");
HttpURLConnection conn= (HttpURLConnection)myFileUrl.openConnection();
conn.setDoInput(true);
conn.connect();
InputStream is = conn.getInputStream();
downloadBitmap = BitmapFactory.decodeStream(is);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Override
onPostExecute()
{
ImageView image = (ImageView) findViewById(R.id.imview);
image.setImageBitmap(downloadBitmap);
}
我已经在我的电脑上安装了Android Studio。当我试图安装cordova到我的电脑,它返回以下错误信息。有人能在这个问题上提供帮助而不重新安装Android SDK吗? npm安装-g cordova cordova创建MyApp
其中“url”是JSON格式的,它使用http GET方法返回大量数据。 我无法从我在getResultForRequest(url)方法中传递的“url”中获取JSON格式的数据。我在urlConnection.connect();中出错。Internet权限也在AndroidManifest.xml文件中给出。 这是我的日志。 提前谢谢。
当我试图使用Spring-Boot-Test在Spring-Boot1.5.3版本中运行控制器测试时,我发现了以下错误:java.lang.ClassNotFoundException:javax.servlet.ServletException 我正在运行ContactsManagementControllerIntegrationTest.java类,使用(eclipse)右键单击文件并以ju
我正试图打开我的应用程序,但这是一个错误的膨胀 日志: 它拉出错误的片段行: 这段代码在一个条目片段中,我正在使用firebase实现一个登录 就在我的startActivity下面,我有一个“finish()”,但由于我在一个片段中,我必须删除它,我不知道如果没有这个方法,问题是否会出现
我试过很多方法,但都不奏效... Android资源编译失败输出:C:\users\user\desktop\new Android\test1_tp7\app\src\main\res\layout\activity_main.xml:1:错误:格式不正确(无效令牌)。命令:c:\users\user.gradle\caches\transforms-1\files-1.1\aapt2-3.2.