执行方法空指针异常时出错

时间:2019-04-30 07:55:20

标签: android nullpointerexception android-asynctask

我正在尝试在我的应用中实施RSS提要。我从网站上获取了此代码。我在按钮单击监听器内的执行方法附近滑动了Java.lang.NullPointerException并刷新刷新监听器。我的应用程序无法启动;只会在Android手机中引发错误。

@Override
    protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_main);

    mRecyclerView = (RecyclerView) 
    findViewById(R.id.recyclerView);

      mEditText = (EditText) 
   findViewById(R.id.rssFeedEditText);

    mFetchFeedButton = (Button) 
  findViewById(R.id.fetchFeedButton);

    mSwipeLayout = (SwipeRefreshLayout) 
   findViewById(R.id.swipeRefreshLayout);

    mFeedTitleTextView = (TextView) 
    findViewById(R.id.feedTitle);

    mFeedDescriptionTextView = (TextView) 
    findViewById(R.id.feedDescription);

    mFeedLinkTextView = (TextView) 
    findViewById(R.id.feedLink);

    mRecyclerView.setLayoutManager(new 
     LinearLayoutManager(this));

    mFetchFeedButton.setOnClickListener(new 
    View.OnClickListener() {
        @Override
        public void onClick(View view) {
            new FetchFeedTask().execute((Void) null);
        }
    });
    mSwipeLayout.setOnRefreshListener(new 
   SwipeRefreshLayout.OnRefreshListener() {
        @Override
        public void onRefresh() {
            new FetchFeedTask().execute((Void) null);
        }
    });
   }
}

  // FetchFeedTask class

 private class FetchFeedTask extends AsyncTask<Void, 
 Void, Boolean> {

    private String urlLink;

    @Override
    protected void onPreExecute() {
        mSwipeLayout.setRefreshing(true);
        urlLink = mEditText.getText().toString();
    }

    @Override
    protected Boolean doInBackground(Void... voids) {
        if (TextUtils.isEmpty(urlLink))
            return false;

        try {
            if(!urlLink.startsWith("http://") && 
            !urlLink.startsWith("https://"))
                urlLink = "http://" + urlLink;

            URL url = new URL(urlLink);
            InputStream inputStream = 
    url.openConnection().getInputStream();
            mFeedModelList = parseFeed(inputStream);
            return true;
        } catch (IOException e) {
            Log.e(TAG, "Error", e);
        } catch (XmlPullParserException e) {
            Log.e(TAG, "Error", e);
        }
        return false;
    }

    @Override
    protected void onPostExecute(Boolean success) {
        mSwipeLayout.setRefreshing(false);

        if (success) {
            mFeedTitleTextView.setText("Feed Title: " + 
       mFeedTitle);
            mFeedDescriptionTextView.setText("Feed 
   Description: " + mFeedDescription);
            mFeedLinkTextView.setText("Feed Link: " + 
    mFeedLink);

            // Fill RecyclerView
            mRecyclerView.setAdapter(new 
    RssFeedListAdapter(mFeedModelList));
        } else {
            Toast.makeText(MainActivity.this,
                    "Enter a valid Rss feed url",
                    Toast.LENGTH_LONG).show();
         }
    }
  }

1 个答案:

答案 0 :(得分:0)

您应该使用没有任何参数的.execute()方法。最重要的是,Void不是可以在方法中作为参数传递的数据类型。

所以只需使用

new FetchFeedTask().execute();

代替

new FetchFeedTask().execute((Void) null);

在两个点击监听器中