2011-05-09 13 views
-1

嘿所有..我很震惊,我必须在新闻链接网站的单独列表视图中显示新闻,但是当我调试光标变为空时。我该如何解决这个问题?这里是我的代码Java Android Eclipse的

package adn.GoMizzou.NB; 

import java.io.IOException; 
import java.io.UnsupportedEncodingException; 
import java.net.URI; 
import java.net.URISyntaxException; 

import javax.xml.parsers.ParserConfigurationException; 
import javax.xml.parsers.SAXParser; 
import javax.xml.parsers.SAXParserFactory; 

import org.apache.http.HttpEntity; 
import org.apache.http.HttpResponse; 
import org.apache.http.client.ClientProtocolException; 
import org.apache.http.client.HttpClient; 
import org.apache.http.client.methods.HttpPost; 
import org.apache.http.entity.BufferedHttpEntity; 
import org.apache.http.entity.StringEntity; 
import org.apache.http.impl.client.DefaultHttpClient; 
import org.xml.sax.InputSource; 
import org.xml.sax.SAXException; 
import org.xml.sax.XMLReader; 

import android.app.ListActivity; 
import android.app.ProgressDialog; 
import android.content.Context; 
import android.content.Intent; 
import android.content.SharedPreferences; 
import android.database.Cursor; 
import android.net.Uri; 
import android.os.Bundle; 
import android.os.Handler; 
import android.os.Message; 
import android.view.View; 
import android.widget.ListView; 
import android.widget.SimpleCursorAdapter; 
import android.widget.TextView; 
import android.widget.Toast; 



public class NB extends ListActivity { 
    public static final String URL = "http://nbsubscribe.missouri.edu/news-releases/feed/atom/"; 

    private String msg; 
    private boolean success; 
    private int scrollIndex; 
    private int scrollTop; 
    private HttpClient httpClient; 
    private NBDBAdapter dbAdapter; 
    private ProgressDialog pDialog; 
    private Context ctx = this; 
    private SharedPreferences prefs; 
    private SharedPreferences.Editor prefsEditor; 
    //private Cursor cur; 
    private Cursor q; 

    private Handler handler = new Handler() { 
     @Override 
     public void handleMessage(Message msg){ 
      pDialog.dismiss(); 
      fillList(); 
     } 
    }; 

    /* ACTIVITY METHODS */ 
    @Override 
    public void onCreate(Bundle savedInstanceState){ 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.nb); 
     setTitle("News"); 
     q=null; 
     scrollIndex = 0; 
     dbAdapter = new NBDBAdapter(this); 
     dbAdapter.open(); 

     registerForContextMenu(getListView()); 

