我正在开发我的第一个 android 应用程序,我无法使用活动的 onCreate 函数创建我在 java 中编程的类的对象。当我不实例化对象时,活动开始很好,但是当我尝试创建对象时,应用程序在切换到活动时崩溃。onCreate 函数看起来像这样......
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_entry);
// Show the Up button in the action bar.
setupActionBar();
ForceTable testTable = new ForceTable();
Double factor = testTable.returnValue(forceTypes.newtons, forceTypes.newtons);
}
ForceTable 是我编写的类,它的代码看起来是这样的......
public class ForceTable {
private double[][] forceTable;
protected enum forceTypes {newtons(0), pounds(1), kilopond(2);
public int num;
private forceTypes(int num)
{
this.num = num;
}
};
protected final class values{
private final static double zeroZero = 1.00;
private final static double zeroOne = 4.44872162;
private final static double zeroTwo = 9.80665;
private final static double oneZero = .224808943;
private final static double oneOne = 1.00;
private final static double oneTwo = 2.20462262;
private final static double twoZero = .10197164;
private final static double twoOne = .45359237;
private final static double twoTwo = 1.00;
}
public ForceTable()
{
this.makeTable();
}
private void makeTable()
{
forceTable[0][0] = values.zeroZero;
forceTable[0][1] = values.zeroOne;
forceTable[0][2] = values.zeroTwo;
forceTable[1][0] = values.oneZero;
forceTable[1][1] = values.oneOne;
forceTable[1][2] = values.oneTwo;
forceTable[2][0] = values.twoZero;
forceTable[2][1] = values.twoOne;
forceTable[2][2] = values.twoTwo;
}
public double returnValue(forceTypes ifYouHave, forceTypes thenYouHave){
double factor = forceTable[thenYouHave.num][ifYouHave.num];
return factor;
}
}
自从我用 Java 编程以来已经很长时间了,而且由于活动在没有实例化的情况下可以正常启动,所以它一定是我的 ForceTable 的 Java 代码。有人注意到有问题吗?很有可能这是一件容易的事,我没有被刷过......
这是日志猫
08-08 18:08:13.206: E/(9801): : 无法打开文件进行读取 08-08 18:08:13.206: E/(9801): : 无法打开文件进行读取 08-08 18 :10:34.045:W / dalvikvm(9801):threadid = 1:线程退出未捕获的异常(组= 0x4136b438)08-08 18:10:34.045:E / AndroidRuntime(9801):致命异常:主要08-08 18 :10:34.045: E/AndroidRuntime(9801): java.lang.RuntimeException: 无法启动活动 ComponentInfo{khandy.application.convertible/khandy.application.convertible.EntryActivity}: java.lang.NullPointerException 08-08 18:10 :34.045: E/AndroidRuntime(9801): 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2110) 08-08 18:10:34.045: E/AndroidRuntime(9801): 在 android.app.ActivityThread.handleLaunchActivity( ActivityThread.java:2135) 08-08 18:10:34.045: E/AndroidRuntime(9801): 在 android。app.ActivityThread.access$700(ActivityThread.java:143) 08-08 18:10:34.045: E/AndroidRuntime(9801): 在 android.app.ActivityThread$H.handleMessage(ActivityThread.java:1241) 08-08 18 :10:34.045: E/AndroidRuntime(9801): 在 android.os.Handler.dispatchMessage(Handler.java:99) 08-08 18:10:34.045: E/AndroidRuntime(9801): 在 android.os.Looper。循环(Looper.java:137)08-08 18:10:34.045:E/AndroidRuntime(9801):在 android.app.ActivityThread.main(ActivityThread.java:4950)08-08 18:10:34.045:E/ AndroidRuntime(9801): at java.lang.reflect.Method.invokeNative(Native Method) 08-08 18:10:34.045: E/AndroidRuntime(9801): at java.lang.reflect.Method.invoke(Method.java: 511) 08-08 18:10:34.045: E/AndroidRuntime(9801): 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1004) 08-08 18:10:34.045: E/AndroidRuntime(9801): 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:771) 08-08 18:10:34.045: E/AndroidRuntime(9801): 在 dalvik.system.NativeStart .main(本机方法)08-08 18:10:34.045:E/AndroidRuntime(9801):由:java.lang.NullPointerException 08-08 18:10:34.045:E/AndroidRuntime(9801):在 khandy.application .convertible.ForceTable.makeTable(ForceTable.java:34) 08-08 18:10:34.045: E/AndroidRuntime(9801): at khandy.application.convertible.ForceTable.(ForceTable.java:29) 08-08 18: 10:34.045: E/AndroidRuntime(9801): 在 khandy.application.convertible.EntryActivity.onCreate(EntryActivity.java:21) 08-08 18:10:34.045: E/AndroidRuntime(9801): 在 android.app.Activity .performCreate(Activity.java:5179) 08-08 18:10:34.045: E/AndroidRuntime(9801): 在 android.app。Instrumentation.callActivityOnCreate(Instrumentation.java:1094) 08-08 18:10:34.045: E/AndroidRuntime(9801): 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2074) 08-08 18:10:34.045: E/AndroidRuntime(9801): ... 11 更多