Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/188.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在android中使用SharedReference向ListView活动添加收藏夹列表活动_Java_Android_Xml - Fatal编程技术网

Java 如何在android中使用SharedReference向ListView活动添加收藏夹列表活动

Java 如何在android中使用SharedReference向ListView活动添加收藏夹列表活动,java,android,xml,Java,Android,Xml,这就是我试图创建的listView类型:当用户单击列表时,它会将用户带到newActivity,用户将在其中找到listView的完整内容。并在长按时将列表项添加到favoritesList 在学习了有关如何使用BaseAdapter在Listview中添加favoritesActivity的教程之后,我遇到了一个错误。就是,;我的应用程序在启动时强制关闭。我知道我遗漏了一些行,或者可能在不知不觉中添加了可能导致应用程序崩溃的非格式化对象。我已经为您提供了logCat报告,以帮助我修复导致崩溃的

这就是我试图创建的listView类型:当用户单击列表时,它会将用户带到newActivity,用户将在其中找到listView的完整内容。并在长按时将列表项添加到favoritesList

在学习了有关如何使用BaseAdapter在Listview中添加favoritesActivity的教程之后,我遇到了一个错误。就是,;我的应用程序在启动时强制关闭。我知道我遗漏了一些行,或者可能在不知不觉中添加了可能导致应用程序崩溃的非格式化对象。我已经为您提供了logCat报告,以帮助我修复导致崩溃的问题。我是编程新手,我想学习使用这个程序。我是一名学生,我需要你的帮助,以便完成我的项目

我的所有尝试和错误都包含在显示的代码中。请你帮我解决问题。谢谢

这是MainActivity.java

public class MainActivity extends AppCompatActivity implements SearchView.OnQueryTextListener {
    private String TAG = "MainActivity ----- ; " ;
    // Store instance variables

    private int page;
    private ConsentForm form;

    ListView listView;
    ListViewAdapter adapter;
    String[] title;
    String[] description;
    int[] icon;
    ArrayList<Model> arrayList = new ArrayList<Model>();


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

        ActionBar actionBar = getSupportActionBar();
        actionBar.setTitle("Redeemed Songs");

        title = new String[]{"This is the lyrics content example of the song1, This is the lyrics content example of the song2, This is the lyrics content example of the song3, This is the lyrics content example of the song4, This is the lyrics content example of the song5",};

        description = new String[]{"MORNING", "MORNING", "MORNING", "MORNING", "MORNING", "MORNING",};

        icon = new int[]{ R.drawable.song, R.drawable.song, R.drawable.song, R.drawable.song,R.drawable.song,R.drawable.song,};

        listView = findViewById(R.id.list);

        for (int i =0; i<title.length; i++){
            Model model =new Model(title[i], description[i], icon[i]);
            //bind all strings in an array
            arrayList.add(model);
        }

        //pass result to listview class
        adapter = new ListViewAdapter(this, arrayList);

        //bind the adapter to the listview class
        listView.setAdapter(adapter);


    }


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

        MenuItem myActionMenuItem = menu.findItem(R.id.action_search);
        SearchView searchView = (SearchView)myActionMenuItem.getActionView();
        searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String s) {
                return false;
            }

            @Override
            public boolean onQueryTextChange(String s) {
                if (TextUtils.isEmpty(s)){
                    adapter.filter("");
                    listView.clearTextFilter();
                }
                else {
                    adapter.filter(s);
                }
                return true;
            }
        });

        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();

        if (id==R.id.action_settings){
            Toast.makeText(this, "Settings", Toast.LENGTH_SHORT).show();
            startActivity(new Intent(this, Main2Activity.class));
            return true;
            //do your funtionality here

        }
        else if (id==R.id.action_howtouse){
                Toast.makeText(this, "manual", Toast.LENGTH_SHORT).show();
            startActivity(new Intent(this, Main3Activity.class));
            return true;
                //do your funtionality here


        }
        else if (id==R.id.action_developers){
            Toast.makeText(this, "Favorites", Toast.LENGTH_SHORT).show();
            startActivity(new Intent(this, Main4Activity.class));
            return true;
            //do your funtionality here

        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public boolean onQueryTextSubmit(String query) {
        return false;
    }

    @Override
    public boolean onQueryTextChange(String newText) {
        return false;
    }
}

这是row.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:gravity="center"
    android:padding="10dp">

    <ImageView
        android:id="@+id/mainIcon"
        android:src="@drawable/song"
        android:layout_width="30dp"
        android:layout_height="40dp" />
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:gravity="center"
        android:layout_marginLeft="5dp"
        android:layout_marginStart="5dp">

        <TextView
            android:id="@+id/mainTitle"
            android:textStyle="bold"
            android:textColor="#740303"
            android:text="Title"
            android:textSize="15sp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
        <TextView
            android:id="@+id/mainDesc"
            android:text="Description"
            android:textColor="#262626"
            android:textSize="12sp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
        <ImageView
            android:id="@+id/list_fav_icon"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:src="@drawable/ic_favorite_active"
            android:layout_gravity="right"
            android:layout_margin="5dp"
            android:layout_weight="1"/>
    </LinearLayout>
