自从我升级到谷歌应用引擎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问题吗? 我需要尽快排序,因为这是我在大学的最后一年项目,我还有几天要完成:(
由于
答案 0 :(得分:2)
您宣布tracks
;
Tracks tracks;
......并使用它......
tracks.setTrackName(String.valueOf(etTrackName.getText()));
...但绝不会在课堂上用new Tracks()
初始化它。我在saveTrackToCloud
方法中将其声明为;
Tracks tracks = new Tracks();
...并删除实例变量,因为不再需要它。最好将变量保持在本地,这样您就可以清楚地看到它们是否已初始化。