NullPointerException thrown in Eclipse when trying to open Android XML view?

放肆的年华 提交于 2019-12-10 14:42:15

问题


Here is the exception:

java.lang.NullPointerException
at android.widget.TextView.setTextColor(TextView.java:1787)
at android.widget.TabHost$LabelIndicatorStrategy.createIndicatorView(TabHost.java:521)
at android.widget.TabHost.addTab(TabHost.java:204)
at com.android.layoutlib.bridge.Bridge.setupTabHost(Bridge.java:880)
at com.android.layoutlib.bridge.Bridge.postInflateProcess(Bridge.java:807)
at com.android.layoutlib.bridge.Bridge.postInflateProcess(Bridge.java:813)
at com.android.layoutlib.bridge.Bridge.computeLayout(Bridge.java:401)
at com.android.ide.eclipse.adt.internal.editors.layout.gle2.GraphicalEditorPart.computeLayout(Unknown Source)
at com.android.ide.eclipse.adt.internal.editors.layout.gle2.GraphicalEditorPart.renderWithBridge(Unknown Source)
at com.android.ide.eclipse.adt.internal.editors.layout.gle2.GraphicalEditorPart.recomputeLayout(Unknown Source)
at com.android.ide.eclipse.adt.internal.editors.layout.gle2.GraphicalEditorPart.activated(Unknown Source)
at com.android.ide.eclipse.adt.internal.editors.layout.LayoutEditor.pageChange(Unknown Source)
at org.eclipse.ui.part.MultiPageEditorPart$2.widgetSelected(MultiPageEditorPart.java:290)
at org.eclipse.swt.widgets.TypedListener.handleEvent(TypedListener.java:234)
at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java:84)
at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1258)
at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1282)
at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1267)
at org.eclipse.swt.widgets.Widget.notifyListeners(Widget.java:1061)
at org.eclipse.swt.custom.CTabFolder.setSelection(CTabFolder.java:2743)
at org.eclipse.swt.custom.CTabFolder.onMouse(CTabFolder.java:1429)
at org.eclipse.swt.custom.CTabFolder$1.handleEvent(CTabFolder.java:257)
at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java:84)
at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1258)
at org.eclipse.swt.widgets.Display.runDeferredEvents(Display.java:3540)
at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:3161)
at org.eclipse.ui.internal.Workbench.runEventLoop(Workbench.java:2640)
at org.eclipse.ui.internal.Workbench.runUI(Workbench.java:2604)
at org.eclipse.ui.internal.Workbench.access$4(Workbench.java:2438)
at org.eclipse.ui.internal.Workbench$7.run(Workbench.java:671)
at org.eclipse.core.databinding.observable.Realm.runWithDefault(Realm.java:332)
at org.eclipse.ui.internal.Workbench.createAndRunWorkbench(Workbench.java:664)
at org.eclipse.ui.PlatformUI.createAndRunWorkbench(PlatformUI.java:149)
at org.eclipse.ui.internal.ide.application.IDEApplication.start(IDEApplication.java:115)
at org.eclipse.equinox.internal.app.EclipseAppHandle.run(EclipseAppHandle.java:196)
at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.runApplication(EclipseAppLauncher.java:110)
at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.start(EclipseAppLauncher.java:79)
at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:369)
at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:179)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:616)
at org.eclipse.equinox.launcher.Main.invokeFramework(Main.java:619)
at org.eclipse.equinox.launcher.Main.basicRun(Main.java:574)
at org.eclipse.equinox.launcher.Main.run(Main.java:1407)

Here is the main.xml file it's associated with:

<?xml version="1.0" encoding="utf-8"?>
<TabHost
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:id="@android:id/tabhost">

  <LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:padding="5dp">
  <TabWidget 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:id="@android:id/tabs" />
    <FrameLayout
        android:id="@android:id/tabcontent"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">

        <include layout="@layout/basic_tab" />
        <include layout="@layout/advanced_tab" />

    </FrameLayout>
  </LinearLayout>
</TabHost>

Both the include files work individually just fine. But when in a tab layout, this error occurs. Any fixes?
Here is the code:

import android.app.TabActivity;
import android.content.res.Resources;
import android.os.Bundle;
import android.widget.TabHost;

import com.mohit.geo2do.R;

public class TaskEdit extends TabActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.edit_task);

        Resources res = getResources();
        TabHost host = getTabHost();

        host.addTab(host.newTabSpec("basic")
            .setIndicator("Basic", res.getDrawable(android.R.drawable.ic_menu_edit))
            .setContent(R.layout.basic_tab));
        host.addTab(host.newTabSpec("advanced")
            .setIndicator("Advanced", res.getDrawable(android.R.drawable.ic_menu_manage))
            .setContent(R.layout.advanced_tab));

        host.setCurrentTab(0);
    }
}

