I'm coming from the C++ world and I think i'm missing something when it comes to managed code and android development. It makes sense to separate out all the UI stuff like strings into xml files. But I would expect errors to be caught at build time. In particular, NullPointerExecption errors can often occur due to bad or missing .xml components. It seems like JVM pushes catching problems to run-time which could be caught at build time, and this seems like a very bad thing.
The current error i'm getting has a NullPointerException but gives no line number. So it could come from a typo from any of my xml resources. This is very inefficient for debugging. To make things worse, I'm having difficulty in stepping through code to debug. Some files work fine, while others do not match the line numbers correctly and others can't be loaded by the debugger (PathClassLoader, BootClassLoader).
Surely there must be a better way to approach this? How can i get the line number that caused the NullPointerException?
Here is my code:
public class FragmentsMainActivity extends FragmentActivity {
public final static int STARTUP_ACTIVITY_RESULT=0;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fragments_main);
Intent intentStartupActivity = new Intent(this, StartupActivity.class);
if(intentStartupActivity != null)
startActivityForResult(intentStartupActivity, STARTUP_ACTIVITY_RESULT);
// get an instance of FragmentTransaction from your Activity
FragmentTransaction fragmentTransaction =
getSupportFragmentManager().beginTransaction();
//add a fragment
StreamingActivity streamingActivity = new StreamingActivity();
if (streamingActivity != null) {
fragmentTransaction.add(R.id.streamingactivity, streamingActivity);
fragmentTransaction.commit();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_fragments_main, menu);
return true;
}
And here is my AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="kesten.fragmentstestbed"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="9"
android:targetSdkVersion="15" />
<!-- Sphero permissions -->
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".FragmentsMainActivity"
android:label="@string/title_activity_fragments_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!-- StartupActivity for connecting to a Robot -->
<activity
android:name="orbotix.robot.app.StartupActivity">
</activity>
</application>
</manifest>
here is the LogCat
07-18 10:37:25.600: W/dalvikvm(4835): threadid=1: thread exiting with uncaught exception (group=0x40020560)
07-18 10:37:25.680: E/AndroidRuntime(4835): FATAL EXCEPTION: main
07-18 10:37:25.680: E/AndroidRuntime(4835): java.lang.RuntimeException: Unable to start activity ComponentInfo{kesten.fragmentstestbed/kesten.fragmentstestbed.FragmentsMainActivity}:java.lang.NullPointerException
07-18 10:37:25.680: E/AndroidRuntime(4835): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1650)
07-18 10:37:25.680: E/AndroidRuntime(4835): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1666)
and the stack trace
Thread [<1> main] (Suspended (exception RuntimeException))
ActivityThread.performLaunchActivity(ActivityThread$ActivityClientRecord, Intent) line: 1650
ActivityThread.handleLaunchActivity(ActivityThread$ActivityClientRecord, Intent) line: 1666
ActivityThread.access$1500(ActivityThread, ActivityThread$ActivityClientRecord, Intent) line: 117
Thanks to the answers, i found the "caused by" down the logCat trace where the line number is. This showed me that my fragment - StreamingActivity.onCreate() was calling getView() and this was returning null. This solved my NPE, so I will start a new thread to discuss the problems I'm having with the order of creating views in Activity and Fragment in another thread and mark this as solved.