简体   繁体   English

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

[英]Android Application Crashes when Creating custom class object

I'm working on my first android app and I'm having trouble creating an object of a class I have programmed in java using the onCreate function of an activity. 我正在开发我的第一个android应用程序,但是在使用活动的onCreate函数创建用Java编程的类的对象时遇到了麻烦。 The activity starts great when I do not instantiate the object, but when I attempt to create the object the app crashes when switching to the activity. 当我不实例化对象时,该活动就很好了,但是当我尝试创建对象时,切换到该活动时,应用程序崩溃了。 The onCreate function looks like this... 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 is the class I have programmed, its code looks this... 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;
}

} }

It's been a very long time since I have programmed in Java, and since the activity starts fine without instantiating it must be my Java code for ForceTable. 自从我用Java编程以来已经有很长时间了,并且由于该活动在没有实例化的情况下开始正常,所以它必须是我的ForceTable Java代码。 Anybody notice something wrong? 有人注意到有问题吗? There's a good chance it's something easy that I'm not brushed up on... 我很容易就发现这很容易...

Here is the logcat 这是logcat

08-08 18:08:13.206: E/(9801): : Can't open file for reading 08-08 18:08:13.206: E/(9801): : Can't open file for reading 08-08 18:10:34.045: W/dalvikvm(9801): threadid=1: thread exiting with uncaught exception (group=0x4136b438) 08-08 18:10:34.045: E/AndroidRuntime(9801): FATAL EXCEPTION: main 08-08 18:10:34.045: E/AndroidRuntime(9801): java.lang.RuntimeException: Unable to start activity ComponentInfo{khandy.application.convertible/khandy.application.convertible.EntryActivity}: java.lang.NullPointerException 08-08 18:10:34.045: E/AndroidRuntime(9801): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2110) 08-08 18:10:34.045: E/AndroidRuntime(9801): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2135) 08-08 18:10:34.045: E/AndroidRuntime(9801): at android.app.ActivityThread.access$700(ActivityThread.java:143) 08-08 18:10:34.045: E/AndroidRuntime(9801): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1241) 08-08 18:10:34.04 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:线程退出且未捕获到异常(group = 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 :34.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.04 5: E/AndroidRuntime(9801): at android.os.Handler.dispatchMessage(Handler.java:99) 08-08 18:10:34.045: E/AndroidRuntime(9801): at android.os.Looper.loop(Looper.java:137) 08-08 18:10:34.045: E/AndroidRuntime(9801): at 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): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1004) 08-08 18:10:34.045: E/AndroidRuntime(9801): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:771) 08-08 18:10:34.045: E/AndroidRuntime(9801): at dalvik.system.NativeStart.main(Native Method) 08-08 18:10:34.045: E/AndroidRuntime(9801): Caused by: java.lang.NullPointerException 08-08 18:10:34.045: E/AndroidRuntime(9801): at khandy.application.convertible.ForceTable.makeTable(ForceTable.java: 5: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(本机方法)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) ):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:771)08-08 18:10:3​​4.045:E / AndroidRuntime(9801):在dalvik.system.NativeStart.main(本机方法)08 -08 18:10:3​​4.045:E / AndroidRuntime(9801):由以下原因引起:java.lang.NullPointerException 08-08 18:10:3​​4.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): at khandy.application.convertible.EntryActivity.onCreate(EntryActivity.java:21) 08-08 18:10:34.045: E/AndroidRuntime(9801): at android.app.Activity.performCreate(Activity.java:5179) 08-08 18:10:34.045: E/AndroidRuntime(9801): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094) 08-08 18:10:34.045: E/AndroidRuntime(9801): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2074) 08-08 18:10:34.045: E/AndroidRuntime(9801): ... 11 more 34)08-08 18:10:3​​4.045:E / AndroidRuntime(9801):位于khandy.application.convertible.ForceTable。(ForceTable.java:29)08-08 18:10:3​​4.045:E / AndroidRuntime(9801):在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个

You probably get a NullPointerException at this line 您可能会在此行收到NullPointerException

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

because you try to access the array without creating it first. 因为您尝试访问数组而不先创建它。 Before accessing it, create it like that 在访问它之前,像这样创建它

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

And please ALWAYS post the logcat output! 并且请始终发布logcat输出! It just makes it so much easier to find the problem. 这使得查找问题变得非常容易。

I think the problem is when your makeTable function is called. 我认为问题是当您的makeTable函数被调用时。 can you allocate space for your forceTable array.. I mean do a new for double[][] forceTable ; 您可以为forceTable数组分配空间吗。我的意思是为double [] [] forceTable做一个新的;

You are missing 你不见了

privatetable = new double[n] [m] ;

You can put it in the constructor of the second class! 您可以将其放在第二个类的构造函数中!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM