Java 无法启动活动:NullPointerException

Java 无法启动活动:NullPointerException,java,android,Java,Android,嗨,当我点击活动链接时,我遇到了一个空指针错误。我的功能的目的是以列表视图的形式调用链接列表。以下是logcat,据我所知,它来自我的侦听器,但我似乎无法指出空错误: sitesList.setOnItemClickListener(new OnItemClickListener() Links.java package com.example.sgrecipe; import java.io.FileNotFoundException; import android.app.Activi

嗨,当我点击活动链接时,我遇到了一个空指针错误。我的功能的目的是以列表视图的形式调用链接列表。以下是logcat,据我所知,它来自我的侦听器,但我似乎无法指出空错误:

sitesList.setOnItemClickListener(new OnItemClickListener()
Links.java

package com.example.sgrecipe;

import java.io.FileNotFoundException;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

public class Links extends Activity {


private SitesAdapter mAdapter;
private ListView sitesList;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Log.i("StackSites", "OnCreate()");
    setContentView(R.layout.activity_main);

    //Get reference to our ListView
    sitesList = (ListView)findViewById(R.id.sitesList);

    //Set the click listener to launch the browser when a row is clicked.
    sitesList.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View v, int pos,long id) {
            String url = mAdapter.getItem(pos).getLink();
            Intent i = new Intent(Intent.ACTION_VIEW);
            i.setData(Uri.parse(url));
            startActivity(i);

        }

    });

    /*
     * If network is available download the xml from the Internet.
     * If not then try to use the local file from last time.
     */
    if(isNetworkAvailable()){
        Log.i("StackSites", "starting download Task");
        SitesDownloadTask download = new SitesDownloadTask();
        download.execute();
    }else{
        mAdapter = new SitesAdapter(getApplicationContext(), -1, SitesXmlPullParser.getStackSitesFromFile(Links.this));
        sitesList.setAdapter(mAdapter);
    }

}

//Helper method to determine if Internet connection is available.
private boolean isNetworkAvailable() {
    ConnectivityManager connectivityManager 
          = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
    return activeNetworkInfo != null && activeNetworkInfo.isConnected();
} 

/*
 * AsyncTask that will download the xml file for us and store it locally.
 * After the download is done we'll parse the local file.
 */
private class SitesDownloadTask extends AsyncTask<Void, Void, Void>{

    @Override
    protected Void doInBackground(Void... arg0) {
        //Download the file
        try {
            Downloader.DownloadFromUrl("https://dl.dropboxusercontent.com/u/5724095/XmlParseExample/stacksites.xml", openFileOutput("StackSites.xml", Context.MODE_PRIVATE));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

        return null;
    }

    @Override
    protected void onPostExecute(Void result){
        //setup our Adapter and set it to the ListView.
        mAdapter = new SitesAdapter(Links.this, -1, SitesXmlPullParser.getStackSitesFromFile(Links.this));
        sitesList.setAdapter(mAdapter);
        Log.i("StackSites", "adapter size = "+ mAdapter.getCount());
    }
}

}

任何帮助都是非常感谢的,谢谢

似乎您使用了不同的布局:

setContentView(R.layout.activity_main);
vs

换行

setContentView(R.layout.activity_main);


我建议检查一下:com.example.sgrecipe.Links.onCreate(Links.java:36)。似乎有一个空的对象在那里。天哪,谢谢你,我觉得自己太傻了,没有看到这个,这么小的错误。
02-23 15:13:10.856: E/AndroidRuntime(8201): FATAL EXCEPTION: main
02-23 15:13:10.856: E/AndroidRuntime(8201): Process: com.example.sgrecipe, PID: 8201
02-23 15:13:10.856: E/AndroidRuntime(8201): java.lang.RuntimeException: Unable to start activity    ComponentInfo{com.example.sgrecipe/com.example.sgrecipe.Links}: java.lang.NullPointerException
02-23 15:13:10.856: E/AndroidRuntime(8201):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2292)
02-23 15:13:10.856: E/AndroidRuntime(8201):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2350)
02-23 15:13:10.856: E/AndroidRuntime(8201):     at android.app.ActivityThread.access$800(ActivityThread.java:163)
02-23 15:13:10.856: E/AndroidRuntime(8201):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1257)
02-23 15:13:10.856: E/AndroidRuntime(8201):     at android.os.Handler.dispatchMessage(Handler.java:102)
02-23 15:13:10.856: E/AndroidRuntime(8201):     at android.os.Looper.loop(Looper.java:157)
02-23 15:13:10.856: E/AndroidRuntime(8201):     at android.app.ActivityThread.main(ActivityThread.java:5335)
02-23 15:13:10.856: E/AndroidRuntime(8201):     at java.lang.reflect.Method.invokeNative(Native Method)
02-23 15:13:10.856: E/AndroidRuntime(8201):     at java.lang.reflect.Method.invoke(Method.java:515)
02-23 15:13:10.856: E/AndroidRuntime(8201):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
02-23 15:13:10.856: E/AndroidRuntime(8201):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
02-23 15:13:10.856: E/AndroidRuntime(8201):     at dalvik.system.NativeStart.main(Native Method)
02-23 15:13:10.856: E/AndroidRuntime(8201): Caused by: java.lang.NullPointerException
02-23 15:13:10.856: E/AndroidRuntime(8201):     at com.example.sgrecipe.Links.onCreate(Links.java:36)
02-23 15:13:10.856: E/AndroidRuntime(8201):     at android.app.Activity.performCreate(Activity.java:5389)
02-23 15:13:10.856: E/AndroidRuntime(8201):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
02-23 15:13:10.856: E/AndroidRuntime(8201):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2256)
setContentView(R.layout.activity_main);
activity_links.xml
setContentView(R.layout.activity_main);
setContentView(R.layout.activity_links);