</LinearLayout>
你在打电话吗

List<Model> favorites = sharedPreference.getFavorites(mContext);
List favorites=SharedReference.getFavorites(mContext);
其中SharedReference从未实例化。因此抛出一个NullPointerException。尝试在构造函数中实例化对象

//Constructor
    public ListViewAdapter(Context context, List<Model> modellist) {
        mContext = context;
        this.modellist = modellist;
        inflater = LayoutInflater.from(mContext);
        this.arrayList = new ArrayList<Model>();
        this.arrayList.addAll(modellist);
        this.sharedPreference = new SharedPreference();
}
//构造函数
公共ListViewAdapter(上下文上下文,列表模型列表){
mContext=上下文;
this.modellist=modellist;
充气器=从(mContext)开始的充气器;
this.arrayList=新的arrayList();
this.arrayList.addAll(modellist);
this.SharedReference=新的SharedReference();
}
您正在呼叫

List<Model> favorites = sharedPreference.getFavorites(mContext);
List favorites=SharedReference.getFavorites(mContext);
其中SharedReference从未实例化。因此抛出一个NullPointerException。尝试在构造函数中实例化对象

//Constructor
    public ListViewAdapter(Context context, List<Model> modellist) {
        mContext = context;
        this.modellist = modellist;
        inflater = LayoutInflater.from(mContext);
        this.arrayList = new ArrayList<Model>();
        this.arrayList.addAll(modellist);
        this.sharedPreference = new SharedPreference();
}
//构造函数
公共ListViewAdapter(上下文上下文,列表模型列表){
mContext=上下文;
this.modellist=modellist;
充气器=从(mContext)开始的充气器;
this.arrayList=新的arrayList();
this.arrayList.addAll(modellist);
this.SharedReference=新的SharedReference();
}

是的,你说得很对。我忘了实例化SharedReference。谢谢,我有一个新问题。listView项是分散的,我无法找出问题所在。你能帮我处理你的电子邮件吗?这样我就可以为你附上完整的项目代码来帮助我修复。请我想拉链的项目,并发送给你,以帮助我修复。谢谢,你说得很对。我忘了实例化SharedReference。谢谢,我有一个新问题。listView项是分散的,我无法找出问题所在。你能帮我处理你的电子邮件吗?这样我就可以为你附上完整的项目代码来帮助我修复。请我想拉链的项目,并发送给你,以帮助我修复。谢谢
public class FavouritesAdapter extends BaseAdapter{

    //Variables
    Context mContext;
    LayoutInflater inflater;
    List<Model> modellist;
    ArrayList<Model> arrayList;
    private Model model;

    //Constructor
    public FavouritesAdapter(Context context, List<Model> modellist) {
        mContext = context;
        this.modellist = modellist;
        inflater = LayoutInflater.from(mContext);
        this.arrayList = new ArrayList<Model>();
        this.arrayList.addAll(modellist);

    }

    public class ViewHolder{
        TextView mTitleTv, mDescTv;
        ImageView mIconTv, favIcon;
    }

    @Override
    public int getCount() {
        return modellist.size();
    }

    @Override
    public Object getItem(int i) {
        return modellist.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(final int i, View view, ViewGroup parent) {
        final ViewHolder holder;
        if (view==null){
            holder = new ViewHolder();
            view = inflater.inflate(R.layout.row, null);

            //locate the views in row.xml
            holder.mTitleTv = (TextView) view.findViewById(R.id.mainTitle);
            holder.mDescTv = (TextView) view.findViewById(R.id.mainDesc);
            holder.mIconTv = view.findViewById(R.id.mainIcon);

            view.setTag(holder);
        }
        else {
            holder = (ViewHolder)view.getTag();
        }

        //set the result into textview
        holder.mTitleTv.setText(modellist.get(i).getTitle());
        holder.mDescTv.setText(modellist.get(i).getDesc());
        //Set the result in imagview
        holder.mIconTv.setImageResource(modellist.get(i).getIcon());

        //listview item clicks
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //code later
                if (modellist.get(i).getTitle().equals("This is the lyrics content example of the song1")){
                    //start NewActivity with title for actionbar and text for textview
                    Intent intent =  new Intent(mContext, NewActivity.class);
                    intent.putExtra("actionBarTitle", "Song 001");
                    intent.putExtra("contentTv", "This is the lyrics content example of the song2");
                    mContext.startActivity(intent);
                }
                if (modellist.get(i).getTitle().equals("This is the lyrics content example of the song1")){
                    //start NewActivity with title for actionbar and text for textview
                    Intent intent =  new Intent(mContext, NewActivity.class);
                    intent.putExtra("actionBarTitle", "Song 002");
                    intent.putExtra("contentTv", "This is the lyrics content example of the song2");
                    mContext.startActivity(intent);
                }
                if (modellist.get(i).getTitle().equals("This is the lyrics content example of the song3")){
                    //start NewActivity with title for actionbar and text for textview
                    Intent intent =  new Intent(mContext, NewActivity.class);
                    intent.putExtra("actionBarTitle", "Song 003");
                    intent.putExtra("contentTv", "This is the lyrics content example of the song3");
                    mContext.startActivity(intent);
                }
                if (modellist.get(i).getTitle().equals("This is the lyrics content example of the song4")){
                    //start NewActivity with title for actionbar and text for textview
                    Intent intent =  new Intent(mContext, NewActivity.class);
                    intent.putExtra("actionBarTitle", "Song 004");
                    intent.putExtra("contentTv", "This is the lyrics content example of the song4");
                    mContext.startActivity(intent);
                }
                if (modellist.get(i).getTitle().equals("This is the lyrics content example of the song5")){
                    //start NewActivity with title for actionbar and text for textview
                    Intent intent =  new Intent(mContext, NewActivity.class);
                    intent.putExtra("actionBarTitle", "Song 005");
                    intent.putExtra("contentTv", "This is the lyrics content example of the song5");
                    mContext.startActivity(intent);
                }

            }
        });


        return view;
    }

    //filter
    public void filter(String charText){
        charText = charText.toLowerCase(Locale.getDefault());
        modellist.clear();
        if (charText.length()==0){
            modellist.addAll(arrayList);
        }
        else {
            for (Model model : arrayList){
                if (model.getTitle().toLowerCase(Locale.getDefault()).contains(charText)){
                    modellist.add(model);
                }
            }
        }
        notifyDataSetChanged();
    }

}

    --------- beginning of crash
