当我想参加活动时,会发生错误。所以这是LogCat
01-16 12:22:17.014: E/AndroidRuntime(832): FATAL EXCEPTION: main
01-16 12:22:17.014: E/AndroidRuntime(832): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.blackcat.triporganizer/com.blackcat.triporganizer.tracker.OthersBudgetActivity}: java.lang.NullPointerException
01-16 12:22:17.014: E/AndroidRuntime(832): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
01-16 12:22:17.014: E/AndroidRuntime(832): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
01-16 12:22:17.014: E/AndroidRuntime(832): at android.app.ActivityThread.access$600(ActivityThread.java:141)
01-16 12:22:17.014: E/AndroidRuntime(832): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
01-16 12:22:17.014: E/AndroidRuntime(832): at android.os.Handler.dispatchMessage(Handler.java:99)
01-16 12:22:17.014: E/AndroidRuntime(832): at android.os.Looper.loop(Looper.java:137)
01-16 12:22:17.014: E/AndroidRuntime(832): at android.app.ActivityThread.main(ActivityThread.java:5103)
01-16 12:22:17.014: E/AndroidRuntime(832): at java.lang.reflect.Method.invokeNative(Native Method)
01-16 12:22:17.014: E/AndroidRuntime(832): at java.lang.reflect.Method.invoke(Method.java:525)
01-16 12:22:17.014: E/AndroidRuntime(832): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
01-16 12:22:17.014: E/AndroidRuntime(832): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
01-16 12:22:17.014: E/AndroidRuntime(832): at dalvik.system.NativeStart.main(Native Method)
01-16 12:22:17.014: E/AndroidRuntime(832): Caused by: java.lang.NullPointerException
01-16 12:22:17.014: E/AndroidRuntime(832): at com.blackcat.triporganizer.tracker.OthersBudgetActivity.onCreate(OthersBudgetActivity.java:27)
01-16 12:22:17.014: E/AndroidRuntime(832): at android.app.Activity.performCreate(Activity.java:5133)
01-16 12:22:17.014: E/AndroidRuntime(832): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
01-16 12:22:17.014: E/AndroidRuntime(832): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
01-16 12:22:17.014: E/AndroidRuntime(832): ... 11 more
现在这是我要去的活动:
public class OthersBudgetActivity extends Activity {
EditText BudgetInput;
ImageButton DoneButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_others_budget);
BudgetInput = (EditText) findViewById(R.id.editText_EntertainmentBudget);
BudgetInput.setInputType(InputType.TYPE_CLASS_NUMBER);
DoneButton = (ImageButton) findViewById(R.id.imageButton_EntertainmentBudget);
DoneButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
boolean didItWork = true;
TrackerDAO gettingInfo = new TrackerDAO(OthersBudgetActivity.this);
gettingInfo.open();
long getlastID = gettingInfo.getMaxID();
int prev_other = gettingInfo.getOtherPrev(getlastID);
String getAmountString = BudgetInput.getText().toString();
int getAmountInt = Integer.parseInt(getAmountString);
prev_other = prev_other + getAmountInt;
int prev_remainbudget = gettingInfo.getPrevRemainBudget(getlastID);
prev_remainbudget = prev_remainbudget - getAmountInt;
try{
gettingInfo.updatePrevRemainBudget(getlastID, prev_remainbudget);
gettingInfo.updateotherEntry(getlastID, prev_other);
gettingInfo.close();
}catch(Exception e){
didItWork = false;
Dialog d = new Dialog(OthersBudgetActivity.this);
d.setTitle("Not Updated");
d.show();
} finally {
if (didItWork) {
Dialog d = new Dialog(OthersBudgetActivity.this);
d.setTitle("Data Updated");
d.show();
}
Intent OpenActivity = new Intent (OthersBudgetActivity.this, BudgetTrackerActivity.class);
startActivity(OpenActivity);
}
}
});
}
@Override
protected void onPause() {
super.onPause();
finish();
}
}
谢谢。
答案 0 :(得分:0)
在onCreate()中实际执行的代码不多,所以我猜你的Buttons
之一为null,当访问其中一个方法或成员时会导致此异常。
确保R.id.editText_EntertainmentBudget
和R.id.imageButton_EntertainmentBudget
成为您的布局R.layout.activity_others_budget
的一部分。
答案 1 :(得分:0)
我在使用活动的NullExceptionPointers时遇到了无法找到资源的经验,即使它处于Intellisense类型中。 最好的办法是在调试中通过AVD运行它,你可以看到哪个对象是带有断点的NULL。有时需要花费一些时间,但它会引导您回答。 我愿意打赌它是你的EditText或Button导致问题。