我正在创建一个应用程序,用户可以购买使用拖放API的建筑物(图像按钮)。购买新建筑物时,建筑物的ID将使用共享偏好设置进行存储,以便在应用程序打开备份时,已知它已被购买并保存坐标,因此可以将建筑物加载到停止的位置。我现在的问题是在创建一个建筑之前,ID是null,所以我得到一个nullpointerexception,因为它知道它是null。我将发布代码和logcat,以便您看到我的问题。
以下是检查建筑物是否存在的代码:
prefs = getSharedPreferences("Buildings", MODE_PRIVATE);
newColonyHutID = prefs.getInt("NewColonyHut", 0);
if (newColonyHutID != 0)
{
prefs = getSharedPreferences("Coordinates", MODE_PRIVATE);
colonyHutX = prefs.getInt("ColonyHutX", 0);
colonyHutY = prefs.getInt("ColonyHutY", 0);
Toast.makeText(getApplicationContext(), "X: " + colonyHutX + " Y: " + colonyHutY, Toast.LENGTH_SHORT).show();
newColonyHutFrame = (FrameLayout) findViewById(R.id.bottomHalf);
newColonyHut = new ImageButton(runGraphics.this);
newColonyHut.setBackgroundResource(R.drawable.mainhut);
newColonyHut.setX(colonyHutX);
newColonyHut.setY(colonyHutY);
newColonyHut.setTag("NewColonyHut");
newColonyHut.setOnTouchListener(new BuildingsClick());
findViewById(R.id.topHalf).setOnDragListener(new ColonyHutDrag(getApplicationContext()));
findViewById(R.id.bottomHalf).setOnDragListener(new ColonyHutDrag(getApplicationContext()));
FrameLayout.LayoutParams param1 = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT);
newColonyHutFrame.addView(newColonyHut, param1);
}//end if
以下是创建建筑物并保存ID的代码:
newColonyHutFrame = (FrameLayout) findViewById(R.id.bottomHalf);
newColonyHut = new ImageButton(runGraphics.this);
newColonyHut.setBackgroundResource(R.drawable.mainhut);
prefs = getSharedPreferences("Buildings", MODE_PRIVATE);
if (v.getId() != 0)
{
Editor edit= prefs.edit();
edit.putInt("NewColonyHut", newColonyHut.getId());
edit.commit();
}//end if
newColonyHut.setTag("NewColonyHut");
newColonyHut.setOnTouchListener(new BuildingsClick());
findViewById(R.id.topHalf).setOnDragListener(new ColonyHutDrag(getApplicationContext()));
findViewById(R.id.bottomHalf).setOnDragListener(new ColonyHutDrag(getApplicationContext()));
FrameLayout.LayoutParams param = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT);
newColonyHutFrame.addView(newColonyHut, param);
这是logcat:
07-27 20:55:41.858: E/AndroidRuntime(16530): FATAL EXCEPTION: main
07-27 20:55:41.858: E/AndroidRuntime(16530): Process: com.twentytwentythree.sab, PID: 16530
07-27 20:55:41.858: E/AndroidRuntime(16530): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.twentytwentythree.sab/com.project.llb.runGraphics}: java.lang.NullPointerException: Attempt to invoke virtual method 'int android.widget.ImageButton.getId()' on a null object reference
07-27 20:55:41.858: E/AndroidRuntime(16530): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2596)
07-27 20:55:41.858: E/AndroidRuntime(16530): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2653)
07-27 20:55:41.858: E/AndroidRuntime(16530): at android.app.ActivityThread.access$800(ActivityThread.java:156)
07-27 20:55:41.858: E/AndroidRuntime(16530): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1355)
07-27 20:55:41.858: E/AndroidRuntime(16530): at android.os.Handler.dispatchMessage(Handler.java:102)
07-27 20:55:41.858: E/AndroidRuntime(16530): at android.os.Looper.loop(Looper.java:157)
07-27 20:55:41.858: E/AndroidRuntime(16530): at android.app.ActivityThread.main(ActivityThread.java:5872)
07-27 20:55:41.858: E/AndroidRuntime(16530): at java.lang.reflect.Method.invoke(Native Method)
07-27 20:55:41.858: E/AndroidRuntime(16530): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:852)
07-27 20:55:41.858: E/AndroidRuntime(16530): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:668)
07-27 20:55:41.858: E/AndroidRuntime(16530): Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'int android.widget.ImageButton.getId()' on a null object reference
07-27 20:55:41.858: E/AndroidRuntime(16530): at com.twentytwentythree.sab.runGraphics.onCreate(runGraphics.java:2131)
07-27 20:55:41.858: E/AndroidRuntime(16530): at android.app.Activity.performCreate(Activity.java:5312)
07-27 20:55:41.858: E/AndroidRuntime(16530): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1111)
07-27 20:55:41.858: E/AndroidRuntime(16530): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2552)
07-27 20:55:41.858: E/AndroidRuntime(16530): ... 9 more
如果我确定如果id不存在则不刷新按钮,我不知道为什么我会在这里得到nullpointerexception。
请让我知道你的想法。任何帮助表示赞赏。谢谢你们。
答案 0 :(得分:0)
在这种情况下你正在检查v!= 0,而我认为你正在检查v.getId,其中v被返回为null
newColonyHutFrame = (FrameLayout) findViewById(R.id.bottomHalf);
newColonyHut = new ImageButton(runGraphics.this);
newColonyHut.setBackgroundResource(R.drawable.mainhut);
prefs = getSharedPreferences("Buildings", MODE_PRIVATE);
// THIS IS WHAT YOU HAVE TO DO
if(v!=null){
if(v.getId()!= 0) { 编辑器编辑= prefs.edit(); edit.putInt(“NewColonyHut”,newColonyHut.getId()); edit.commit(); } //结束如果
}
newColonyHut.setTag("NewColonyHut");
newColonyHut.setOnTouchListener(new BuildingsClick());
findViewById(R.id.topHalf).setOnDragListener(new ColonyHutDrag(getApplicationContext()));
findViewById(R.id.bottomHalf).setOnDragListener(new ColonyHutDrag(getApplicationContext()));
FrameLayout.LayoutParams param = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT);
newColonyHutFrame.addView(newColonyHut, param);