I am getting the following error
Attempt to invoke virtual method 'void
android.widget.StackView.setAdapter(android.widget.Adapter)' on a null
object reference
on this line
stackView.setAdapter(adapter);
The complete fragment EventsFragment.java is
public class EventsFragment extends android.support.v4.app.Fragment {
private StackView stackView;
private ArrayList<Stack_Items> list;
TypedArray eventLogo ;
String eventName[];
#Nullable
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
eventLogo= getResources().obtainTypedArray(R.array.event_stack_icon);
eventName = getResources().getStringArray(R.array.event_stack);
stackView = (StackView) getActivity().findViewById(R.id.stackView1);
list = new ArrayList<Stack_Items>();
//Adding items to the list
for (int i = 0; i < eventLogo.length(); i++) {
list.add(new Stack_Items(eventName[i], eventLogo.getResourceId(i,-1)));
}
//Calling adapter and setting it over stackView
Stack_Adapter adapter = new Stack_Adapter(getActivity().getApplicationContext(), list );
stackView.setAdapter(adapter);
adapter.notifyDataSetChanged();
return inflater.inflate(R.layout.events_layout, null);
}
}
Stack_Adapter.java
public class Stack_Adapter extends BaseAdapter {
ArrayList<Stack_Items> arrayList;
LayoutInflater inflater;
ViewHolder holder = null;
public Stack_Adapter(Context context, ArrayList<Stack_Items> arrayList) {
this.arrayList = arrayList;
this.inflater = LayoutInflater.from(context);
}
#Override
public int getCount() {
return arrayList.size();
}
#Override
public Stack_Items getItem(int pos) {
return arrayList.get(pos);
}
#Override
public long getItemId(int pos) {
return pos;
}
#Override
public View getView(int pos, View view, ViewGroup parent) {
if (view == null) {
view = inflater.inflate(R.layout.stack_layout, parent, false);
holder = new ViewHolder();
holder.text = (TextView) view.findViewById(R.id.textView1);
holder.image = (ImageView) view.findViewById(R.id.imageView1);
view.setTag(holder);
} else {
holder = (ViewHolder) view.getTag();
}
holder.text.setText(arrayList.get(pos).getName());
holder.image.setBackgroundResource(arrayList.get(pos).getImage());
return view;
}
public class ViewHolder {
TextView text;
ImageView image;
}
}
Stack_Items
public class Stack_Items {
String name;
Integer image;
public Stack_Items(String name, Integer image) {
this.name = name;
this.image = image;
}
public String getName() {
return name;
}
public int getImage() {
return image;
}
}
You are doing:
stackView = (StackView) getActivity().findViewById(R.id.stackView1);
Your stackView is null. getActivity().findViewById returns null.
Why are you using getActivity()?
Where is the stackView? You should load it from the right xml.
as #Tauqir mentioned, you need to inflate the right xml like this:
View view = inflater.inflate(R.layout.events_layout, null);
stackView = (StackView) view.findViewById(R.id.stackView1);
return view;
Try this inside onCreateView
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.events_layout, null);
-------------some codes ------------
stackView = (StackView) view.findViewById(R.id.stackView1);
-------------some codes ------------
return view;
}
Do this
View rootView = inflater.inflate(R.layout.events, container, false);
stackView = (StackView) rootview.findViewById(R.id.stackView1);
.
.
.
return rootview
You first need to inflate the layout and call findViewById() on the View it returns, thus getActivity().findViewById should be rootView.findViewByID.
There are some other issues with your code as well. Like getctivity().getApplicationContext, just getActivity() is fine.
R.id.stackView1 is not found when you try to assign stackView, so it is null
change container and false to null
Do this:
View rootView = inflater.inflate(R.layout.events, null);
Related
i want know why getview() function is not call
maybe override is wrong?
i want make custom listlayout
what's wrong in listviewadapter?
public class ListViewAdapter extends BaseAdapter {
private Context mContext;
private ArrayList<Word> items;
public ListViewAdapter(Context mContext, ArrayList<Word> items) {
this.mContext = mContext;
this.items = items;
}
#Override
public int getCount() {
return items.size();
}
#Override
public Object getItem(int i) {
return items.get(i);
}
#Override
public long getItemId(int i) {
return i;
}
#Override
public View getView(int i, View view, ViewGroup viewGroup) {
if (view == null) {
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.wordlist_layout, viewGroup, false);
}
TextView wordview1 = view.findViewById(R.id.wordView1);
System.out.println("wordlist layout = " + items.get(i).w1);
wordview1.setText(items.get(i).w1);
return view;
}
// this is my Main activity code
private ArrayList<Word> items;
private ListViewAdapter listviewadapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn = findViewById(R.id.submit_Button);
btn.setOnClickListener(view -> {
ListView word_listview = findViewById(R.id.Wordle_Word_List);
items = new ArrayList<Word>();
items.add(new Word('a'));
listviewadapter = new ListViewAdapter(getApplicationContext(), items);
word_listview.setAdapter(listviewadapter);
});
}
i dont know what is missing in my code
In ListviewAdapter I dont know why getview() function not call
I think your problem is the condition in its body, not the whole function. There is no need to check if it is null or not. you can simply inflate the view as mentioned in this article:
#Override
public View getView(int i, View view, ViewGroup viewGroup) {
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.wordlist_layout, viewGroup, false);
TextView wordview1 = view.findViewById(R.id.wordView1);
System.out.println("wordlist layout = " + items.get(i).w1);
wordview1.setText(items.get(i).w1);
return view;
}
#Override
public View getView(int i, View view, ViewGroup viewGroup) {
view = LayoutInflater.from(mContext).inflate(R.layout.wordlist_layout, viewGroup, false);
TextView wordview1 = view.findViewById(R.id.wordView1);
wordview1.setText(items.get(i).w1);
return view;
}
I just created a listview and I can see all the rows at my listview exept to the first one. anyone got any idea why this is happend. my adapter code is:
public class ChatAdapter extends ArrayAdapter<Message> {
public ChatAdapter(Context context, int resource, List<Message> items) {
super(context, resource, items);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater vi = LayoutInflater.from(getContext());
v = vi.inflate(R.layout.chat_raw, null);
}
Message message = getItem(position);
Log.d("GET VIEW MESSAGE: ", message.getText());
if (message != null) {
TextView tvName = (TextView) v.findViewById(R.id.tvChatName);
TextView tvMessage = (TextView) v.findViewById(R.id.tvChatMessage);
if (tvName != null) {
tvName.setText(message.getUserName());
}
if (tvMessage != null) {
tvMessage.setText(message.getText());
}
}
return v;
}
}
I must say that at line: Log.d("GET VIEW MESSAGE: ", message.getText()); I see all the messages and also the 1st one but at the listview I don't see the 1st one.
edit:
Just change the code according to what you seggest but I still have the same problem, I can't see the 1st row. my adapter code is:
public class ChatAdapter extends ArrayAdapter<Message> {
private Context context;
public ChatAdapter(Context context, int resource, List<Message> items) {
super(context, resource, items);
this.context = context;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
ViewHolder viewHolder=null;
if(convertView==null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(R.layout.chat_raw, null);
viewHolder = new ViewHolder(row);
row.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) row.getTag();
}
Message message = getItem(position);
if(message!=null) {
viewHolder.tvMessage.setText(message.getText());
viewHolder.tvName.setText(message.getUserName());
}
return row;
}
public class ViewHolder {
TextView tvName;
TextView tvMessage;
public ViewHolder(View view) {
tvName = (TextView) view.findViewById(R.id.tvChatName);
tvMessage = (TextView) view.findViewById(R.id.tvChatMessage);
}
}
}
and my code is:
public class ChatFragment extends Fragment {
private ListView lvChat;
private Button btChatSend;
private EditText etChatMessage;
private ArrayList<Message> alMessage;
private int groupID, userId;
private String userName;
private RequestQueue requestQueue;
private Gson gson;
public ChatFragment() {
// Required empty public constructor
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
alMessage = new ArrayList<>();
groupID = ((DrawAndChat) getActivity()).getGroupId();
userName = ((DrawAndChat) getActivity()).getUserName();
userId = ((DrawAndChat) getActivity()).getUserId();
requestQueue = Volley.newRequestQueue(getActivity());
getAllMessages();
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.chat_fragment, container, false);
lvChat = (ListView) view.findViewById(R.id.lvChat);
etChatMessage = (EditText) view.findViewById(R.id.etChatMessage);
btChatSend = (Button) view.findViewById(R.id.btChatSend);
lvChat.setAdapter(new ChatAdapter(getActivity(), R.layout.chat_raw, alMessage));
btChatSend.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
sendMessage();
}
});
return view;
}
I have Fragment.java and its XML. In XML I put ListView and I want to update the ListView in Fragment and add some String.
I am trying to make a custom adapter. When I run the ListView in Activity with custom adapter all works great, but when I put the same code in Fragment nothing happens. I am using EventBus3.
This is my Activity send Event
EventBus.getDefault().post(new SendDataHelper(nameOfItem[myItemInt]));
This is my Fragment:
public class MyOrderFragment extends Fragment {
TextView name;
ListView listOrder;
private Context context;
List<cources> collegeList;
public MyOrderFragment() {
// Required empty public constructor
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
EventBus.getDefault().register(this);
}
#Subscribe
public void onMessageEvent(SendDataHelper event){
Toast.makeText(getActivity(), event.getText(), Toast.LENGTH_SHORT).show();
String text = event.getText();
name.setText(text);
cources college;
collegeList = new ArrayList<cources>();
college = new cources();
college.name = "sharon";
collegeList.add(college);
listOrder.setVisibility(View.VISIBLE);
ListAdapter adapter = new ListAdapter(collegeList, context);
listOrder.setAdapter(adapter);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.my_order_fragment, container, false);
name = (TextView) rootView.findViewById(R.id.textView);
listOrder = (ListView) rootView.findViewById(R.id.listOrder);
return rootView;
}
}
This is my adapter
public class ListAdapter extends BaseAdapter
{
Context context;
List<cources> valueList;
public ListAdapter(List<cources> listValue, Context context)
{
this.context = context;
this.valueList = listValue;
}
#Override
public int getCount()
{
return this.valueList.size();
}
#Override
public Object getItem(int position)
{
return this.valueList.get(position);
}
#Override
public long getItemId(int position)
{
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent)
{
ViewItem viewItem = null;
if(convertView == null)
{
viewItem = new ViewItem();
LayoutInflater layoutInfiater = (LayoutInflater)this.context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
//LayoutInflater layoutInfiater = LayoutInflater.from(context);
convertView = layoutInfiater.inflate(R.layout.list_adapter_view, null);
viewItem.txtNamePlanche = (TextView)convertView.findViewById(R.id.Tx_name_planche);
viewItem.txtPriceBaget = (TextView)convertView.findViewById(R.id.Tx_price_baget);
viewItem.txtPricePlate = (TextView)convertView.findViewById(R.id.Tx_price_plate);
convertView.setTag(viewItem);
}
else
{
viewItem = (ViewItem) convertView.getTag();
}
viewItem.txtNamePlanche.setText(valueList.get(position).name);
viewItem.txtPriceBaget.setText(valueList.get(position).price_baget);
viewItem.txtPricePlate.setText(valueList.get(position).price_plate);
return convertView;
}
}
This is my ItemView
import android.widget.TextView;
class ViewItem
{
TextView txtNamePlanche;
TextView txtPricePlate;
TextView txtPriceBaget;
}
This is cources:
public class cources
{
public String name;
public String price_plate;
public String price_baget;
}
Change extends Fragment to extends ListFragment
In my app I have PostersFragment that will be a GridView with movies posters, in this fragment I need to initialize the GridView and customGridViewAdapter.
My customGridViewAdapter need to get the Bitmaps array and then work with it.
The problem that I call to AsyncTask that is in different class that gets all information from JSON and stores the information with the movies posters on local database.
I cant understand how and when to use the .setAdapter(gridViewCustomAdapter);
There is my code.
public class PostersFragment extends Fragment implements AdapterView.OnItemClickListener {
private GridView gv_posters;
private GridViewCustomAdapter gridViewCustomAdapter;
public PostersFragment() {
setHasOptionsMenu(true);
}
#Override
public void onStart() {
super.onStart();
UpdatePosters();
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Log.d("TESTAG","onCreateView");
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
gv_posters = (GridView) rootView.findViewById(R.id.gv_posters);
gv_posters.setAdapter(gridViewCustomAdapter);
gv_posters.setOnScrollListener(new PostersScrollListener(getActivity().getApplicationContext()));
gv_posters.setOnItemClickListener(this);
// Define new adapter for grid view
return rootView;
}
private void UpdatePosters() {
Log.d("TESTAG","UpdatePoster");
MoviesTask task = new MoviesTask(getActivity(),gridViewCustomAdapter);
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getActivity());
String path = prefs.getString(getString(R.string.sort_method_posters_pref), getString(R.string.default_path));
task.execute(path);
}
MoviesTask.java
public class MoviesTask extends AsyncTask<String, Void, Bitmap[]> {
// Byte array to handle bitmaps
private byte[] imgByte;
// Context and custom adapter variables
private final Context mContext;
private GridViewCustomAdapter mGridCustomAdapter;
// MovieTask Constructor
public MoviesTask(Context context, GridViewCustomAdapter gridAdapter) {
mContext = context;
mGridCustomAdapter = gridAdapter;
}
Parsing JSON methods...
#Override
protected void onPostExecute(Bitmap[] bitmaps){
Log.d("TESTAG","onPostExecute");
if (bitmaps != null)
mGridCustomAdapter = new GridViewCustomAdapter(mContext,R.id.single_gv_poster,bitmaps);
}
GridViewCustomAdapter.java
public class GridViewCustomAdapter extends BaseAdapter{
private Context context;
int layoutResource;
private Bitmap[] bitmaps;
public GridViewCustomAdapter(Context context,int layoutResource, Bitmap[] bitmaps){
this.context = context;
this.layoutResource = layoutResource;
this.bitmaps = bitmaps;
}
#Override
public int getCount() {
return 0;
}
#Override
public Object getItem(int position) {
return null;
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
viewHolder holder = null;
if (view == null){
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
view = inflater.inflate(layoutResource,parent,false);
holder = new viewHolder();
holder.imageView = (ImageView)view.findViewById(R.id.single_gv_poster);
view.setTag(holder);
}else
holder = (viewHolder)view.getTag();
Bitmap bitmap = bitmaps[position];
holder.imageView.setImageBitmap(bitmap);
return view;
}
static class viewHolder{
ImageView imageView;
}
}
Anyone can help me with this issue please?
You can do this way:
Delete line from onCreate():
gv_posters.setAdapter(gridViewCustomAdapter);
Your PostExecute should looks like this:
#Override
protected void onPostExecute(Bitmap[] bitmaps){
Log.d("TESTAG","onPostExecute");
if (bitmaps != null)
mGridCustomAdapter = new GridViewCustomAdapter(mContext,R.id.single_gv_poster,bitmaps);
gv_posters.setAdapter(mGridCustomAdapter);
}
}
Hope this will help you.
So my coding got a few problems in its fragment the line "super(FragmentDishcover.this, R.layout.listviewdishcover, LV_Dishcover);" goes red line. i copied most of its files on the net and got no idea whats "getLayoutInflater()" is for. i thought it a new class or something but well tell me if it its please.
my FragmentD.class
public class FragmentD extends Fragment {
public FragmentD(){}
private List<NavItemViewlistD> LV_D = new ArrayList<NavItemViewlistD>();
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_d, container, false);
populateListViewD();
populateListView();
return rootView;
}
private void populateListViewD(){
LV_D.add(new NavItemViewlistD("Trololololol", R.drawable.kittylogotemp, R.drawable.ic_paw_white, 10));
LV_D.add(new NavItemViewlistD("booom", R.drawable.kittylogotemp, R.drawable.ic_paw_white, 20));
LV_D.add(new NavItemViewlistD("ahahahaha", R.drawable.kittylogotemp, R.drawable.ic_paw_white, 30));
}
private void populateListView(){
ArrayAdapter<NavItemViewlistD> adapter = new dListAdapter();
ListView list = (ListView) getActivity().findViewById(R.id.listViewD);
list.setAdapter(adapter);
}
private class dListAdapter extends ArrayAdapter<NavItemViewlistD>{
private dListAdapter(){
super(FragmentD.this, R.layout.listviewd, LV_D);
}
#Override
public View getView(int position, View convertView, ViewGroup parent){
View itemView = convertView;
if(itemView == null){
itemView = getLayoutInflater().inflate(R.layout.listviewd, parent, false);
}
return itemView;
}
}
}
this is my NavItemViewList.java
public class NavItemViewlist {
private String title;
private int image;
private int icon;
private int points;
public NavItemViewlist(String title, int image, int icon, int points){
super();
this.title = title;
this.image = image;
this.icon = icon;
this.points = points;
}
public String getTitle(){
return title;
}
public int getImage(){
return image;
}
public int getIcon(){
return icon;
}
public int getPoints(){
return points;
}
}
YOUR DOING IN WRONG WAY.i suggested you to not include adapter in fragment.use as separate class like this
public class SampAdapter extends ArrayAdapter<Contact> {
/**
* #param context
* #param resource
* #param objects
*/
public SampAdapter(Context context, int resource, Contact[] objects) {
super(context, resource, objects);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
return super.getView(position, convertView, parent);
}
#Override
public int getCount() {
return super.getCount();
}
}
and set yout adapter to your list view in fragment like this
SampAdapter ad=new SampAdapter(getActivity(), R.layout.your_root_view, YourArrayObject);
if its not working change to base adapter and write your own Constructor with Context,YourArrayObject and try.
I think this would help you make a java class adapter to call the items
public class DListViewAdapter extends ArrayAdapter<DListView> {
public DListViewAdapter(Context context, List<DListView> items) {
super(context, R.layout.listviewd, items);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder;
if(convertView == null) {
// inflate the GridView item layout
LayoutInflater inflater = LayoutInflater.from(getContext());
convertView = inflater.inflate(R.layout.listviewd, parent, false);
// initialize the view holder
viewHolder = new ViewHolder();
viewHolder.ivIcon = (ImageView) convertView.findViewById(R.id.icon);
viewHolder.tvTitle = (TextView) convertView.findViewById(R.id.title);
viewHolder.tvpoints = (TextView) convertView.findViewById(R.id.points);
convertView.setTag(viewHolder);
} else {
// recycle the already inflated view
viewHolder = (ViewHolder) convertView.getTag();
}
// update the item view
DListView item = getItem(position);
viewHolder.ivIcon.setImageDrawable(item.iconAdapter);
viewHolder.tvTitle.setText(item.titleAdapter);
viewHolder.tvpoints.setText(item.pointsAdapter);
return convertView;
}
private static class ViewHolder {
ImageView ivIcon;
TextView tvTitle;
TextView tvDescription;
}
}
create an listviewItem java class like this one i made for you :D
public class DListView {
public final Drawable iconAdapter;
public final String titleAdapter;
public final String pointsAdapter;
public DListView(Drawable icon, String title, String points) {
this.iconAdapter = icon;
this.titleAdapter = title;
this.pointsAdapter = points;
}
}
and create the layout
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/listViewD"
android:layout_centerHorizontal="true" />
</RelativeLayout>
and i kinda forgot what to put at the fragment be right back on it :(