0

我有一个 Android Runtime Fatal Exception: main 我不知道我必须做什么来解决这个问题。我有以下代码,导致此异常:

    ... @Override
public void onItemClick(AdapterView<?> parent, View view, int position,
        long id) {
    Cursor c = (Cursor) mAdapter.getItem(position);
    // startActivity(new Intent(Intent.ACTION_VIEW,
    // Uri.parse(c.getString(c.getColumnIndex("link")))));
    // Cursor c = (Cursor) mAdapter.getItem(position);
    // String urlWebView =
    // Uri.parse(c.getString(c.getColumnIndex("link"))).toString();
    String feedDescription = Uri.parse(
            c.getString(c.getColumnIndex("description"))).toString();
    String feedTitle = Uri.parse(c.getString(c.getColumnIndex("title")))
            .toString();
    String feedLink = Uri.parse(c.getString(c.getColumnIndex("link")))
            .toString();

    String imageLink = Uri.parse(c.getString(c.getColumnIndex("img_link"))).toString();
    Intent in = new Intent(MainActivity.this, ShowNewsSummary.class);
    in.putExtra("FeedDescription", feedDescription);
    in.putExtra("FeedTitle", feedTitle);
    in.putExtra("FeedLink", feedLink);
    in.putExtra("ImageLink", imageLink);
    startActivity(in);
}

在这段代码中,我得到一个 img_link,当在数据库中找不到图像链接时,它可以为空。当我单击一个项目时ShowNewsSummaryActivity,如果在数据库中找不到 img_link,它会抛出 Fatal Main Exception,因为找不到图像地址时 AsyncTask 无法下载图像:

... @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activty_show_news_summary);
    String descriptionFeed = getIntent().getStringExtra("FeedDescription");
    String titleFeed = getIntent().getStringExtra("FeedTitle");
    imageLinkFeed = getIntent().getStringExtra("ImageLink");
    title = (TextView) findViewById(R.id.title_feed);
    description = (TextView) findViewById(R.id.description_feed);
    imageDescription = (ImageView) findViewById(R.id.descriptionImage);
    if (imageDescription != null) {
    imageDescription.setVisibility(View.VISIBLE);
    new DownloadImageAsyncTask(imageDescription).execute(imageLinkFeed);

    }
    title.setText(titleFeed);
    description.setText(descriptionFeed);
    title.setOnClickListener(this);
}

这是异常的LogCat:

09-23 22:54:42.210: E/AndroidRuntime(11885): FATAL EXCEPTION: main
09-23 22:54:42.210: E/AndroidRuntime(11885): java.lang.NullPointerException: uriString
09-23 22:54:42.210: E/AndroidRuntime(11885):    at android.net.Uri$StringUri.<init>(Uri.java:464)
09-23 22:54:42.210: E/AndroidRuntime(11885):    at android.net.Uri$StringUri.<init>(Uri.java:454)
09-23 22:54:42.210: E/AndroidRuntime(11885):    at android.net.Uri.parse(Uri.java:426)
09-23 22:54:42.210: E/AndroidRuntime(11885):    at com.fire147.samplesmartrss.MainActivity.onItemClick(MainActivity.java:176)
09-23 22:54:42.210: E/AndroidRuntime(11885):    at android.widget.AdapterView.performItemClick(AdapterView.java:298)
09-23 22:54:42.210: E/AndroidRuntime(11885):    at android.widget.AbsListView.performItemClick(AbsListView.java:1280)
09-23 22:54:42.210: E/AndroidRuntime(11885):    at android.widget.AbsListView$PerformClick.run(AbsListView.java:3083)
09-23 22:54:42.210: E/AndroidRuntime(11885):    at android.widget.AbsListView$1.run(AbsListView.java:3983)
09-23 22:54:42.210: E/AndroidRuntime(11885):    at android.os.Handler.handleCallback(Handler.java:615)
09-23 22:54:42.210: E/AndroidRuntime(11885):    at android.os.Handler.dispatchMessage(Handler.java:92)
09-23 22:54:42.210: E/AndroidRuntime(11885):    at android.os.Looper.loop(Looper.java:137)
09-23 22:54:42.210: E/AndroidRuntime(11885):    at android.app.ActivityThread.main(ActivityThread.java:4949)
09-23 22:54:42.210: E/AndroidRuntime(11885):    at java.lang.reflect.Method.invokeNative(Native Method)
09-23 22:54:42.210: E/AndroidRuntime(11885):    at java.lang.reflect.Method.invoke(Method.java:511)
09-23 22:54:42.210: E/AndroidRuntime(11885):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1043)
09-23 22:54:42.210: E/AndroidRuntime(11885):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:810)
09-23 22:54:42.210: E/AndroidRuntime(11885):    at dalvik.system.NativeStart.main(Native Method)

我的错误在哪里?

4

1 回答 1

2

在 Android Developers API Uri.Parse 中声明以下内容:

http://developer.android.com/reference/android/net/Uri.html#parse(java.lang.String)

Throws NullPointerException   if uriString is null

它出现在您的 MainActivity - onItemClick 方法中。所以这里的问题是您在 Uri.parse() 中输入的以下字符串之一为空。

调试以下代码并检查哪一个为空。

String feedDescription = Uri.parse(
            c.getString(c.getColumnIndex("description"))).toString();
    String feedTitle = Uri.parse(c.getString(c.getColumnIndex("title")))
            .toString();
    String feedLink = Uri.parse(c.getString(c.getColumnIndex("link")))
            .toString();

    String imageLink = Uri.parse(c.getString(c.getColumnIndex("img_link"))).toString();

澄清一下,以下代码之一返回空值

c.getString(c.getColumnIndex("description"))).toString();
c.getString(c.getColumnIndex("title"))).toString();
c.getString(c.getColumnIndex("link"))).toString();
c.getString(c.getColumnIndex("img_link"))).toString();

stackstrace 说的是第 176 行,但我看不到这里是哪一行,所以你必须检查一下。

编辑

你说这个是返回null的字符串。

c.getString(c.getColumnIndex("img_link"))).toString();

要解决此问题,只需使用以下代码:

String img_link = c.getString(c.getColumnIndex("img_link"));
String imageLink = (img_link == null) ? "NULL" : Uri.parse(img_link).toString();

它可能不是最干净的空检查,但您可以使用以下站点在那里找到您最好的空检查

http://howtodoinjava.com/2013/04/05/how-to-effectively-handle-nullpointerexception-in-java/

于 2014-09-23T20:47:21.853 回答