我来自C ++世界,我认为在托管代码和Android开发方面我缺少一些东西。将所有UI内容(如字符串)分离到xml文件中是有意义的。但我希望在构建时能够捕获错误。特别是,NullPointerExecption错误通常可能由于错误或缺少.xml组件而发生。似乎JVM将捕获问题推向可能在构建时捕获的运行时,这看起来非常糟糕。
我得到的当前错误有一个NullPointerException但没有给出行号。所以它可能来自我的任何xml资源的拼写错误。这对于调试来说非常低效。更糟糕的是,我很难通过代码来调试。有些文件工作正常,而有些文件没有正确匹配行号,而其他文件无法由调试器加载(PathClassLoader,BootClassLoader)。
当然必须有更好的方法来解决这个问题吗?如何获取导致NullPointerException的行号?
这是我的代码:
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;
}
这是我的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>
这是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)
和堆栈跟踪
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
感谢答案,我在logCat跟踪中找到了“由”引起的行号。 这向我展示了我的片段 - StreamingActivity.onCreate()正在调用getView()并返回null。这解决了我的NPE,所以我将开始一个新线程来讨论我在另一个线程中在Activity和Fragment中创建视图的顺序所遇到的问题并将其标记为已解决。
答案 0 :(得分:2)
如何获取导致NullPointerException的行号?
信息在那里,它只是有点神秘。从NullPointerException/Help Reading LogCat:
考虑这个LogCat06-11 11:56:08.118: W/dalvikvm(593): threadid=1: thread exiting with uncaught exception (group=0x409c01f8)
06-11 11:56:08.128: E/AndroidRuntime(593): FATAL EXCEPTION: main
06-11 11:56:08.128: E/AndroidRuntime(593): java.lang.RuntimeException: Unable to start activity ComponentInfo{gaurav.android/gaurav.android.CalcActivity}: java.lang.NullPointerException
06-11 11:56:08.128: E/AndroidRuntime(593): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
06-11 11:56:08.128: E/AndroidRuntime(593): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
06-11 11:56:08.128: E/AndroidRuntime(593): at android.app.ActivityThread.access$600(ActivityThread.java:123)
06-11 11:56:08.128: E/AndroidRuntime(593): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
06-11 11:56:08.128: E/AndroidRuntime(593): at android.os.Handler.dispatchMessage(Handler.java:99)
06-11 11:56:08.128: E/AndroidRuntime(593): at android.os.Looper.loop(Looper.java:137)
06-11 11:56:08.128: E/AndroidRuntime(593): at android.app.ActivityThread.main(ActivityThread.java:4424)
06-11 11:56:08.128: E/AndroidRuntime(593): at java.lang.reflect.Method.invokeNative(Native Method)
06-11 11:56:08.128: E/AndroidRuntime(593): at java.lang.reflect.Method.invoke(Method.java:511)
06-11 11:56:08.128: E/AndroidRuntime(593): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
06-11 11:56:08.128: E/AndroidRuntime(593): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
06-11 11:56:08.128: E/AndroidRuntime(593): at dalvik.system.NativeStart.main(Native Method)
06-11 11:56:08.128: E/AndroidRuntime(593): Caused by: java.lang.NullPointerException
06-11 11:56:08.128: E/AndroidRuntime(593): at gaurav.android.CalcActivity.reset(CalcActivity.java:341)
06-11 11:56:08.128: E/AndroidRuntime(593): at gaurav.android.CalcActivity.onCreate(CalcActivity.java:58)
06-11 11:56:08.128: E/AndroidRuntime(593): at android.app.Activity.performCreate(Activity.java:4465)
06-11 11:56:08.128: E/AndroidRuntime(593): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
06-11 11:56:08.128: E/AndroidRuntime(593): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
你需要在你的LogCat中找到“引起:”最远的地方,在这种情况下:
06-11 11:56:08.128: E/AndroidRuntime(593): Caused by: java.lang.NullPointerException
06-11 11:56:08.128: E/AndroidRuntime(593): at gaurav.android.CalcActivity.reset(CalcActivity.java:341)
在这里我们可以看到CalcActivity在reset()中抛出NullPointerException,特别是(CalcActivity.java:341)
告诉我们它在第341行。你可以进一步关注堆栈跟踪,如果它有帮助,但这是如何阅读NPE的Android LogCat。
希望有所帮助!否则请发布完整的LogCat。
答案 1 :(得分:0)
如果与XML有关,Eclipse将在编译时标记错误。否则,它不会让您构建您的应用程序。 NullPointerExceptions
只能在运行时找到,因为它完全取决于变量是否被赋值。当然,有一些简单的例子,例如将变量设置为null
,然后再也不会重新分配,但编译器很难检测到所有逻辑。您不需要检查您创建的每个对象是否与代码中的null
一样。
至于解决您的NullPointerException
问题,您需要发布给定的完整堆栈跟踪。它通常会在Caused by
行中显示错误发生的行号。