自从我升级到Google App Engine 1.7.7之后,就无法在Android应用程序上设置实体数据。我收到一个nullpointer异常错误。在GAE的早期版本中,我没有这个问题。这是我的代码
public class SaveTrackActivity extends Activity {
static String TAG = "SaveTrackActivity";
Tracksendpoint endpoint;
private TrackRateApp trackapp;
private EditText etTrackName;
private EditText etTrackDes;
private ImageView ivTrackPic;
Bitmap trackImage;
Tracks tracks;
int numBackEndTasks;
String encodedImage;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_save_track);
initialize();
Intent saveTrackIntent = getIntent();
trackImage = (Bitmap)saveTrackIntent.getParcelableExtra("BitMapImage");
ivTrackPic.setImageBitmap(trackImage);
}
private void initialize() {
trackapp = (TrackRateApp)getApplication();
endpoint = trackapp.getEndpoint();
etTrackName = (EditText) findViewById(R.id.etTrackName);
etTrackDes = (EditText) findViewById(R.id.edTrackDescr);
ivTrackPic = (ImageView) findViewById(R.id.trackImage);
}
public void saveTrackToCloud(View v){
ByteArrayOutputStream baos = new ByteArrayOutputStream();
trackImage.compress(Bitmap.CompressFormat.PNG, 100 , baos);
byte[] b = baos.toByteArray();
encodedImage = Base64.encodeToString(b, Base64.DEFAULT);
Log.d(TAG, "GeopointArray size is: " + trackapp.getGeoPtArray().size());
tracks.setTrackName(String.valueOf(etTrackName.getText()));
tracks.setTrackDescription(String.valueOf(etTrackDes.getText()));
tracks.setTrackImage(encodedImage);
tracks.setRoute(trackapp.getGeoPtArray());
addTracks(tracks);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.save_track, menu);
return true;
}
public void addTracks(Tracks tracks) {
new AsyncAddTrack(this).execute(tracks);
}
}
当我尝试设置数据
tracks.setTrackName(String.valueOf(etTrackName.getText())); tracks.setTrackDescription(String.valueOf(etTrackDes.getText())); tracks.setTrackImage(encodedImage); tracks.setRoute(trackapp.getGeoPtArray());
时出现问题我得到这个错误
05-01 12:41:19.522: E/AndroidRuntime(6858): FATAL EXCEPTION: main
05-01 12:41:19.522: E/AndroidRuntime(6858): java.lang.IllegalStateException: Could not execute method of the activity
05-01 12:41:19.522: E/AndroidRuntime(6858): at android.view.View$1.onClick(View.java:3691)
05-01 12:41:19.522: E/AndroidRuntime(6858): at android.view.View.performClick(View.java:4211)
05-01 12:41:19.522: E/AndroidRuntime(6858): at android.view.View$PerformClick.run(View.java:17267)
05-01 12:41:19.522: E/AndroidRuntime(6858): at android.os.Handler.handleCallback(Handler.java:615)
05-01 12:41:19.522: E/AndroidRuntime(6858): at android.os.Handler.dispatchMessage(Handler.java:92)
05-01 12:41:19.522: E/AndroidRuntime(6858): at android.os.Looper.loop(Looper.java:137)
05-01 12:41:19.522: E/AndroidRuntime(6858): at android.app.ActivityThread.main(ActivityThread.java:4898)
05-01 12:41:19.522: E/AndroidRuntime(6858): at java.lang.reflect.Method.invokeNative(Native Method)
05-01 12:41:19.522: E/AndroidRuntime(6858): at java.lang.reflect.Method.invoke(Method.java:511)
05-01 12:41:19.522: E/AndroidRuntime(6858): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
05-01 12:41:19.522: E/AndroidRuntime(6858): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
05-01 12:41:19.522: E/AndroidRuntime(6858): at dalvik.system.NativeStart.main(Native Method)
05-01 12:41:19.522: E/AndroidRuntime(6858): Caused by: java.lang.reflect.InvocationTargetException
05-01 12:41:19.522: E/AndroidRuntime(6858): at java.lang.reflect.Method.invokeNative(Native Method)
05-01 12:41:19.522: E/AndroidRuntime(6858): at java.lang.reflect.Method.invoke(Method.java:511)
05-01 12:41:19.522: E/AndroidRuntime(6858): at android.view.View$1.onClick(View.java:3686)
05-01 12:41:19.522: E/AndroidRuntime(6858): ... 11 more
05-01 12:41:19.522: E/AndroidRuntime(6858): Caused by: java.lang.NullPointerException
05-01 12:41:19.522: E/AndroidRuntime(6858): at com.jman.trackrate.SaveTrackActivity.saveTrackToCloud(SaveTrackActivity.java:60)
05-01 12:41:19.522: E/AndroidRuntime(6858): ... 14 more
有人可以帮忙吗,或者这是已知的GAE问题?
我需要尽快获得这种排序,因为这是我在大学中的最后一个项目,而且我只剩下几天要完成:(
谢谢
最佳答案
您在声明tracks
;
Tracks tracks;
...并使用它...
tracks.setTrackName(String.valueOf(etTrackName.getText()));
...但是不要在类中使用
new Tracks()
对其进行初始化。我会在saveTrackToCloud
方法中将其声明为;Tracks tracks = new Tracks();
...并删除实例变量,因为不再需要它。最好将变量放在局部,这样您就可以清楚地看到它们是否已初始化。