-2

活动类:

private ArrayList xmlList = new ArrayList();
private Integer konum = 0;
private Integer konumsayi = 0;
private AdView adView;
private GalleryViewPager mViewPager;

public void onCreate(Bundle savedInstanceState)
{

    Bundle extras = getIntent().getExtras();

    if(extras != null)
    {
        konum = extras.getInt("konum");
        konumsayi = konum;
    };
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    xmlList = xmloku("http://www.domain.com/database.xml");
    AdView adView = (AdView) this.findViewById(R.id.adView);

    adView.refreshDrawableState();

    adView.setVisibility(AdView.VISIBLE);

    AdRequest adRequest = new AdRequest();

    adView.loadAd(adRequest);

    UrlPagerAdapter pagerAdapter = new UrlPagerAdapter(this, xmlList);
    pagerAdapter.setOnItemChangeListener(new OnItemChangeListener()
    {
        @Override
        public void onItemChange(int currentPosition)
        {
            Toast.makeText(GalleryUrlActivity.this, "PageNumber" + (currentPosition + konumsayi), Toast.LENGTH_SHORT).show();
        }
    });


    mViewPager = (GalleryViewPager) findViewById(R.id.viewer);
    mViewPager.setOffscreenPageLimit(3);
    mViewPager.setAdapter(pagerAdapter);
}

public void onDestroy()
{
    if(adView != null)
    {
        adView.destroy();
    }
    super.onDestroy();
}

public ArrayList xmloku(String url)
{

    ArrayList xmllistesi = new ArrayList();

    try
    {
        URL xmlyolu = new URL(url);
        DocumentBuilderFactory dFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder dBuilder = dFactory.newDocumentBuilder();

        Document document = dBuilder.parse(new InputSource(xmlyolu.openStream()));
        document.getDocumentElement().normalize();

        NodeList nodeListCountry = document.getElementsByTagName("karikatur");
        for(int i = konum; i < nodeListCountry.getLength(); i++)
        {
            Node node = nodeListCountry.item(i);
            Element elementMain = (Element) node;

            NodeList nodeListText = elementMain.getElementsByTagName("karikatur");
            Element elementText = (Element) nodeListText.item(0);

            xmllistesi.add(elementText.getChildNodes().item(0).getNodeValue());
        }
    }
    catch(MalformedURLException e)
    {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    catch(SAXException e)
    {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    catch(IOException e)
    {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    catch(ParserConfigurationException e)
    {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return xmllistesi;
}

安卓清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="ru.truba.touchgallery"
    android:versionCode="6"
    android:versionName="1.3.3"
    android:installLocation="auto" >

    <uses-sdk android:minSdkVersion="4" android:targetSdkVersion="16" />

    <uses-permission android:name="android.permission.INTERNET"
     />

    <application
        android:icon="@drawable/icon"
        android:label="@string/app_name"

        android:allowBackup="false" >
        <activity
            android:name="ru.truba.touchgallery.GalleryUrlActivity"
            android:configChanges="orientation" >
        </activity>
        <activity
            android:name="ru.truba.touchgallery.GalleryFileActivity"
            android:configChanges="orientation" >
        </activity>
        <activity
            android:name="ru.truba.touchgallery.SonEklenen"
            android:configChanges="orientation" >
        </activity>
          <activity
            android:name="ru.truba.touchgallery.SecimEkran" 
             >
            <intent-filter>
                <action android:name="android.intent.action.SecimEkran" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>

        </activity>
        <activity
            android:name="ru.truba.touchgallery.MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>

        </activity>
         <activity android:name="com.google.ads.AdActivity" 
   android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"/>

    </application>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
</manifest>

对话:

10-02 17:07:36.646: E/AndroidRuntime(604): FATAL EXCEPTION: main
10-02 17:07:36.646: E/AndroidRuntime(604): java.lang.RuntimeException: Unable to start activity ComponentInfo{ru.truba.touchgallery/ru.truba.touchgallery.GalleryUrlActivity}: java.lang.NullPointerException
10-02 17:07:36.646: E/AndroidRuntime(604):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
10-02 17:07:36.646: E/AndroidRuntime(604):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
10-02 17:07:36.646: E/AndroidRuntime(604):  at android.app.ActivityThread.access$600(ActivityThread.java:130)
10-02 17:07:36.646: E/AndroidRuntime(604):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
10-02 17:07:36.646: E/AndroidRuntime(604):  at android.os.Handler.dispatchMessage(Handler.java:99)
10-02 17:07:36.646: E/AndroidRuntime(604):  at android.os.Looper.loop(Looper.java:137)
10-02 17:07:36.646: E/AndroidRuntime(604):  at android.app.ActivityThread.main(ActivityThread.java:4745)
10-02 17:07:36.646: E/AndroidRuntime(604):  at java.lang.reflect.Method.invokeNative(Native Method)
10-02 17:07:36.646: E/AndroidRuntime(604):  at java.lang.reflect.Method.invoke(Method.java:511)
10-02 17:07:36.646: E/AndroidRuntime(604):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
10-02 17:07:36.646: E/AndroidRuntime(604):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
10-02 17:07:36.646: E/AndroidRuntime(604):  at dalvik.system.NativeStart.main(Native Method)
10-02 17:07:36.646: E/AndroidRuntime(604): Caused by: java.lang.NullPointerException
10-02 17:07:36.646: E/AndroidRuntime(604):  at ru.truba.touchgallery.GalleryUrlActivity.xmloku(GalleryUrlActivity.java:144)
10-02 17:07:36.646: E/AndroidRuntime(604):  at ru.truba.touchgallery.GalleryUrlActivity.onCreate(GalleryUrlActivity.java:88)
10-02 17:07:36.646: E/AndroidRuntime(604):  at android.app.Activity.performCreate(Activity.java:5008)
10-02 17:07:36.646: E/AndroidRuntime(604):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
10-02 17:07:36.646: E/AndroidRuntime(604):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
10-02 17:07:36.646: E/AndroidRuntime(604):  ... 11 more

我的系统适用于最高 4.0 的设备,但我NullPointerException在 4.1 中得到了一个。什么可能导致这种情况?有没有人遇到过这个错误?

4

1 回答 1

0

您没有指出引发 NullPointerException 的行,但这些可能是返回null值的罪魁祸首:

Node node=nodeListCountry.item(i);

Element elementText=(Element) nodeListText.item(0);

这实际上甚至可能引发异常:

xmllistesi.add(elementText.getChildNodes().item(0).getNodeValue()

问题是,如果 XML 不包含给定元素,NodeList.item() 将返回 null...

NodeList.item(int index) 的文档

返回集合中的索引项。如果 index 大于或等于列表中的节点数,则返回 null。

解决方案:检查空...

Node node=nodeListCountry.item(i);
if(node !=null) {
    Element elementMain=(Element) node;

    NodeList nodeListText=elementMain.getElementsByTagName("karikatur");

    Element elementText=(Element) nodeListText.item(0);
    if(elementText!=null) {
        Node n = elementText.getChildNodes().item(0);
        if(n!=null) {
           xmllistesi.add(n.getNodeValue());
        }
    }
}
于 2013-10-02T19:00:55.020 回答