And here are the xml files, advanced_tab:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:orientation="vertical"
  android:id="@+id/advanced_tab_layout">

  <TextView 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true" 
    android:text="Notify me:"
    android:textColor="#FFFFFF"
    android:textStyle="bold" 
  />

  <CheckBox 
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:layout_alignParentTop="true"
       android:id="@+id/task_due" 
       android:text="when task is due"
       android:textColor="#FFFFFF"
       android:textStyle="bold" 
       android:paddingTop="5dip"
   />

   <CheckBox 
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:layout_alignParentTop="true"
       android:id="@+id/task_overdue" 
       android:text="when task is overdue"
       android:textColor="#FFFFFF"
       android:textStyle="bold" 
       android:paddingTop="5dip"
   /> 

   <CheckBox 
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:layout_alignParentTop="true"
       android:id="@+id/task_datetime" 
       android:text="at"
       android:textColor="#FFFFFF"
       android:textStyle="bold" 
       android:paddingTop="5dip"
   />

   <TextView 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true" 
    android:text="Calendar:"
    android:textColor="#FFFFFF"
    android:textStyle="bold" 
    android:paddingTop="5dip"
    />

   <CheckBox 
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:layout_alignParentTop="true"
       android:id="@+id/task_calendar" 
       android:text="Create a calendar event"
       android:textColor="#FFFFFF"
       android:textStyle="bold" 
       android:paddingTop="5dip"
   />

</LinearLayout>

And basic_tab.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" 
android:layout_height="fill_parent"
android:orientation="vertical"
android:id="@+id/basic_tab_layout">
<TextView 
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:layout_alignParentTop="true"
   android:text="Title:"
   android:textColor="#FFFFFF"
   android:textStyle="bold" 
   android:paddingTop="5dip"
/>

<EditText
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:id="@+id/title"
   android:hint="Name of the task..."
/>  

<TextView
   android:layout_height="wrap_content"
   android:layout_width="wrap_content"
   android:text="Importance:"
   android:textColor="#FFFFFF"
   android:textStyle="bold"
   android:paddingTop="10dip"
/>

<RadioGroup 
    android:layout_height="wrap_content"
    android:layout_width="fill_parent"
    android:orientation="horizontal"
    android:id="@+id/importance_grp">

    <RadioButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="High" 
        android:paddingRight="25dip"
    />
    <RadioButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Medium" 
        android:checked="true"
        android:paddingRight="25dip"
    />
    <RadioButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Low" 
    />

</RadioGroup>

<TextView
   android:layout_height="wrap_content"
   android:layout_width="wrap_content"
   android:text="Due Date:"
   android:textColor="#FFFFFF"
   android:textStyle="bold"
   android:paddingTop="10dip"
/>

<Spinner
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:id="@+id/due_date" 
   android:prompt="@string/due_date_prompt"
   android:entries="@array/due_date_entries"
/>

    <TextView
       android:layout_height="wrap_content"
       android:layout_width="wrap_content"
       android:text="Notes:"
       android:textColor="#FFFFFF"
       android:textStyle="bold"
       android:paddingTop="10dip"
    />

    <EditText
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        android:id="@+id/notes"
        android:hint="Notes..."
        android:minLines="4" 
    />

</LinearLayout>

UPDATE:

Apparently:

host.addTab(host.newTabSpec("advanced")
        .setIndicator("Advanced", res.getDrawable(android.R.drawable.ic_menu_manage))
        .setContent(R.layout.advanced_tab));

Doesn't take a layout xml file to be it's content. It should be:

host.addTab(host.newTabSpec("advanced")
        .setIndicator("Advanced", res.getDrawable(android.R.drawable.ic_menu_manage))
        .setContent(R.id.advanced_tab_layout));

Where setContent takes an id. I still get that error, but on the emulator, the tabs actually display, BUT WITH NO CONTENT!


回答1:


The exception you are listing is a bug in the layout editor itself. When running the Android layout code, we were not properly initializing the SDK version, which meant some version-conditional code in the TabWidget would get run (which fails because it's not actually running in the version it thinks). We're fixing this in ADT 10 (along with various other rendering/previewing issues for tabs.)

(This is the changeset which looks up the right version to pass to the renderer)




回答2:


Well, I just cleaned the project and deleted the edit_task.xml file and recreated it. It works fine now.




回答3:


finally a workaround exists, install the Android 3.0 Preview SDK, you can set the Render Target in the layout editor to 3.0.

More info here-> http://code.google.com/p/android/issues/detail?id=13092



来源:https://stackoverflow.com/questions/4687371/nullpointerexception-thrown-in-eclipse-when-trying-to-open-android-xml-view

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!