2014-01-18 71 views
4

重要提示:您可以使用:負荷ImageView的圖像(Parse.com)

ParseImageView

輕鬆地處理這個過程。它已經在2014年左右在Parse中可用。希望它可以幫助人們通過谷歌到達這裏。


我想從Parse.com加載一個圖像,對象id方便的ImageView。但不知何故,從解析中獲取圖像時,應用程序崩潰。我無法找到問題的線索。我在佈局中有6個圖像,現在我試圖僅在1個ImageView中加載圖像,其餘的我從Drawable中指定它們的源代碼。請幫忙 !!

public class Login extends Activity { 
EditText fullname, mobilenumber, occupation; 
Button save; 
ImageView ad2,ad3,ad4,ad5,ad6; 
HorizontalScrollView horizontalScrollView1; 
    private ProgressDialog progressDialog; 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.userdata); 
    fullname = (EditText) findViewById(R.id.fullname) ; 
    mobilenumber = (EditText) findViewById(R.id.mobile) ; 
    occupation = (EditText) findViewById(R.id.occupation) ; 
    save=(Button) findViewById(R.id.btnSave);  
    horizontalScrollView1=(HorizontalScrollView) findViewById(R.id.horizontalScrollView1); 
    //ad1=(ImageView) findViewById(R.id.ad1); 
    ad2=(ImageView) findViewById(R.id.ad2); 
    ad3=(ImageView) findViewById(R.id.ad3); 
    ad4=(ImageView) findViewById(R.id.ad4); 
    ad5=(ImageView) findViewById(R.id.ad5); 
    ad6=(ImageView) findViewById(R.id.ad6); 
    progressDialog = ProgressDialog.show(Login.this, "","Downloading Image...", true); 
    // Locate the class table named "Footer" in Parse.com 
    ParseQuery<ParseObject> query = new ParseQuery<ParseObject>("Footer"); 
    // Locate the objectId from the class 
    query.getInBackground("tNp607WyQD", new GetCallback<ParseObject>() { 
       public void done(ParseObject object,ParseException e) { 
        // TODO Auto-generated method stub 
        // Locate the column named "ImageName" and set 
        // the string 
        ParseFile fileObject = (ParseFile) object.get("imageFile"); 
        fileObject.getDataInBackground(new GetDataCallback() { 
           public void done(byte[] data, 
             ParseException e) { 
            if (e == null) { 
             Log.d("test", 
               "We've got data in data."); 
             // Decode the Byte[] into 
             // Bitmap 
             Bitmap bmp = BitmapFactory.decodeByteArray(data, 0,data.length); 
             // Get the ImageView from main.xml 
             //ImageView image = (ImageView) findViewById(R.id.ad1); 
             ImageView ad1=(ImageView) findViewById(R.id.ad1); 
             // Set the Bitmap into the 
             // ImageView 
             ad1.setImageBitmap(bmp); 
             // Close progress dialog 
             progressDialog.dismiss(); 
            } else { 
             Log.d("test", 
               "There was a problem downloading the data."); 
            } 
           } 
          }); 
       } 
      }); 
}} 

版式文件:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:orientation="vertical" 
android:padding="10dip" > 

<!-- Full Name Label --> 

<TextView 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="Full Name" 
    android:textColor="#372c24" 
    tools:ignore="HardcodedText" /> 

<EditText 
    android:id="@+id/fullname" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_marginBottom="10dip" 
    android:layout_marginTop="5dip" 
    android:singleLine="true" /> 
<!-- Email Label --> 

<TextView 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="Mobile number" 
    android:textColor="#372c24" 
    tools:ignore="HardcodedText" /> 

<EditText 
    android:id="@+id/mobile" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_marginBottom="10dip" 
    android:layout_marginTop="5dip" 
    android:singleLine="true" 
    android:inputType="phone" /> 


<!-- Password Label --> 

<TextView 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="Occupation" 
    android:textColor="#372c24" 
    tools:ignore="HardcodedText" /> 

<EditText 
    android:id="@+id/occupation" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_marginTop="10dip" 
    android:singleLine="true" 
    tools:ignore="TextFields" /> 

<!-- Register Button --> 

<Button 
    android:id="@+id/btnSave" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_marginTop="30dip" 
    android:text="Save" 
    tools:ignore="HardcodedText" /> 
<RelativeLayout 
android:layout_width="match_parent" 
android:layout_height="match_parent"  
android:background="#ffffff"> 
    <LinearLayout 
    android:id="@+id/footer" 
    android:orientation="horizontal" 
    android:layout_width="match_parent" 
    android:layout_height="90dp" 
    android:background="#5C03" 
    android:layout_alignParentBottom="true">  
