Android - 使用ViewGroup的NullPointerException

时间:2014-04-15 19:08:27

标签: java android nullpointerexception viewgroup

当我使用简单的TextView从一个Activity重定位到另一个Activity时 - 它可以工作。 一段代码:

 private RMProject currentProject;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.open_proj);
    Bundle bundle = getIntent().getExtras();
    currentProject = bundle.getParcelable("current");
    TextView textView = (TextView) findViewById(R.id.label_open_project);
    textView.setText("Project "+currentProject.getName());

}

但是当我在 onCreate()方法中使用下一个代码的另一个活动时 - 它不起作用,我收到NullPointerException。在第38行。我查了一下,在使用第一段代码时我收到了textView。

public class EventsActivity extends Activity {
private RMProject currentProject;
private ListView listView;
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    EventDBManager.init(this);
    ViewGroup contentView = (ViewGroup) getLayoutInflater().inflate(R.layout.orm_event_list, null);
    listView = (ListView) contentView.findViewById(R.id.ormListView);
    Bundle bundle = getIntent().getExtras();
    currentProject = bundle.getParcelable("current");
    TextView textView = (TextView) findViewById(R.id.ormTextView);
    //38 line - next, which cause exception
    textView.setText("Project "+currentProject.getName());
    Button btn = (Button) contentView.findViewById(R.id.ormButtonAdd);
    setupButton(btn);
    setContentView(contentView);
}
}

我猜这个问题与组件布局有关。 这是我的布局文件:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="match_parent"
            android:layout_height="match_parent">
<TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:editable="true"
        android:text="Project name"
        android:id="@+id/ormTextView"
        android:layout_alignParentTop="true"
        android:layout_alignParentStart="true"
        android:layout_marginLeft="90dp"
        android:textSize="20dp"/>
<Button
        android:id="@+id/ormButtonAdd"
        android:layout_alignParentBottom="true"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Add " />
<ListView
        android:id="@+id/ormListView"
        android:layout_above="@id/ormButtonAdd"

        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />
</RelativeLayout>

我收到的例外:

  

04-15 18:26:29.209 1401-1401 / nikolay.rm E / AndroidRuntime:FATAL EXCEPTION:main       过程:nikolay.rm,PID:1401       java.lang.RuntimeException:无法启动活动ComponentInfo {nikolay.rm / nikolay.rm.activities.EventsActivity}:   显示java.lang.NullPointerException               在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)               在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)               在android.app.ActivityThread.access $ 800(ActivityThread.java:135)               在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1196)               在android.os.Handler.dispatchMessage(Handler.java:102)               在android.os.Looper.loop(Looper.java:136)               在android.app.ActivityThread.main(ActivityThread.java:5017)               at java.lang.reflect.Method.invokeNative(Native Method)               在java.lang.reflect.Method.invoke(Method.java:515)               在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:779)               在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)               at dalvik.system.NativeStart.main(Native Method)        引起:java.lang.NullPointerException               at nikolay.rm.activities.EventsActivity.onCreate(EventsActivity.java:38)               在android.app.Activity.performCreate(Activity.java:5231)               在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)               在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)   在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)   在android.app.ActivityThread.access $ 800(ActivityThread.java:135)   在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1196)   在android.os.Handler.dispatchMessage(Handler.java:102)   在android.os.Looper.loop(Looper.java:136)   在android.app.ActivityThread.main(ActivityThread.java:5017)   at java.lang.reflect.Method.invokeNative(Native Method)   在java.lang.reflect.Method.invoke(Method.java:515)

1 个答案:

答案 0 :(得分:1)

您在以下行中缺少的contentView参考

TextView textView = (TextView) findViewById(R.id.ormTextView);

所以修复就是用

替换它
TextView textView = (TextView) contentView .findViewById(R.id.ormTextView);

希望这有帮助