2012-06-26 14 views
8

संभव डुप्लिकेट:
Android - ViewRootImpl$CalledFromWrongThreadException

इम मेरी यूआरएल और आवेदन में प्रदर्शन से छवि प्राप्त करने की कोशिश, लेकिन यह त्रुटि फेंक android.view.ViewRootImpl $ CalledFromWrongThreadException: की वजह से केवल मूल बात यह है कि एक दृश्य पदानुक्रम बनाया अपने विचार छू सकता है। नीचे मेरी कोड

कोड के उपयोग AsyncTask की

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; 
     } 
    } 
} 
+0

im खेद मेरा बुरा ... मैं इस सवाल का पहले उठाया। अपडेट के लिए धन्यवाद। –

+1

यूआई में परिवर्तन 'onPostExecute' – CaffeineShots

उत्तर

35

बुरा व्यवहार,

आप doInBackGround() से अपने मुख्य यूआई धागा अद्यतन करने के लिए के रूप में AsyncTask कि अनुमति कभी नहीं की कोशिश कर रहे है।

कभी अपने ही कार्यकर्ता धागे के रूप AsyncTask की doInBackGround() से अपने यूआई अद्यतन करें। तो .. AsyncTask की onPostExecute() विधि में अपने मुख्य यूआई updatation कोड लिखने

@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); 
} 
+3

कारण है कि आप बुरा व्यवहार ने कहा ... कृपया सलाह से किया जाना चाहिए। Thansk –

6

@ user370305 ने कहा,

आप गैर ui सूत्र में यूआई तत्वों को स्पर्श नहीं कर सकते हैं क्या करने के लिए कुछ अंक जोड़ देगा।

आप AsyncTaskdoInBackground उपयोग कर रहे हैं गैर ui धागा में मार डाला ताकि आप उस में imageView तक नहीं पहुँच सकता है।

आप onPreExecute, onProgressUpdate & onPostExcecute में UI तत्वों तक पहुंच सकते हैं।

संबंधित मुद्दे