空对象引用Android Azure

时间:2016-01-16 15:09:38

标签: android azure azure-mobile-services

我在Android上遇到Azure移动服务的问题,我看到了ExDople ToDoItem,我创建了另一个名为User的实例来保存数据。 这是代码

public class User {

@com.google.gson.annotations.SerializedName("email")
private String email;

@com.google.gson.annotations.SerializedName("password")
private String password;

@com.google.gson.annotations.SerializedName("name")
private String name;

@com.google.gson.annotations.SerializedName("lastname")
private String lastname;

@com.google.gson.annotations.SerializedName("phone")
private String phone;

public User(){

}
public User(String email, String password, String name, String lastname, String phone){

    this.setEmail(email);
    this.setPassword(password);
    this.setName(name);
    this.setLastname(lastname);
    this.setPhone(phone);
}

public String getEmail() {
    return email;
}

public String getPassword() {
    return password;
}

public String getName() {
    return name;
}

public String getLastname() {
    return lastname;
}

public String getPhone() {
    return phone;
}

public void setEmail(String email) {
    this.email = email;
}

public void setPassword(String password) {
    this.password = password;
}

public void setName(String name) {
    this.name = name;
}

public void setLastname(String lastname) {
    this.lastname = lastname;
}

public void setPhone(String phone) {
    this.phone = phone;
}

}

这是UserActivity的代码

public class UserActivity extends Activity {

private MobileServiceClient mUser;

private MobileServiceTable<User> mToDoTable;


private EditText editTextEmail;
private EditText editTextMP;
private EditText editTextName;
private EditText editTextLastName;
private EditText editTextPhone;

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

    editTextEmail = (EditText) findViewById(R.id.editTextEmail);
    editTextMP = (EditText) findViewById(R.id.editTextMP);
    editTextName = (EditText) findViewById(R.id.editTextName);
    editTextLastName = (EditText) findViewById(R.id.editTextLastName);
    editTextPhone = (EditText) findViewById(R.id.editTextPhone);
    try {
        // Create the Mobile Service Client instance, using the provided

        // Mobile Service URL and key
        mUser = new MobileServiceClient(
                "******",
                "******",
                this);


    } catch (MalformedURLException e) {
        createAndShowDialog(new Exception("There was an error creating the Mobile Service. Verify the URL"), "Error");
    }


}
public User addItemInTable(User user) throws ExecutionException, InterruptedException {
    User entity = mToDoTable.insert(user).get();
    return entity;
}

private AsyncTask<Void, Void, Void> runAsyncTask(AsyncTask<Void, Void, Void> task) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
        return task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
    } else {
        return task.execute();
    }
}
public void addItem(View view) {
    if (mUser == null) {
        return;
    }

    final User user = new User();
    user.setEmail(editTextEmail.getText().toString());
    user.setPassword(editTextMP.getText().toString());
    user.setName(editTextName.getText().toString());
    user.setLastname(editTextLastName.getText().toString());
    user.setPhone(editTextPhone.getText().toString());


    // Insert the new item
    AsyncTask<Void, Void, Void> task = new AsyncTask<Void, Void, Void>(){
        @Override
        protected Void doInBackground(Void... params) {
            try {
                final User entity = addItemInTable(user);
            } catch (final Exception e) {
                createAndShowDialogFromTask(e, "Error");
            }
            return null;
        }
    };

    runAsyncTask(task);
    editTextEmail.setText("");
    editTextMP.setText("");
    editTextName.setText("");
    editTextLastName.setText("");
    editTextPhone.setText("");
}

/**
 * Creates a dialog and shows it
 *
 * @param exception
 *            The exception to show in the dialog
 * @param title
 *            The dialog title
 */
private void createAndShowDialogFromTask(final Exception exception, String title) {
    runOnUiThread(new Runnable() {
        @Override
        public void run() {
            createAndShowDialog(exception, "Error");
        }
    });
}


/**
 * Creates a dialog and shows it
 *
 * @param exception
 *            The exception to show in the dialog
 * @param title
 *            The dialog title
 */
private void createAndShowDialog(Exception exception, String title) {
    Throwable ex = exception;
    if(exception.getCause() != null){
        ex = exception.getCause();
    }
    createAndShowDialog(ex.getMessage(), title);
}

/**
 * Creates a dialog and shows it
 *
 * @param message
 *            The dialog message
 * @param title
 *            The dialog title
 */
private void createAndShowDialog(final String message, final String title) {
    final AlertDialog.Builder builder = new AlertDialog.Builder(this);

    builder.setMessage(message);
    builder.setTitle(title);
    builder.create().show();
}
private AsyncTask<Void, Void, Void> initLocalStore() throws MobileServiceLocalStoreException, ExecutionException, InterruptedException {

    AsyncTask<Void, Void, Void> task = new AsyncTask<Void, Void, Void>() {
        @Override
        protected Void doInBackground(Void... params) {
            try {

                MobileServiceSyncContext syncContext = mUser.getSyncContext();

                if (syncContext.isInitialized())
                    return null;

                SQLiteLocalStore localStore = new SQLiteLocalStore(mUser.getContext(), "OfflineStore", null, 1);

                Map<String, ColumnDataType> tableDefinition = new HashMap<String, ColumnDataType>();
                tableDefinition.put("email", ColumnDataType.String);
                tableDefinition.put("password", ColumnDataType.String);
                tableDefinition.put("name", ColumnDataType.String);
                tableDefinition.put("lastname", ColumnDataType.String);
                tableDefinition.put("phone", ColumnDataType.String);

                localStore.defineTable("User", tableDefinition);

                SimpleSyncHandler handler = new SimpleSyncHandler();

                syncContext.initialize(localStore, handler).get();

            } catch (final Exception e) {
                createAndShowDialogFromTask(e, "Error");
            }

            return null;
        }
    };

    return runAsyncTask(task);
}

}

点击按钮后,显示异常: 尝试调用虚拟方法com.google.common.util.concurrent.ListenableFutre com.microsoft.windowsazure.mobileservices.table.MobileServiceTable.insert(java.lang.Object)&#39;在null对象上。 任何帮助

1 个答案:

答案 0 :(得分:1)

您在类中声明了字段mToDoTable,但从未对其进行初始化。您可以在创建MobileServiceClient实例后初始化它:

try {
    // Create the Mobile Service Client instance, using the provided

    // Mobile Service URL and key
    mUser = new MobileServiceClient(
            "******",
            "******",
            this);
    mToDoTable = mUser.getTable("User", User.class);
} catch (...)

您还需要向User类Id添加一个新成员,以表示数据库中用户的标识符。

public class User {
    // ...

    @com.google.gson.annotations.SerializedName("id")
    private String id;

    // You can also add the get/set if you want.
}