<ImageView 
android:id="@+id/ad1" 
android:layout_width="90dp" 
android:layout_height="wrap_content" /> 
<HorizontalScrollView 
android:id="@+id/horizontalScrollView1" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" > 
<LinearLayout 
android:layout_width="wrap_content" 
android:layout_height="match_parent" 
android:orientation="horizontal" > 
<ImageView 
    android:id="@+id/ad2" 
    android:layout_width="90dp" 
    android:layout_height="wrap_content" 
    android:layout_margin="3dp" 
    android:src="@drawable/bg" /> 
    <ImageView 
    android:id="@+id/ad3" 
    android:layout_margin="3dp" 
    android:src="@drawable/bg" 
    android:layout_width="90dp" 
    android:layout_height="wrap_content"/> 
    <ImageView 
    android:id="@+id/ad4" 
    android:layout_margin="3dp" 
    android:src="@drawable/bg" 
    android:layout_width="90dp" 
    android:layout_height="wrap_content" /> 
    <ImageView 
    android:id="@+id/ad5" 
    android:layout_margin="3dp" 
    android:layout_width="90dp" 
    android:src="@drawable/bg" 
    android:layout_height="wrap_content" /> 
    <ImageView 
    android:id="@+id/ad6" 
    android:layout_margin="3dp" 
    android:layout_width="90dp" 
    android:src="@drawable/bg" 
    android:layout_height="wrap_content" /> 
    </LinearLayout> 
</HorizontalScrollView> 
</LinearLayout> 

的logcat:

01-18 20:31:07.290: E/AndroidRuntime(2420): Uncaught handler: thread main exiting due to uncaught exception 
01-18 20:31:07.370: E/AndroidRuntime(2420): java.lang.NullPointerException 
01-18 20:31:07.370: E/AndroidRuntime(2420):  at com.mixorg.parsefooter.Login$1.done(Login.java:51) 
01-18 20:31:07.370: E/AndroidRuntime(2420):  at com.parse.GetCallback.internalDone(GetCallback.java:43) 
01-18 20:31:07.370: E/AndroidRuntime(2420):  at com.parse.GetCallback.internalDone(GetCallback.java:1) 
01-18 20:31:07.370: E/AndroidRuntime(2420):  at com.parse.Parse$6$1.run(Parse.java:818) 
01-18 20:31:07.370: E/AndroidRuntime(2420):  at android.os.Handler.handleCallback(Handler.java:587) 
01-18 20:31:07.370: E/AndroidRuntime(2420):  at android.os.Handler.dispatchMessage(Handler.java:92) 
01-18 20:31:07.370: E/AndroidRuntime(2420):  at android.os.Looper.loop(Looper.java:123) 
01-18 20:31:07.370: E/AndroidRuntime(2420):  at android.app.ActivityThread.main(ActivityThread.java:4370) 
01-18 20:31:07.370: E/AndroidRuntime(2420):  at java.lang.reflect.Method.invokeNative(Native Method) 
01-18 20:31:07.370: E/AndroidRuntime(2420):  at java.lang.reflect.Method.invoke(Method.java:521) 
01-18 20:31:07.370: E/AndroidRuntime(2420):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) 
01-18 20:31:07.370: E/AndroidRuntime(2420):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 
01-18 20:31:07.370: E/AndroidRuntime(2420):  at dalvik.system.NativeStart.main(Native Method) 
01-18 20:31:07.430: E/SemcCheckin(2420): Get crash dump level : java.io.FileNotFoundException: /data/semc-checkin/crashdump 
+0

如果你'不知道問題在哪裏',搜索答案的最佳位置是logcat。 – FWeigl

+1

@Ascorbin Logcat添加,如果你可以幫忙? – addy123

+0

檢查byte []數據是否爲空。 – Egor

回答

1

我自己解決了。其實這是一個可怕的錯誤,而不是在Android清單文件中添加兩個權限。 :

<uses-permission android:name="android.permission.INTERNET" /> 
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 

謝謝大家!! :)

0

按照logcat的轉儲,你有Login.java線51空變量。調試代碼,並找出原因。

+0

我在第51行得到異常:'ParseFile fileObject =(ParseFile)object.get(「imageFile」);' – addy123

+0

你需要檢查'e'是否不爲null,你最有可能得到'ParseException',這就是對象爲null的原因。在第51行之前檢查它。 –