Android 启动时的NullPointerException

Android 启动时的NullPointerException,android,nullpointerexception,actionbarsherlock,runtime-error,Android,Nullpointerexception,Actionbarsherlock,Runtime Error,我得到了一个解决方案的一个较早的问题,我张贴在。 然而,出现了一个新问题。action.xml文件具有: <?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android" > <item android:id="@+id/location" android:actionLayout="@layout/a

我得到了一个解决方案的一个较早的问题,我张贴在。 然而,出现了一个新问题。action.xml文件具有:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >

<item 
     android:id="@+id/location"
     android:actionLayout="@layout/action_location"
     android:showAsAction="never"
     android:title="location">

</item>
<item 
    android:id="@+id/save" 
    android:title="Save" 
    android:showAsAction="always">

</item>
<item 
    android:id="@+id/saveBackground" 
    android:showAsAction="always|withText"
    android:title="in BG">

</item>
[更新2] FileDemoActivity.java的第29行

external=(CheckBox) menu.findItem(R.id.location).getActionView().findViewById(R.id.external);
如果这一行被注释掉,它运行正常。但是我想获得对复选框的引用

[更新3]action_location.xml的内容是:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<CheckBox 
    android:id="@+id/external"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:title="@+string/external"
    />


</LinearLayout>


您的应用程序无法使用这两种XML,因为这些XML中没有定义
R.id.external
。请确保这是两个文件之间的唯一区别。

由于您试图查找对子布局的引用,因此必须以正确的XML文件为目标。安卓系统本身无法做到这一点

编辑1,更新。从一个项目开始,看看它是否有效。现在是这样,见下文


结果是foo+bar+success被记录下来。所以它起作用了。我不得不用LayoutInflater对其进行调整,因为Android在运行时没有包含action_location.xml。

请从logcat发布整个stacktrace。@m0skit0:我已经包含stacktrace,请检查……尝试清理项目。这是您的代码中的错误。FileDemoActivity.java:29是哪一行?@m0skit0:我刚刚添加了[UPDATE 2],请检查。我不明白的是,如果这是代码中的错误,应用程序应该在启动时崩溃,而不管菜单项如何放置在actions.xml文件中。不是吗
R.id.external
是在android:actionLayout=“@layout/action\u location中定义的。我添加了一个[UPDATE 3],显示了xml文件的内容。我尝试过了,同样的问题仍然存在:(不,同样的问题仍然存在。我不确定这是否重要,但基类是SherlockFragmentActivity(以防万一它有什么不同)
external=(CheckBox) menu.findItem(R.id.location).getActionView().findViewById(R.id.external);
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<CheckBox 
    android:id="@+id/external"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:title="@+string/external"
    />


</LinearLayout>
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.action, menu);

    // inflate parent view      
    View parentView = null;
    Log.d(null,"159 - foo --");
    // then inflate the xml, that it exists on android
    try {
        LayoutInflater inflater = LayoutInflater.from(this.getBaseContext());
        parentView = inflater.inflate(R.layout.action_location, null);
    }
    catch (Exception e) {
        Log.d(null, "159 - error after inflater : " + e.getMessage());
    }
    Log.d(null,"159 - bar --");
    View childView = parentView.findViewById(R.id.external);
    Log.d(null,"159 - success --");
    return true;
}