     getData(); 
    } 

    public void getData(){ 
     pDialog = ProgressDialog.show(this, "", "Loading. Please wait...", true); 
     new Thread(){ 
      public void run(){ 
       dbAdapter.deleteAll(); 
       dbAdapter.close(); 

       doPost(URL, ""); 

       dbAdapter.open(); 
       handler.sendEmptyMessage(0); 
      } 
     }.start(); 
    } 

    public boolean doPost(String url, String postMsg){ 
     HttpResponse response = null; 

     createHttpClient(); 
     try { 
      URI uri = new URI(url); 
      HttpPost httppost = new HttpPost(uri); 
      StringEntity postEntity = new StringEntity(postMsg); 
      httppost.setHeader("Content-Type", "application/x-www-form-urlencoded"); 
      postEntity.setContentType("application/x-www-form-urlencoded"); 
      httppost.setEntity(postEntity); 
      response = httpClient.execute(httppost); 
     } catch (UnsupportedEncodingException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } catch (ClientProtocolException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } catch (URISyntaxException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 

     if(response == null){ 
      msg = "No internet connection."; 
      return false; 
     } 

     if(response.getStatusLine().getStatusCode()!= 200){ 
      msg = "Server error."; 
      return false; 
     } 

     return doParse(response); 
    } 

    public void createHttpClient(){ 
     if(httpClient == null){ 
      httpClient = new DefaultHttpClient(); 
     } 
    } 

    public boolean doParse(HttpResponse response){ 
     try { 
      SAXParserFactory spf = SAXParserFactory.newInstance(); 
      SAXParser sp = spf.newSAXParser(); 
      XMLReader xr = sp.getXMLReader(); 

      NBParser respHandler = new NBParser(ctx); 
      xr.setContentHandler(respHandler); 

      HttpEntity entity = response.getEntity(); 
      BufferedHttpEntity buffEntity = new BufferedHttpEntity(entity); 

      xr.parse(new InputSource(buffEntity.getContent())); 
     } catch (ParserConfigurationException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } catch (SAXException e) { 
      e.printStackTrace(); 
      if(e.getMessage().toString().contains("nothing found")){ 
       msg = e.getMessage().toString(); 
       return false; 
      } 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 

     return true; 
    } 
    protected void onListItemClick(ListView l, View v, int position, long id) { 
     super.onListItemClick(l, v, position, id); 
     Cursor c = q; 
     q.moveToPosition(position); 
     //Intent i = new Intent(this, NB.class); 
     //i.putExtra("link", c.getString(c.getColumnIndexOrThrow(NBDBAdapter.NB_LINK))); 
     String newsLinkString = q.getString(q.getColumnIndexOrThrow(NBDBAdapter.NB_LINK)); 

     TextView linkTV = (TextView) v.findViewById(R.id.rowlink); 
     newsLinkString = linkTV.getText().toString(); 

     if (newsLinkString.startsWith("http://")){ 
      Uri uri = Uri.parse(newsLinkString); 
      Intent i = new Intent(this, NB.class); 
      // Save ListView position 
      i.putExtra("link", c.getString(c.getColumnIndexOrThrow(NBDBAdapter.NB_LINK))); 

      startActivity(i); 
     } 
    }  

     //else { 
      //showLinkError(); 
     //} 
//  Sends an error message to the user if the news item link is malformed 
     //public void showLinkError() { 
      //Toast.makeText(getApplicationContext(), "Error - Link to story unavailable with news source could not load the News Story", Toast.LENGTH_SHORT).show(); 
     //} 


    private void showLinkError() { 
     // TODO Auto-generated method stub 
     Toast.makeText(getApplicationContext(), "Error - Link to story unavailable with news source could not load the News Story", Toast.LENGTH_SHORT).show(); 

    } 

    public void fillList(){ 
     if(!success) { 
      TextView emptyView = (TextView) findViewById(android.R.id.empty); 
      emptyView.setText(msg); 
      msg = ""; 
     } 
     if(!dbAdapter.isOpen()) dbAdapter.open(); 
     Cursor cursor = dbAdapter.fetchAll(); 
     startManagingCursor(cursor); 

     String [] from = new String[] { dbAdapter.NB_AUTHOR, dbAdapter.NB_TITLE,dbAdapter.NB_LINK }; 
     int[] to = new int[] { R.id.rowAuthor, R.id.rowTitle, R.id.rowlink }; 

     SimpleCursorAdapter list = new SimpleCursorAdapter(this, R.layout.row, cursor, from, to); 
     setListAdapter(list); 
    } 


} 
+0

1)我已经格式化了您的代码,将来会突出显示代码块并单击“{}”按钮将其一次全部格式化。 2)请把它减少到能够证明问题的最小代码量(并且最好是一个完全可编译的例子)。为了提供有用的答案,有人需要挖掘太多代码。有关提问的帮助,请参阅此处:http://tinyurl.com/so-hints – eldarerathis 2011-05-09 20:45:39

回答

0

你在说什么光标?

如果它是当你点击一个项目,这是因为你在onCreate中分配游标q null,然后在onListItemClick上调用它的moveToPosition。