Project with Recycler View that has multiple layouts and in one layout it has a simple line graph. So Screen has buttons and when i click at them it displays the layout that i want in the list.
FATAL EXCEPTION: main
Process: com.example.rv_multipllayouts, PID: 16703
java.lang.NullPointerException: Attempt to invoke virtual method 'void com.jjoe64.graphview.GraphView.addSeries(com.jjoe64.graphview.series.Series)' on a null object reference
at com.example.rv_multipllayouts.MainActivity$1.onClick(MainActivity.java:60)
`package com.example.rvmultipllayouts;
public class ExampleAdapter extends RecyclerView.Adapter{
private ArrayList mExampleList;
private static final int TYPE_ONE = 1;
private static final int TYPE_TWO = 2;
private static final int TYPE_THREE=3;
private static final int TYPE_FOUR =4;
// public static class ExampleViewHolder extends RecyclerView.ViewHolder {
// public TextView mTextView1;
// public TextView mTextView2;
//
// public ExampleViewHolder(View itemView) {
// super(itemView);
// mTextView1 = itemView.findViewById(R.id.textView01);
// mTextView2 = itemView.findViewById(R.id.textView02);
// }
// }
public ExampleAdapter(ArrayList<ExampleItem> exampleList) {
mExampleList = exampleList;
}
// determine which layout to use for the row
@Override
public int getItemViewType(int position) {
ExampleItem exampleItem = mExampleList.get(position);
if (exampleItem.getType() == ExampleItem.ItemType.ONE_ITEM) {
return TYPE_ONE;
} else if (exampleItem.getType() == ExampleItem.ItemType.TWO_ITEM) {
return TYPE_TWO;
} else if (exampleItem.getType() == ExampleItem.ItemType.THREE_ITEM) {
return TYPE_THREE;
} else if (exampleItem.getType() == ExampleItem.ItemType.FOUR_ITEM) {
return TYPE_FOUR;
} else {
return -1;
}
}
// @Override
// public ExampleViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
// View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.example_item, parent, false);
// ExampleViewHolder evh = new ExampleViewHolder(v);
// return evh;
// }
// specify the row layout file and click for each row
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
if (viewType == TYPE_ONE) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.example_item, parent, false);
return new ViewHolderOne(view);
} else if (viewType == TYPE_TWO) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.example_item2, parent, false);
return new ViewHolderTwo(view);
} else if (viewType == TYPE_THREE) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.example_item3, parent, false);
return new ViewHolderThree(view);
} else if (viewType == TYPE_FOUR) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.example_item4, parent, false);
return new ViewHolderFour(view);
} else {
throw new RuntimeException("The type has to be ONE or TWO or Three");
}
}
public class MainActivity extends AppCompatActivity {
private RecyclerView mRecyclerView;
private RecyclerView.Adapter mAdapter;//Bridge between data and image/ arrayList and RecyclerView
private RecyclerView.LayoutManager mLayoutManager;//allign single items in our list
private Button button1, button2, button3, button4;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ArrayList<ExampleItem> exampleList = new ArrayList<>();
button1 = findViewById(R.id.button1);
button2 = findViewById(R.id.button2);
button3 = findViewById(R.id.button3);
button4 = findViewById(R.id.button4);
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
exampleList.clear();
//mAdapter.notifyDataSetChanged();
for(int i=0;i<10;i++){
exampleList.add(new ExampleItem("Text1.", "Num-" + i, ExampleItem.ItemType.ONE_ITEM));
}
mRecyclerView = findViewById(R.id.recyclerView);
mRecyclerView.setHasFixedSize(true);
mLayoutManager = new LinearLayoutManager(getApplicationContext());
mAdapter = new ExampleAdapter(exampleList);
mRecyclerView.setLayoutManager(mLayoutManager);
mRecyclerView.setAdapter(mAdapter);
//GraphCode
GraphView graph = (GraphView) findViewById(R.id.graph);
LineGraphSeries<DataPoint> series = new LineGraphSeries<DataPoint>(new DataPoint[] {
new DataPoint(0, 1),
new DataPoint(1, 5),
new DataPoint(2, 3),
new DataPoint(3, 2),
new DataPoint(4, 6)
});
graph.addSeries(series);
//exampleList.clear();
//mAdapter.notifyDataSetChanged();
}
});
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
exampleList.clear();
//mAdapter.notifyDataSetChanged();
for(int i=0;i<2;i++){
exampleList.add(new ExampleItem("Text1.", "Text2.", "Num-" + i, ExampleItem.ItemType.TWO_ITEM));
}
mRecyclerView = findViewById(R.id.recyclerView);
mRecyclerView.setHasFixedSize(true);
mLayoutManager = new LinearLayoutManager(getApplicationContext());
mAdapter = new ExampleAdapter(exampleList);
mRecyclerView.setLayoutManager(mLayoutManager);
mRecyclerView.setAdapter(mAdapter);
}
});
button3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
exampleList.clear();
//mAdapter.notifyDataSetChanged();
for(int i=0;i<2;i++){
exampleList.add(new ExampleItem("Text1.", "Text2.", "Num-" + i, ExampleItem.ItemType.THREE_ITEM));
}
mRecyclerView = findViewById(R.id.recyclerView);
mRecyclerView.setHasFixedSize(true);
mLayoutManager = new LinearLayoutManager(getApplicationContext());
mAdapter = new ExampleAdapter(exampleList);
mRecyclerView.setLayoutManager(mLayoutManager);
mRecyclerView.setAdapter(mAdapter);
}
});
button4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
exampleList.clear();
//mAdapter.notifyDataSetChanged();
exampleList.add(new ExampleItem("Text1", ExampleItem.ItemType.FOUR_ITEM));
mRecyclerView = findViewById(R.id.recyclerView);
mRecyclerView.setHasFixedSize(true);
mLayoutManager = new LinearLayoutManager(getApplicationContext());
mAdapter = new ExampleAdapter(exampleList);
mRecyclerView.setLayoutManager(mLayoutManager);
mRecyclerView.setAdapter(mAdapter);
}
});
//GraphCode
GraphView graph = (GraphView) findViewById(R.id.graph2);
LineGraphSeries<DataPoint> series = new LineGraphSeries<DataPoint>(new DataPoint[] {
new DataPoint(0, 1),
new DataPoint(1, 5),
new DataPoint(2, 3),
new DataPoint(3, 2),
new DataPoint(4, 6)
});
graph.addSeries(series);
}
Project with Recycler View that has multiple layouts and in one layout it has a simple line graph. So Screen has buttons and when i click at them it displays the layout that i want in the list.
FATAL EXCEPTION: main Process: com.example.rv_multipllayouts, PID: 16703 java.lang.NullPointerException: Attempt to invoke virtual method 'void com.jjoe64.graphview.GraphView.addSeries(com.jjoe64.graphview.series.Series)' on a null object reference at com.example.rv_multipllayouts.MainActivity$1.onClick(MainActivity.java:60) `package com.example.rvmultipllayouts;
import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.TextView;
import androidx.recyclerview.widget.RecyclerView;
import java.util.ArrayList;
public class ExampleAdapter extends RecyclerView.Adapter{
private ArrayList mExampleList;
private static final int TYPE_ONE = 1;
private static final int TYPE_TWO = 2;
private static final int TYPE_THREE=3;
private static final int TYPE_FOUR =4;
// public static class ExampleViewHolder extends RecyclerView.ViewHolder { // public TextView mTextView1; // public TextView mTextView2; // // public ExampleViewHolder(View itemView) { // super(itemView); // mTextView1 = itemView.findViewById(R.id.textView01); // mTextView2 = itemView.findViewById(R.id.textView02); // } // }
// @Override // public ExampleViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { // View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.example_item, parent, false); // ExampleViewHolder evh = new ExampleViewHolder(v); // return evh; // }
// @Override // public void onBindViewHolder(ExampleViewHolder holder, int position) { // ExampleItem currentItem = mExampleList.get(position); // // holder.mTextView1.setText(currentItem.getText1()); // holder.mTextView2.setText(currentItem.getText2()); // }
}
package com.example.rv_multipllayouts;
public class ExampleItem {
}
package com.example.rv_multipllayouts;
import androidx.appcompat.app.AppCompatActivity; import androidx.recyclerview.widget.LinearLayoutManager; import androidx.recyclerview.widget.RecyclerView;
import android.os.Bundle; import android.view.View; import android.widget.Button;
import com.jjoe64.graphview.GraphView; import com.jjoe64.graphview.series.DataPoint; import com.jjoe64.graphview.series.LineGraphSeries;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity { private RecyclerView mRecyclerView; private RecyclerView.Adapter mAdapter;//Bridge between data and image/ arrayList and RecyclerView private RecyclerView.LayoutManager mLayoutManager;//allign single items in our list private Button button1, button2, button3, button4;
}`