2015-02-23 57 views
-2

嗨我單擊活動鏈接時遇到空指針錯誤。我的功能的目的是以列表視圖的形式調用鏈接列表。下面是logcat的而據我的理解是從我onItemClickListener到來,但我似乎無法指出空錯誤:無法啓動活動:空指針異常

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(爲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> 

這裏是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) 

任何幫助是很感激,謝謝!

+0

我建議檢查:com.example.sgrecipe.Links.onCreate(Links.java:36)。看起來這裏有一個空對象。 – 2015-02-23 07:34:30

回答

1

更改以下行

setContentView(R.layout.activity_main); 

setContentView(R.layout.activity_links); 
0

好像你用不同的佈局:

setContentView(R.layout.activity_main); 

VS

activity_links.xml 
+0

omg謝謝,我覺得這麼愚蠢,沒有看到這個,這麼小的錯誤。 – zana 2015-02-23 07:35:45

相關問題