-2

Hi I am encountering a null pointer error when I click on the activity Links. The purpose of my feature is to call a list of links in the form of list view. Below is the logcat and as far as I understand it is coming from my onItemClickListener, but I can't seem to point out the null error.:

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());
    }
}

}

activity_links.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Links" >

<ListView
    android:id="@+id/sitesList"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

</RelativeLayout>

row_site.xml (for the listview)

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp" >

<ProgressBar
    android:id="@+id/progress"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

<ImageView
    android:id="@+id/iconImg"
    android:layout_width="80dp"
    android:layout_height="80dp"
    android:layout_marginRight="8dp" />

<TextView
    android:id="@+id/nameTxt"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_toRightOf="@id/iconImg"
    android:textSize="16sp"
    android:textStyle="bold" />

<TextView
    android:id="@+id/aboutTxt"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_toRightOf="@id/iconImg"
    android:textSize="12sp"
    android:layout_below="@id/nameTxt"
    />

</RelativeLayout>

Here is the logcat:

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)

Any help is really appreciate, thank you!

zana
  • 259
  • 2
  • 4
  • 15
  • I suggest checking at: com.example.sgrecipe.Links.onCreate(Links.java:36). It seems there's a null object there. – nic Feb 23 '15 at 07:34

2 Answers2

0

Seems like your using different layouts:

setContentView(R.layout.activity_main);

vs

activity_links.xml
Simas
  • 43,548
  • 10
  • 88
  • 116
0

Change following line

setContentView(R.layout.activity_main);

to

setContentView(R.layout.activity_links);
Nagaraju V
  • 2,739
  • 1
  • 14
  • 19