2012-02-07 51 views
0

好吧,我們是Android的新手。我正在開發一個登錄頁面,將用戶註冊到後端服務器。我使用的後端服務是Parse。所以,當我包含一些代碼寫onclick方法的數據時,我得到一個錯誤,顯示有一個未處理的異常。Android中未處理的異常

下面是頁面的XML文件:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" > 

    <TextView 
     android:id="@+id/acchead" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentTop="true" 
     android:gravity="center" 
     android:text="Create a FFOD account" 
     android:textSize="25dp" /> 

    <EditText 
     android:id="@+id/fnameffod" 
     android:layout_width="180dp" 
     android:layout_height="wrap_content" 
     android:layout_marginLeft="120dp" 
     android:layout_marginTop="40dp" /> 

    <TextView 
     android:id="@+id/lname" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_marginTop="100dp" 
     android:gravity="center" 
     android:text="Last Name" 
     android:textSize="20dp" /> 

    <EditText 
     android:id="@+id/lnameffod" 
     android:layout_width="180dp" 
     android:layout_height="wrap_content" 
     android:layout_marginLeft="120dp" 
     android:layout_marginTop="90dp" /> 

     <TextView 
     android:id="@+id/uname" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignBaseline="@+id/userffod" 
     android:layout_alignBottom="@+id/userffod" 
     android:layout_alignParentLeft="true" 
     android:gravity="center" 
     android:text="Username" 
     android:textSize="20dp" /> 

    <EditText 
     android:id="@+id/userffod" 
     android:layout_width="180dp" 
     android:layout_height="wrap_content" 
     android:layout_marginLeft="120dp" 
     android:layout_marginTop="140dp" /> 

    <TextView 
     android:id="@+id/email" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignBaseline="@+id/emailffod" 
     android:layout_alignBottom="@+id/emailffod" 
     android:layout_alignParentLeft="true" 
     android:gravity="center" 
     android:text="Email" 
     android:textSize="20dp" /> 

    <EditText 
     android:id="@+id/emailffod" 
     android:layout_width="180dp" 
     android:layout_height="wrap_content" 
     android:layout_marginLeft="120dp" 
     android:layout_marginTop="190dp" /> 

    <TextView 
     android:id="@+id/pass" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignBaseline="@+id/passffod" 
     android:layout_alignBottom="@+id/passffod" 
     android:layout_alignParentLeft="true" 
     android:gravity="center" 
     android:text="Password" 
     android:textSize="20dp" /> 

    <EditText 
     android:id="@+id/passffod" 
     android:layout_width="180dp" 
     android:layout_height="wrap_content" 
     android:layout_marginLeft="120dp" 
     android:layout_marginTop="240dp" 
     android:password="true" /> 


    <ImageButton 
     android:id="@+id/Btwitter1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:layout_alignRight="@+id/passffod" 
     android:layout_marginBottom="16dp" 
     android:layout_marginRight="26dp" 
     android:src="@drawable/twitter_icon" /> 

    <ImageButton 
     android:id="@+id/Bfacebook1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignBottom="@+id/Btwitter1" 
     android:layout_toLeftOf="@+id/Bcreate" 
     android:src="@drawable/facebook_icon2" /> 


    <Button 
     android:id="@+id/Bcreate" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/pass" 
     android:layout_marginBottom="14dp" 
     android:layout_alignLeft="@+id/passffod" 
     android:gravity="center" 
     android:text="Create Account" /> 

    <TextView 
     android:id="@+id/fbtwitt" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_below="@+id/Bcreate" 
     android:gravity="center" 
     android:text="Use FACEBOOK or TWITTER account? Touch any one!" 
     android:textSize="17dp" /> 

    <TextView 
     android:id="@+id/fname" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignBaseline="@+id/fnameffod" 
     android:layout_alignBottom="@+id/fnameffod" 
     android:layout_alignRight="@+id/pass" 
     android:gravity="center" 
     android:text="First Name" 
     android:textSize="15dp" /> 

</RelativeLayout> 

針對上述XML的java文件如下:

package com.ffod.bingo; 


import com.parse.Parse; 
import com.parse.ParseException; 
import com.parse.ParseUser; 
import com.parse.SignUpCallback; 

import android.app.Activity; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.Button; 
import android.widget.EditText; 

public class AccountCreate extends Activity { 

    String username, pass, email, firstname, lastname; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     // TODO Auto-generated method stub 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.login); 
     Parse.initialize(this, "my app key", "my app key"); 

     Button register_user; 
     EditText user_name, password, e_mail, f_name, l_name; 
     user_name = (EditText)findViewById(R.id.userffod); 
     password = (EditText)findViewById(R.id.passffod); 
     e_mail = (EditText)findViewById(R.id.emailffod); 
     f_name = (EditText)findViewById(R.id.fnameffod); 
     l_name = (EditText)findViewById(R.id.lnameffod); 

     username = user_name.getText().toString(); 
     pass = password.getText().toString(); 
     email = e_mail.getText().toString(); 
     firstname = f_name.getText().toString(); 
     lastname = l_name.getText().toString(); 

