2014-09-12 45 views
2

我正在使用cloudinary上傳圖片。Android cloudinary異步上傳器錯誤

當我試圖在onCreate中做到這一點時,它給出了錯誤明顯的錯誤NetworkOnMainThread。所以現在我嘗試使用AsyncTask,但現在它的結果是NPE

09-12 23:36:18.443: E/AndroidRuntime(24715): Caused by: java.lang.NullPointerException 
09-12 23:36:18.443: E/AndroidRuntime(24715): at com.example.dbms.UploadActivity$Upload.doInBackground(UploadActivity.java:51) 

,並考慮代碼如下:

public class UploadActivity extends Activity { 

private ProgressDialog dialog = null; 
Cloudinary cloudinary; 
JSONObject Result; 
String file_path; 
File file; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_upload); 

    Intent i = getIntent(); 
    file_path = i.getStringExtra("file"); 
    HashMap config = new HashMap(); 
    config.put("cloud_name", "dbms"); 
    config.put("api_key", "key");//I have changed the key and secret 
    config.put("api_secret", "secret"); 
    Cloudinary cloudinary = new Cloudinary(config); 
    TextView textView = (TextView) findViewById(R.id.text5); 
    textView.setText(file_path); 
    file = new File(file_path); 
} 

private class Upload extends AsyncTask<String, Void, String> { 
    @Override 
    protected String doInBackground(String... urls) { 
     String response = ""; 

     try { 
      Result = cloudinary.uploader().upload(file, 
        Cloudinary.emptyMap()); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
     return response; 
    } 

    @Override 
    protected void onPostExecute(String result) { 
     TextView textView = (TextView) findViewById(R.id.text5); 
     textView.setText("file uploaded"); 
    } 
} 

public void onClick(View view) { 
    Upload task = new Upload(); 
    task.execute(new String[] { "http://www.vogella.com" }); 

} 

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

} 

在關注該生產線是:

Result = cloudinary.uploader().upload(file, 
        Cloudinary.emptyMap()); 
+0

瞭解什麼是空(可能是'cloudinary'),並確保它不是」噸。 – 323go 2014-09-12 20:20:01

回答

4

您已經定義cloudinary兩次 - 一次在課堂上,和一次在onCreate()。當您將值分配給onCreate()中的值時,該類中的cloudinary成員保持爲空,所以您的應用程序崩潰。這將是最好的實例傳遞到的AsyncTask,如:

onCreate()變化

Cloudinary cloudinary = new Cloudinary(config); 

cloudinary = new Cloudinary(config); 

更改Upload到:

private class Upload extends AsyncTask<String, Void, String> { 
    private Cloudinary mCloudinary; 

    public Upload(Cloudinary cloudinary) { 
     super(); 
     mCloudinary = cloudinary; 
    } 

,並在doInBackground(),更改:

Result = cloudinary.uploader().upload(file, 
        Cloudinary.emptyMap()); 

Result = mCloudinary.uploader().upload(file, 
        Cloudinary.emptyMap()); 

最後,改變你的onClick()

public void onClick(View view) { 
    Upload task = new Upload(); 
    task.execute(new String[] { "http://www.vogella.com" }); 

} 

public void onClick(View view) { 
    Upload task = new Upload(cloudinary); 
    task.execute(new String[] { "http://www.vogella.com" }); 

} 
+0

就是我的想法。愚蠢的錯誤! Thanx的方式.. – thestrongenough 2014-09-12 20:36:14

+0

很高興我能幫助! – 323go 2014-09-12 20:54:56