我正在关注Google tutorial中提供的示例,为我的Android应用创建Google App Engine后端。 在本教程中,MainActivity如下:
import java.io.IOException;
import java.util.Date;
import android.os.AsyncTask;
import android.content.Context;
import com.cloudnotes.noteendpoint.Noteendpoint;
import com.cloudnotes.noteendpoint.model.Note;
import com.google.api.client.extensions.android.http.AndroidHttp;
import com.google.api.client.http.HttpRequest;
import com.google.api.client.http.HttpRequestInitializer;
import com.google.api.client.json.jackson.JacksonFactory;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new EndpointsTask().execute(getApplicationContext());
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
public class EndpointsTask extends AsyncTask<Context, Integer, Long> {
protected Long doInBackground(Context... contexts) {
Noteendpoint.Builder endpointBuilder = new Noteendpoint.Builder(
AndroidHttp.newCompatibleTransport(),
new JacksonFactory(),
new HttpRequestInitializer() {
public void initialize(HttpRequest httpRequest) { }
});
Noteendpoint endpoint = CloudEndpointUtils.updateBuilder(
endpointBuilder).build();
try {
Note note = new Note().setDescription("Note Description");
String noteID = new Date().toString();
note.setId(noteID);
note.setEmailAddress("E-Mail Address");
Note result = endpoint.insertNote(note).execute();
} catch (IOException e) {
e.printStackTrace();
}
return (long) 0;
}
}
}
这种情况很糟糕,每次运行应用程序时都会创建实体。所以我通过添加textfields
来修改它,以便从用户那里获取description
和email
的值并得到此信息作为新的MainActivity
public class MainActivity extends Activity implements View.OnClickListener{
EditText descriptionTF;
EditText emailTF;
Button submitBtn;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
submitBtn = (Button) findViewById(R.id.submitButton);
descriptionTF = (EditText) findViewById(R.id.descriptionTextField);
emailTF = (EditText) findViewById(R.id.emailTextField);
}
public class EndpointsTask extends AsyncTask<Context, Integer, Long> {
protected Long doInBackground(Context... contexts) {
Noteendpoint.Builder endpointBuilder = new Noteendpoint.Builder(
AndroidHttp.newCompatibleTransport(),
new JacksonFactory(),
new HttpRequestInitializer() {
public void initialize(HttpRequest httpRequest) { }
});
Noteendpoint endpoint = CloudEndpointUtils.updateBuilder(
endpointBuilder).build();
String descrptn = descriptionTF.getText().toString();
String email = emailTF.getText().toString();
String noteID = new Date().toString();
try {
Note note = new Note();
note.setDescription(descrptn);
note.setId(noteID);
note.setEmailAddress(email);
Note result = endpoint.insertNote(note).execute();
} catch (IOException e) {
e.printStackTrace();
}
return (long) 0;
}
}
@Override
public void onClick(View v) {
if (v.getId()== R.id.submitButton) {
new EndpointsTask().execute(getApplicationContext());
}
}
}
当我点击submit
按钮时,没有任何反应。
我猜测问题来自于我从new EndpointsTask().execute(getApplicationContext());
方法调用onClick()
而不是像教程中所做的那样onCreate()
方法。有人知道解决这个问题吗?
答案 0 :(得分:1)
最后通过将onClick()
方法移动到onCreate()
方法解决了这个问题:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
submitBtn = (Button) findViewById(R.id.submitButton);
descriptionTF = (EditText) findViewById(R.id.descriptionTextField);
emailTF = (EditText) findViewById(R.id.emailTextField);
submitBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (v.getId()== R.id.submitButton) {
new EndpointsTask().execute(getApplicationContext());
}
}
});
}