tags:

views:

1512

answers:

5

I'm tinkering around with android on Netbeans and did a simple math application. Somewhere along the way the application suddenly force closes as soon as it opens(On the emulator)... I tried commenting all the code in the class that hosts the main activity but to no avail. Since it doesn't give me a specific error message, I feel kind of lost.

Any reason why this is happening?

Here is the layout file as requested:

<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <TextView  android:id="@+id/result"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text=""/>
        <LinearLayout android:orientation="horizontal"
     android:layout_width="fill_parent"
     android:layout_height="wrap_content">

     <TextView android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="@string/number" />
     <EditText android:id="@+id/n"
       android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_weight="1"
                        android:numeric="decimal"/>
                <Button android:id="@+id/calc"
                  android:text="@string/calculate"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content" />
    </LinearLayout>
</LinearLayout>

As suggested I checked logcat and this is the list of errors

E/AndroidRuntime(  881): Uncaught handler: thread main exiting due to uncaught exception
E/AndroidRuntime(  881): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{org.me.mathdroid/org.me.mathdroid.MainActivity}: java.lang.NullPointerException
E/AndroidRuntime(  881):        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2324)
E/AndroidRuntime(  881):        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2417)
E/AndroidRuntime(  881):        at android.app.ActivityThread.access$2100(ActivityThread.java:116)
E/AndroidRuntime(  881):        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1794)
E/AndroidRuntime(  881):        at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime(  881):        at android.os.Looper.loop(Looper.java:123)
E/AndroidRuntime(  881):        at android.app.ActivityThread.main(ActivityThread.java:4203)
E/AndroidRuntime(  881):        at java.lang.reflect.Method.invokeNative(NativeMethod)
E/AndroidRuntime(  881):        at java.lang.reflect.Method.invoke(Method.java:521)
E/AndroidRuntime(  881):        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
E/AndroidRuntime(  881):        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:549)
E/AndroidRuntime(  881):        at dalvik.system.NativeStart.main(Native Method)

E/AndroidRuntime(  881): Caused by: java.lang.NullPointerException
E/AndroidRuntime(  881):        at android.app.Activity.findViewById(Activity.java:1610)
E/AndroidRuntime(  881):        at org.me.mathdroid.MainActivity.<init>(MainActivity.java:22)
E/AndroidRuntime(  881):        at java.lang.Class.newInstanceImpl(Native Method)
E/AndroidRuntime(  881):        at java.lang.Class.newInstance(Class.java:1472)
E/AndroidRuntime(  881):        at android.app.Instrumentation.newActivity(Instrumentation.java:1097)
E/AndroidRuntime(  881):        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2316)
E/AndroidRuntime(  881):        ... 11 more
+1  A: 

Is this on a phone, or in the emulator? First place I'd look is in the logs. In Eclipse go to Window > Show View > Other... > Android > LogCat.

You can add your own debug logging messages in your code by

Log.d("Debug", "Informative Log Message.");

It would also be helpful if you could post your code here. Post the minimum working code example that exhibits the buggy behavior.

Bill the Lizard
Thing is I commented all the code of the main activity..so none of my code is being run..What I am assuming is there is something wrong with the strings or layout files
Drahcir
+1  A: 

Usually this happens to me when I mess up the layout file. Try opening the main layout file in Eclipse and see if it can still render it. If it can't, that could be the cause of your troubles. Post it here if you will, maybe we can help you out.

Gabri van Lee
I had the same problem.
fatcat1111
+1  A: 

try running
adb logcat
adb is found in the tools directory in the android sdk, it shows you exceptions during application load, things that can't be caught.

Fredrik Leijon
A: 

Have you created a strings.xml file in your res/values folder? You need to declare the strings that you are using:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="app_name">Test App</string>
    <string name="number">Number</string>
    <string name="calculate">Calculate</string>
</resources>

I would recommend using Eclipse rather than Netbeans. Then you can use the ADT plugin to create your layouts which should help you avoid problems.

AshtonBRSC
+2  A: 

I have figured out what the problem was, in the main activity I was setting an Edittext and TextView objects with the ones in the R class, before setting the contentView. Sorry for the trouble.

Drahcir