Android应用程序在创建自定义类对象时崩溃

时间:2013-08-09 01:02:22

标签: java android android-activity oncreate

我正在开发我的第一个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代码。有人注意到了什么问题吗?很有可能它很简单,我没有被刷过......

这是logcat

  
    

08-08 18:08:13.206:E /(9801)::无法打开文件进行阅读     08-08 18:08:13.206:E /(9801)::无法打开文件进行阅读     08-08 18:10:3​​4.045:W / dalvikvm(9801):threadid = 1:线程退出,未捕获异常(组= 0x4136b438)     08-08 18:10:3​​4.045:E / AndroidRuntime(9801):致命异常:主要     08-08 18:10:3​​4.045:E / AndroidRuntime(9801):java.lang.RuntimeException:无法启动活动ComponentInfo {khandy.application.convertible / khandy.application.convertible.EntryActivity}:java.lang.NullPointerException     08-08 18:10:3​​4.045:E / AndroidRuntime(9801):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2110)     08-08 18:10:3​​4.045:E / AndroidRuntime(9801):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2135)     08-08 18:10:3​​4.045:E / AndroidRuntime(9801):在android.app.ActivityThread.access $ 700(ActivityThread.java:143)     08-08 18:10:3​​4.045:E / AndroidRuntime(9801):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1241)     08-08 18:10:3​​4.045:E / AndroidRuntime(9801):在android.os.Handler.dispatchMessage(Handler.java:99)     08-08 18:10:3​​4.045:E / AndroidRuntime(9801):在android.os.Looper.loop(Looper.java:137)     08-08 18:10:3​​4.045:E / AndroidRuntime(9801):在android.app.ActivityThread.main(ActivityThread.java:4950)     08-08 18:10:3​​4.045:E / AndroidRuntime(9801):at java.lang.reflect.Method.invokeNative(Native Method)     08-08 18:10:3​​4.045:E / AndroidRuntime(9801):at java.lang.reflect.Method.invoke(Method.java:511)     08-08 18:10:3​​4.045:E / AndroidRuntime(9801):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1004)     08-08 18:10:3​​4.045:E / AndroidRuntime(9801):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:771)     08-08 18:10:3​​4.045:E / AndroidRuntime(9801):at dalvik.system.NativeStart.main(Native Method)     08-08 18:10:3​​4.045:E / AndroidRuntime(9801):引起:java.lang.NullPointerException     08-08 18:10:3​​4.045:E / AndroidRuntime(9801):at khandy.application.convertible.ForceTable.makeTable(ForceTable.java:34)     08-08 18:10:3​​4.045:E / AndroidRuntime(9801):at khandy.application.convertible.ForceTable。(ForceTable.java:29)     08-08 18:10:3​​4.045:E / AndroidRuntime(9801):at khandy.application.convertible.EntryActivity.onCreate(EntryActivity.java:21)     08-08 18:10:3​​4.045:E / AndroidRuntime(9801):在android.app.Activity.performCreate(Activity.java:5179)     08-08 18:10:3​​4.045:E / AndroidRuntime(9801):在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)     08-08 18:10:3​​4.045:E / AndroidRuntime(9801):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2074)     08-08 18:10:3​​4.045:E / AndroidRuntime(9801):... 11更多

  

3 个答案:

答案 0 :(得分:1)

您可能会在此行获得NullPointerException

forceTable[0][0] = values.zeroZero;

因为您尝试访问数组而不先创建它。在访问它之前,按照

创建它
forceTable = new double[][];
forceTable[0][0] = values.zeroZero;

并且请始终发布logcat输出!它只是让它更容易找到问题。

答案 1 :(得分:0)

我认为问题出在你的makeTable函数被调用时。你可以为你的forceTable数组分配空间吗。我的意思是为 double [] [] forceTable ;

做一个新的

答案 2 :(得分:0)

你错过了

privatetable = new double[n] [m] ;

你可以把它放在第二堂课的构造函数中!