NotesListActivity. java
package com.example.notes;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.nfc.Tag;
import android.os.Bundle;
import android.util.Log;
import com.example.notes.Adapters.NotesRecyclerAdapter;
import com.example.notes.Models.Note;
import java.util.ArrayList;
public class NotesListActivity extends AppCompatActivity
{
private static final String TAG = "NotesListActivity";
//UI COMPONENTS
private RecyclerView mrecyclerview;
//VARIABLE SECTION
private ArrayList<Note> mNotes=new ArrayList<>();
private NotesRecyclerAdapter mNotesRecylerAdapter;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_notes_list);
mrecyclerview=findViewById(R.id.recyclerView);
initRecyclerView();
fakeNotes();
}
private void fakeNotes() //method to populate reccylerview for testing
{
for(int i=0;i<1000;i++)
{
Note note=new Note();
note.setTitle("Title#"+i);
note.setContent("content #"+i);
note.setTimestamp(" jan 2019");
mNotes.add(note);
}
mNotesRecylerAdapter.notifyDataSetChanged();
}
private void initRecyclerView()
{
LinearLayoutManager linerlayoutmanager=new LinearLayoutManager(this);
mrecyclerview.setLayoutManager(linerlayoutmanager);
mNotesRecylerAdapter=new NotesRecyclerAdapter(mNotes);
mrecyclerview.setAdapter(mNotesRecylerAdapter);
}
}
NotesRecyclerAdapter. java (адаптер)
package com.example.notes.Adapters;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.example.notes.Models.Note;
import com.example.notes.R;
import java.util.ArrayList;
public class NotesRecyclerAdapter extends RecyclerView.Adapter<NotesRecyclerAdapter.ViewHolder>
{
private ArrayList<Note> mNotes=new ArrayList<>();
public NotesRecyclerAdapter(ArrayList<Note> mNotes)
{
this.mNotes = mNotes;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType)
{
View view=
LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_notes_list_item,parent,false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position)
{
holder.timestamp.setText(mNotes.get(position).getTimestamp());
holder.title.setText(mNotes.get(position).getTitle());
}
@Override
public int getItemCount()
{
return mNotes.size();
}
public class ViewHolder extends RecyclerView.ViewHolder
{
TextView title, timestamp;
public ViewHolder(@NonNull View itemView)
{
super(itemView);
title =itemView.findViewById(R.id.note_title);
timestamp=itemView.findViewById(R.id.note_timestamp);
}
}
}
activity_notes_list. xml (представление Recycler)
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".NotesListActivity">
<androidx.recyclerview.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/recyclerView"
android:orientation="vertical"
android:padding="5dp">
layout_notes_list_item. xml (макет представления переработчика)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/darkYellow"
android:weightSum="100"
android:gravity="center_vertical"
>
<view
android:layout_width="0dp"
android:layout_height="match_parent"
android:background="@color/darkGrey"
android:layout_weight="1"
/>
<TextView
android:id="@+id/note_title"
android:text="This is my first note"
android:textColor="@color/black"
android:textSize="23sp"
android:padding="10dp"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="75"
android:lines="1"/>
<view
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="4"
/>
<TextView
android:id="@+id/note_timestamp"
android:text="12jan2019"
android:textColor="@color/white"
android:textSize="23sp"
android:padding="10dp"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="20"
android:lines="1"/>
</LinearLayout>
Примечание. java
package com.example.notes.Models;
public class Note
{
private String title;
private String content;
private String timestamp;
public Note(String title, String content, String timestamp)
{
this.title = title;
this.content = content;
this.timestamp = timestamp;
}
public Note()
{ }
public String getTitle()
{
return title;
}
public void setTitle(String title)
{
this.title = title;
}
public String getContent()
{
return content;
}
public void setContent(String content) {
this.content = content;
}
public String getTimestamp() {
return timestamp;
}
public void setTimestamp(String timestamp) {
this.timestamp = timestamp;
}
@Override
public String toString()
{
return "Note{" +
"title='" + title + '\'' +
", content='" + content + '\'' +
", timestamp='" + timestamp + '\'' +
'}';
}
}
ОШИБКА, ПОЛУЧАЮЩАЯ И ПРИЛОЖЕНИЕ ПРИКЛЮЧАЕТСЯ ИЗ-ЗА ЭТОГО! Я Изучаю android сейчас, и я даже проверил исходный код, но не могу найти никаких отличий.
D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.notes, PID: 2846
android.view.InflateException: Binary XML file line #11: Attempt to invoke virtual method 'boolean
java.lang.String.equals(java.lang.Object)' on a null object reference
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean
java.lang.String.equals(java.lang.Object)' on a null object reference
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:764)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:730)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:863)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:824)
at android.view.LayoutInflater.inflate(LayoutInflater.java:515)
at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
at
com.example.notes.Adapters.NotesRecyclerAdapter.onCreateViewHolder(NotesRecyclerAdapter.java:31)
at
com.example.notes.Adapters.NotesRecyclerAdapter.onCreateViewHolder(NotesRecyclerAdapter.java:15)
at androidx.recyclerview.widget.RecyclerView$Adapter.createViewHolder(RecyclerView.java:6794)
at
androidx.recyclerview.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline
(RecyclerView.jaVa:5975)
at androidx.recyclerview.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5858)
at androidx.recyclerview.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5854)
at
androidx.recyclerview.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2230)
at androidx.recyclerview.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1557)
at androidx.recyclerview.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1517)
at
androidx.recyclerview.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:612)
at androidx.recyclerview.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3924)
at androidx.recyclerview.widget.RecyclerView.dispatchLayout(RecyclerView.java:3641)
at androidx.recyclerview.widget.RecyclerView.onLayout(RecyclerView.java:4194)
at android.view.View.layout(View.java:19590)
at android.view.ViewGroup.layout(ViewGroup.java:6053)
at androidx.constraintlayout.widget.ConstraintLayout.onLayout(ConstraintLayout.java:1915)
at android.view.View.layout(View.java:19590)
at android.view.ViewGroup.layout(ViewGroup.java:6053)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
at android.view.View.layout(View.java:19590)
at android.view.ViewGroup.layout(ViewGroup.java:6053)
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:19590)
at android.view.ViewGroup.layout(ViewGroup.java:6053)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
at android.view.View.layout(View.java:19590)
at android.view.ViewGroup.layout(ViewGroup.java:6053)
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:19590)
at android.view.ViewGroup.layout(ViewGroup.java:6053)
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:758)
at android.view.View.layout(View.java:19590)
at android.view.ViewGroup.layout(ViewGroup.java:6053)
at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2484)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2200)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1386)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6733)
E/AndroidRuntime: at android.view.Choreographer$CallbackRecord.run(Choreographer.java:911)
at android.view.Choreographer.doCallbacks(Choreographer.java:723)
at android.view.Choreographer.doFrame(Choreographer.java:658)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:897)
at android.os.Handler.handleCallback(Handler.java:789)
at android.os.Handler.dispatchMessage(Handler.java:98)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6541)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
Application terminated.