2011-07-12 19 views
0

コードを実行しようとするとjson例外が発生します。 問題はonclickリスナーで発生します。私は、アプリケーションがここクリックでRSSフィードの記事へonclickをlistview/jsonに設定するとエラーが発生する

を行く持ってしようとしているここでの主な活動

public class Blocku extends ListActivity { 

private RssListAdapter adapter; 

/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    List<JSONObject> jobs = new ArrayList<JSONObject>(); 
    try { 
     jobs = BlockuReader.getLatestRssFeed(); 
    } catch (Exception e) { 
     Log.e("RSS ERROR", "Error loading RSS Feed Stream >> " + e.getMessage() + " //" + e.toString()); 
    } 

    adapter = new RssListAdapter(this,jobs); 
    setListAdapter(adapter); 
} 

protected void onListItemClick(ListView l, View v, int position, long id) { 
     super.onListItemClick(l, v, position, id); 

     String link = null; 
    try { 
     String url = Article.getUrl().toString(); 

     link = adapter.getItem(position).getString(url).toString(); 
     Intent i = new Intent(Intent.ACTION_VIEW); 
      i.setData(Uri.parse(link)); 
      startActivity(i); 
    } catch (JSONException e) { 
     Context context = getApplicationContext(); 
     CharSequence text = "error"; 
     int duration = Toast.LENGTH_SHORT; 

     Toast toast = Toast.makeText(context, text, duration); 
     toast.show(); 
     e.printStackTrace(); 
    } 

    } 

} 

アダプタですよ。

public class RssListAdapter extends ArrayAdapter<JSONObject> { 

public RssListAdapter(Activity activity, List<JSONObject> imageAndTexts) { 
    super(activity, 0, imageAndTexts); 
} 


@Override 
public View getView(int position, View convertView, ViewGroup parent) { 

    Activity activity = (Activity) getContext(); 
    LayoutInflater inflater = activity.getLayoutInflater(); 

    // Inflate the views from XML 
    View rowView = inflater.inflate(R.layout.image_text_layout, null); 
    JSONObject jsonImageText = getItem(position); 


    ////////////////////////////////////////////////////////////////////////////////////////////////////// 
    //The next section we update at runtime the text - as provided by the JSON from our REST call 
    //////////////////////////////////////////////////////////////////////////////////////////////////// 
    TextView textView = (TextView) rowView.findViewById(R.id.job_text); 

    try { 
     Spanned text = (Spanned)jsonImageText.get("text"); 
     textView.setText(text); 

    } catch (JSONException e) { 
     textView.setText("JSON Exception"); 
    } 

    return rowView; 

} 


} 

および記事クラス

public class Article { 

private long articleId; 
private long feedId; 
private String title; 
private String description; 
private String pubDate; 
private static URL url; 
private String encodedContent; 

private static String link; 
/** 
* @return the articleId 
*/ 
public long getArticleId() { 
    return articleId; 
} 
/** 
* @param articleId the articleId to set 
*/ 
public void setArticleId(long articleId) { 
    this.articleId = articleId; 
} 
/** 
* @return the feedId 
*/ 
public long getFeedId() { 
    return feedId; 
} 
/** 
* @param feedId the feedId to set 
*/ 
public void setFeedId(long feedId) { 
    this.feedId = feedId; 
} 
/** 
* @return the title 
*/ 
public String getTitle() { 
    return title; 
} 
/** 
* @param title the title to set 
*/ 
public void setTitle(String title) { 
    this.title = title; 
} 
/** 
* @return the url 
*/ 
public static URL getUrl() { 
    return url; 
} 
/** 
* @param url the url to set 
*/ 
public void setUrl(URL url) { 
    Article.url = url; 
} 
/** 
* @param description the description to set 
*/ 
public void setDescription(String description) { 
    this.description = description; 
} 
/** 
* @return the description 
*/ 
public String getDescription() { 
    return description; 
} 
/** 
* @param pubDate the pubDate to set 
*/ 
public void setPubDate(String pubDate) { 
    this.pubDate = pubDate; 
} 
/** 
* @return the pubDate 
*/ 
public String getPubDate() { 
    return pubDate; 
} 
/** 
* @param encodedContent the encodedContent to set 
*/ 
public void setEncodedContent(String encodedContent) { 
    this.encodedContent = encodedContent; 
} 
/** 
* @return the encodedContent 
*/ 
public String getEncodedContent() { 
    return encodedContent; 
} 


} 
+0

エラーメッセージは何ですか? –

+0

私はデバッガを実行しているときlogcatは履歴を表示しないので、現在の行だけが表示されます。どのように私は歴史全体を見ることができます。申し訳ありません、私はまだ学んでいます。私は実行中です。eclipse btw – poerg

+0

私にとっては、logcatをEclipseで表示すると、ログがいくつかのサイズ制限に達した後、あなたが何を記述しているのか:最後の行だけが表示されます。現在のログをクリアするためにボタンを押すと、物事は正常に戻ります。同様に、Eclipseを再起動するか、別のエミュレータ(または実際のデバイス)に切り替えるだけで問題は解決します。 –

答えて

1

JSONExceptionは、通常、解析に問題がある場合にスローされます。 JSONの解析方法を確認してください。これは、「あなたが望むサイトをリストしている」という名前のキーの値を取得しようとしていると言います。

+0

私はonclickの中身を " "に置き換えることでエラーを取り除く方法を考え出しましたString link = Article.getUrl()。toString(); Intent i = new Intent(Intent.ACTION_VIEW); i.setData(Uri最後の記事のURLをつかむだけです。 – poerg

+0

link = adapter.getItem(position).getString(url).toString(.parse(link)); startActivity(i); " );;あなたは、リストアダプタからその位置にアイテムを取得する必要があります。そのメソッドをオーバーライドするか、リストのゲッターを作成します。アイテムの位置は、コードのどこかに含める必要があります。 – DArkO

+0

私はあなたが与えた文字列にエラーが発生しました。そのURLは変数に解決できません。何か案は? – poerg

関連する問題