应用程序无法打开链接和崩溃

时间:2018-04-10 18:27:16

标签: java android crash news-feed

我正在构建一个简单的rss新闻阅读器应用程序,与其他2个人一起为学校项目工作。 我们已经加载了纽约时代rss部分的链接,但是当我点击链接时,应用程序崩溃了。

这是logcat:

04-10 20:20:29.451 22669-22686/? I/OpenGLRenderer: Initialized EGL, version 1.4
04-10 20:20:29.451 22669-22686/? D/OpenGLRenderer: Swap behavior 1
04-10 20:20:29.471 22669-22669/? E/RecyclerView: No adapter attached; skipping layout
04-10 20:20:29.518 22669-22669/? W/art: Before Android 4.1, method int android.support.v7.widget.DropDownListView.lookForSelectablePosition(int, boolean) would have incorrectly overridden the package-private method in android.widget.ListView
04-10 20:20:29.533 22669-22669/? E/RecyclerView: No adapter attached; skipping layout
04-10 20:20:29.809 22669-22669/? I/Timeline: Timeline: Activity_idle id: android.os.BinderProxy@ac486df time:93727463
04-10 20:20:38.775 22669-22669/com.example.m.myapplication I/ViewRootImpl: ViewRoot's Touch Event : ACTION_DOWN
04-10 20:20:38.777 22669-22669/com.example.m.myapplication V/BoostFramework: BoostFramework() : mPerf = com.qualcomm.qti.Performance@9016573
04-10 20:20:38.824 22669-22669/com.example.m.myapplication I/ViewRootImpl: ViewRoot's Touch Event : ACTION_UP
04-10 20:20:38.827 22669-22669/com.example.m.myapplication I/AudioManagerEx: AudioManagerEx created
04-10 20:20:38.831 22669-22669/com.example.m.myapplication D/AndroidRuntime: Shutting down VM
04-10 20:20:38.832 22669-22669/com.example.m.myapplication E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.m.myapplication, PID: 22669
    java.lang.NullPointerException: Attempt to invoke interface method 'void com.example.m.myapplication.Interface.ItemClickListener.onClick(android.view.View, int, boolean)' on a null object reference
        at com.example.m.myapplication.Adapter.FeedViewHolder.onClick(FeedAdapter.java:38)
        at android.view.View.performClick(View.java:5623)
        at android.view.View$PerformClick.run(View.java:22433)
        at android.os.Handler.handleCallback(Handler.java:751)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:154)
        at android.app.ActivityThread.main(ActivityThread.java:6247)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:872)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:762)

我很确定这个错误非常愚蠢,但我在应用程序的开发中是一个菜鸟,所以我找不到它。

这是主要活动:

package com.example.m.myapplication;

import android.annotation.SuppressLint;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;

import com.example.m.myapplication.Adapter.FeedAdapter;
import com.example.m.myapplication.Common.HTTPDataHandler;
import com.example.m.myapplication.Model.RSSObject;
import com.google.gson.Gson;

public class MainActivity extends AppCompatActivity {

    Toolbar toolbar;
    RecyclerView recyclerView;
    RSSObject rssObject;

    private final String RSS_link="http://feeds.nytimes.com/nyt/rss/Technology";
    private final String RSS_to_Json_API=" https://api.rss2json.com/v1/api.json?rss_url=";

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

        toolbar = (Toolbar)findViewById(R.id.toolbar);
        toolbar.setTitle("News");
        setSupportActionBar(toolbar);

        recyclerView = (RecyclerView)findViewById(R.id.recyclerView);
        LinearLayoutManager LinearLayoutManager = new LinearLayoutManager(getBaseContext(), android.support.v7.widget.LinearLayoutManager.VERTICAL,false);
        recyclerView.setLayoutManager(LinearLayoutManager);

        loadRSS();
    }

    @SuppressLint("StaticFieldLeak")
    private void loadRSS(){
        AsyncTask<String,String,String> loadRSSAsync = new AsyncTask<String, String, String>() {

            ProgressDialog mDialog = new ProgressDialog(MainActivity.this);

            @Override
            protected void onPreExecute() {
                mDialog.setMessage("Please wait...");
                mDialog.show();
            }

            @Override
            protected String doInBackground(String... params) {
                String result;
                HTTPDataHandler http = new HTTPDataHandler();
                result = http.getHTTPData(params[0]);
                return result;
            }

            @Override
            protected void onPostExecute(String s) {
                mDialog.dismiss();
                rssObject = new Gson().fromJson(s,RSSObject.class);
                FeedAdapter adapter = new FeedAdapter(rssObject,getBaseContext());
                recyclerView.setAdapter(adapter);
                adapter.notifyDataSetChanged();
            }
        };

        StringBuilder url_get_data = new StringBuilder(RSS_to_Json_API);
        url_get_data.append(RSS_link);
        loadRSSAsync.execute(url_get_data.toString());
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main_menu,menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == R.id.menu_refresh)
            loadRSS();
        return true;
    }
}

Feed Feed:

package com.example.m.myapplication.Adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.example.m.myapplication.Interface.ItemClickListener;
import com.example.m.myapplication.Model.RSSObject;
import com.example.m.myapplication.R;

class FeedViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener,View.OnLongClickListener{

    public TextView txtTitle,txtPubDate,txtContent;
    private ItemClickListener itemClickListener;

    public FeedViewHolder(View itemView) {
        super(itemView);

        txtTitle = (TextView)itemView.findViewById(R.id.txtTitle);
        txtPubDate = (TextView)itemView.findViewById(R.id.txtPubDate);
        txtContent = (TextView)itemView.findViewById(R.id.txtContent);


        itemView.setOnClickListener(this);
        itemView.setOnLongClickListener(this);
    }

    public void setItemClickListener(ItemClickListener itemClickListener) {
        this.itemClickListener = itemClickListener;
    }

    @Override
    public void onClick(View v) {
        itemClickListener.onClick(v,getAdapterPosition(),false);
    }

    @Override
    public boolean onLongClick(View v) {
        itemClickListener.onClick(v,getAdapterPosition(),true);
        return true;
    }
}

public class FeedAdapter extends RecyclerView.Adapter<FeedViewHolder>{

    private RSSObject rssObject;
    private Context mContext;
    private LayoutInflater inflater;

    public FeedAdapter(RSSObject rssObject, Context mContext) {
        this.rssObject = rssObject;
        this.mContext = mContext;
        inflater = LayoutInflater.from(mContext);
    }

    @Override
    public FeedViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View itemView = inflater.inflate(R.layout.row,parent,false);
        return new FeedViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(@NonNull FeedViewHolder holder, int position) {
        holder.txtTitle.setText(rssObject.getItems().get(position).getTitle());
        holder.txtPubDate.setText(rssObject.getItems().get(position).getPubDate());
        holder.txtContent.setText(rssObject.getItems().get(position).getContent());
    }

    @Override
    public int getItemCount() {
        return rssObject.items.size();
    }
}

1 个答案:

答案 0 :(得分:0)

您的问题是您尚未初始化itemClickListener

你需要在任何点击之前调用你的setItemClickListener()