2019-10-22 02:02:39.715 11214-11214/com.gritchen.redeemedsongs E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.gritchen.redeemedsongs, PID: 11214
    java.lang.NullPointerException: Attempt to invoke virtual method 'java.util.ArrayList com.gritchen.redeemedsongs.SharedPreference.getFavorites(android.content.Context)' on a null object reference
        at com.gritchen.redeemedsongs.ListViewAdapter.checkFavoriteItem(ListViewAdapter.java:372)
        at com.gritchen.redeemedsongs.ListViewAdapter.getView(ListViewAdapter.java:86)
        at android.widget.AbsListView.obtainView(AbsListView.java:2365)
        at android.widget.ListView.makeAndAddView(ListView.java:2052)
        at android.widget.ListView.fillDown(ListView.java:786)
        at android.widget.ListView.fillFromTop(ListView.java:847)
        at android.widget.ListView.layoutChildren(ListView.java:1826)
        at android.widget.AbsListView.onLayout(AbsListView.java:2164)
        at android.view.View.layout(View.java:20323)
        at android.view.ViewGroup.layout(ViewGroup.java:6199)
        at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1083)
        at android.view.View.layout(View.java:20323)
        at android.view.ViewGroup.layout(ViewGroup.java:6199)
        at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
        at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
        at android.view.View.layout(View.java:20323)
        at android.view.ViewGroup.layout(ViewGroup.java:6199)
        at android.support.v7.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:443)
        at android.view.View.layout(View.java:20323)
        at android.view.ViewGroup.layout(ViewGroup.java:6199)
        at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
        at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
        at android.view.View.layout(View.java:20323)
        at android.view.ViewGroup.layout(ViewGroup.java:6199)
        at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1791)
        at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1635)
        at android.widget.LinearLayout.onLayout(LinearLayout.java:1544)
        at android.view.View.layout(View.java:20323)
        at android.view.ViewGroup.layout(ViewGroup.java:6199)
        at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
        at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
        at com.android.internal.policy.DecorView.onLayout(DecorView.java:764)
        at android.view.View.layout(View.java:20323)
        at android.view.ViewGroup.layout(ViewGroup.java:6199)
        at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2612)
        at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2317)
        at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1453)
        at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:7047)
        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:924)
        at android.view.Choreographer.doCallbacks(Choreographer.java:732)
        at android.view.Choreographer.doFrame(Choreographer.java:664)
        at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:910)
        at android.os.Handler.handleCallback(Handler.java:790)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:164)
        at android.app.ActivityThread.main(ActivityThread.java:6524)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:888)
2019-10-22 02:02:39.737 11214-11253/com.gritchen.redeemedsongs D/NetworkSecurityConfig: No Network Security Config specified, using platform default
2019-10-22 02:02:39.899 11214-11253/com.gritchen.redeemedsongs D/libc-netbsd: getaddrinfo: get result from proxy gai_error = 0
2019-10-22 02:02:39.918 11214-11253/com.gritchen.redeemedsongs I/System.out: port:443
2019-10-22 02:02:41.595 11214-11239/com.gritchen.redeemedsongs

List<Model> favorites = sharedPreference.getFavorites(mContext);
//Constructor
    public ListViewAdapter(Context context, List<Model> modellist) {
        mContext = context;
        this.modellist = modellist;
        inflater = LayoutInflater.from(mContext);
        this.arrayList = new ArrayList<Model>();
        this.arrayList.addAll(modellist);
        this.sharedPreference = new SharedPreference();
}