     register_user = (Button) findViewById(R.id.Bcreate); 
     register_user.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       // TODO Auto-generated method stub 
       ParseUser user = new ParseUser(); 
       user.setUsername(username); 
       user.setPassword(pass); 
       user.setEmail(email); 
       user.put("First Name", firstname); 
       user.put("Last Name", lastname); 
       user.signUpInBackground(new SignUpCallback() { 
        public void done(ParseException e) { 
         if (e == null) { 
          // Hooray! Let them use the application now. 
         } else { 
          // Sign up didn't succeed. Look at the ParseException 
          // to figure out what went wrong 
         } 
        } 
       }); 
      } 
     });  
    } 

} 

請注意,我只是改變了實際值parse.initialize簡單地appkeys。

這裏是日誌貓O/P:

>02-07 10:22:17.242: D/com.parse.ParseBroadcastReceiver(284): received >android.intent.action.USER_PRESENT 
>02-07 10:22:33.101: W/KeyCharacterMap(284): No keyboard for id 0 
>02-07 10:22:33.101: W/KeyCharacterMap(284): Using default keymap: /system/usr/keychars/qwerty.kcm.bin 
>02-07 10:22:55.262: W/dalvikvm(284): threadid=7: thread exiting with uncaught exception (group=0x4001d800) 
>02-07 10:22:55.335: E/AndroidRuntime(284): FATAL EXCEPTION: AsyncTask #1 
>02-07 10:22:55.335: E/AndroidRuntime(284): java.lang.RuntimeException: An error occured while executing doInBackground() 
>02-07 10:22:55.335: E/AndroidRuntime(284):  at android.os.AsyncTask$3.done(AsyncTask.java:200) 
>02-07 10:22:55.335: E/AndroidRuntime(284):  at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273) 
>02-07 10:22:55.335: E/AndroidRuntime(284):  at java.util.concurrent.FutureTask.setException(FutureTask.java:124) 
>02-07 10:22:55.335: E/AndroidRuntime(284):  at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307) 
>02-07 10:22:55.335: E/AndroidRuntime(284):  at java.util.concurrent.FutureTask.run(FutureTask.java:137) 
>02-07 10:22:55.335: E/AndroidRuntime(284):  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1068) 
>02-07 10:22:55.335: E/AndroidRuntime(284):  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:561) 
>02-07 10:22:55.335: E/AndroidRuntime(284):  at java.lang.Thread.run(Thread.java:1096) 
>02-07 10:22:55.335: E/AndroidRuntime(284): Caused by: java.lang.IllegalArgumentException: Username cannot be missing or blank 
>02-07 10:22:55.335: E/AndroidRuntime(284):  at com.parse.ParseUser.signUp(ParseUser.java:232) 
>02-07 10:22:55.335: E/AndroidRuntime(284):  at com.parse.ParseUser$1.run(ParseUser.java:273) 
>02-07 10:22:55.335: E/AndroidRuntime(284):  at com.parse.ParseUser$1.run(ParseUser.java:1) 
>02-07 10:22:55.335: E/AndroidRuntime(284):  at com.parse.BackgroundTask.doInBackground(BackgroundTask.java:39) 
>02-07 10:22:55.335: E/AndroidRuntime(284):  at com.parse.BackgroundTask.doInBackground(BackgroundTask.java:1) 
>02-07 10:22:55.335: E/AndroidRuntime(284):  at android.os.AsyncTask$2.call(AsyncTask.java:185) 
>02-07 10:22:55.335: E/AndroidRuntime(284):  at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305) 
>02-07 10:22:55.335: E/AndroidRuntime(284):  ... 4 more 
>02-07 10:23:06.851: I/Process(284): Sending signal. PID: 284 SIG: 9 

這是一個非常迫切的要求。我會很感激所有的幫助,我可以得到。謝謝!

+0

您的ParseUser.signup()拋出錯誤:IllegalArgumentException,請檢查那裏或粘貼ParseUser.signup的代碼。 – jeet 2012-02-07 05:26:49

+0

'> 02-07 10:22:55.335:E/AndroidRuntime(284):引起:java.lang.IllegalArgumentException:用戶名不能丟失或空白::這行說明它到底是什麼。請雙擊'02-07 10:22:55.335:E/AndroidRuntime(284):在你的logcat的com.parse.ParseUser.signUp(ParseUser.java:232) '行。 – Ghost 2012-02-07 05:27:06

+0

檢查正在傳遞的userName的值。 – SAMD 2012-02-07 05:49:06

回答

2

的問題可能是因爲這條線的,

username = user_name.getText().toString(); 

我想你可能不會拿在其中的EditText可能返回這將導致該問題的空值的任何值。

或由於這是一個EditText而不是user_name.getText().toString();我建議您使用user_name.getEditableText().toString();

問題是因爲這裏的值爲空,導致您的其他課程中出現一些異常。

+0

Andro Selva:使用getEditableText()似乎沒有幫助。 – 2012-02-07 15:33:11

+0

您是否獲得用戶名的某些值或是否爲空? – 2012-02-07 15:34:59

+0

@jitendrasharma:註冊是解析服務給出的一個函數。 – 2012-02-07 15:40:46