My App crashes after using a Custom Adapter - java

There is no error showing in the Java file or resources file. This app builds and installs on my device but when I run it, it shows nothing and crashes. What can I do? Here is all of the code and the error message from Logcat when I open the app from my device:
Error Message:
2020-11-13 16:09:33.634 26684-26684/com.example.android.playmanplay E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.android.playmanplay, PID: 26684
java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.View.findViewById(int)' on a null object reference
at com.example.android.playmanplay.CustomAdapter.getView(CustomAdapter.java:52)
at android.widget.AbsListView.obtainView(AbsListView.java:2408)
at android.widget.ListView.makeAndAddView(ListView.java:2126)
at android.widget.ListView.fillDown(ListView.java:851)
at android.widget.ListView.fillDown(ListView.java:833)
at android.widget.ListView.fillFromTop(ListView.java:921)
at android.widget.ListView.layoutChildren(ListView.java:1900)
at android.widget.AbsListView.onLayout(AbsListView.java:2204)
at android.view.View.layout(View.java:20699)
at android.view.ViewGroup.layout(ViewGroup.java:6198)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1812)
at android.widget.LinearLayout.layoutHorizontal(LinearLayout.java:1801)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1567)
at android.view.View.layout(View.java:20699)
at android.view.ViewGroup.layout(ViewGroup.java:6198)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
at android.view.View.layout(View.java:20699)
at android.view.ViewGroup.layout(ViewGroup.java:6198)
at androidx.appcompat.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:446)
at android.view.View.layout(View.java:20699)
at android.view.ViewGroup.layout(ViewGroup.java:6198)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
at android.view.View.layout(View.java:20699)
at android.view.ViewGroup.layout(ViewGroup.java:6198)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1812)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1656)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1565)
at android.view.View.layout(View.java:20699)
at android.view.ViewGroup.layout(ViewGroup.java:6198)
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:757)
at android.view.View.layout(View.java:20699)
at android.view.ViewGroup.layout(ViewGroup.java:6198)
at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2872)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2399)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1534)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:7421)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:1092)
at android.view.Choreographer.doCallbacks(Choreographer.java:888)
at android.view.Choreographer.doFrame(Choreographer.java:819)
at android.view.Choreographer$FrameHandler.handleMessage(Choreographer.java:991)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:201)
at android.app.ActivityThread.main(ActivityThread.java:6810)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:873)
MainActivity.java:
package com.example.android.playmanplay;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private ListView listview;
private String[] countryNames;
int[] flags = {R.drawable.afghanistan,R.drawable.bangladesh,R.drawable.bhutan,R.drawable.brazil,R.drawable.canada,
R.drawable.china,R.drawable.denmark,R.drawable.egypt,R.drawable.france,R.drawable.ghana,R.drawable.haiti,
R.drawable.iran,R.drawable.iran,R.drawable.japan,R.drawable.kenya,R.drawable.norway,R.drawable.netherlands,
R.drawable.oman,R.drawable.pakistan,R.drawable.pakistan,R.drawable.russia,R.drawable.slovakia,R.drawable.srilanka,
R.drawable.tajikistan,R.drawable.uzbekistn,R.drawable.wales,R.drawable.zimbabwe};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
countryNames = getResources().getStringArray(R.array.name_list);
listview = findViewById(R.id.list_vew);
//ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,R.layout.text_setter,R.id.text_view,countryNames);
CustomAdapter adapter = new CustomAdapter(this,countryNames,flags);
listview.setAdapter(adapter);
}
}
CustomAdapter.java:
package com.example.android.playmanplay;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
public class CustomAdapter extends BaseAdapter {
int[]flags;
String[]countryNames;
Context context;
private LayoutInflater inflater;
CustomAdapter(Context context, String[] countryNames,int[] flags){
this.context = context;
this.countryNames = countryNames;
this.flags = flags;
}
#Override
public int getCount() {
return countryNames.length;
}
#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;
if(convertView == null){
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = this.inflater.inflate(R.layout.text_setter,parent,false);
}
else{
view = convertView;
}
ImageView imageView = convertView.findViewById(R.id.imageview);
TextView textView = convertView.findViewById(R.id.text_view);
imageView.setImageResource(flags[position]);
textView.setText(countryNames[position]);
return (view);
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#3D5AFE"
tools:context=".MainActivity">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:divider="#color/teal_200"
android:dividerHeight="2dp"
android:id="#+id/list_vew"
/>
</LinearLayout>
text_setter.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="match_parent"
android:orientation="vertical">
<LinearLayout
android:weightSum="4"
android:layout_width="match_parent"
android:layout_height="70dp"
android:layout_marginBottom="10dp"
android:layout_marginTop="10dp"
android:orientation="horizontal"
>
<ImageView
android:id="#+id/imageview"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:contentDescription="#string/flag_of_country"
android:src="#drawable/afghanistan" />
<LinearLayout
android:layout_weight="3"
android:layout_width="0dp"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_marginTop="5dp"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="#string/bangladesh"
android:gravity="center_horizontal"
android:textStyle="bold"
android:id="#+id/text_view" />
<TextView
android:id="#+id/textviewdown"
android:layout_marginTop="5dp"
android:gravity="center_horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/hello"
android:textAppearance="?android:attr/textAppearanceSmall"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>

You are trying to set size with an appearance:
<TextView
android:id="#+id/textviewdown"
android:layout_marginTop="5dp"
android:gravity="center_horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/hello"
android:textSize="?android:attr/textAppearanceSmall"/>
This is your view that is throwing an UnsupportedOperationException .
Remove the text size and add android:textAppearance="?android:attr/textAppearanceSmall

#Override
public int getCount() {
return countryNames.size();
}
#Override
public Object getItem(int position) {
return countryNames.get(position);
}
#Override
public long getItemId(int position) {
return countryNames.get(position).getid();
}

Related

ListView only shows the first item from the arraylist

Previously I had crash issues due to the wrong reference to the resource files. Fixed that issue and updated this thread with the logical error that I am getting.
I am new to android and currently learning custom classes and adapter. While working I am facing a problem which is the listview shows the first arraylist item only.
I have attached the codes of the required files as well.
Working Activity
package np.com.shresthakiran.tourswoniga;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ListView;
import java.util.ArrayList;
public class KhowpaActivity extends AppCompatActivity {
ListView lvHeritageList;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listview_heritage);
lvHeritageList = findViewById(R.id.lvHeritage);
ArrayList<Heritages> heritageAryList = new ArrayList<>();
heritageAryList.add(new Heritages(R.drawable.ic_launcher_background,"Ngatapol", "Taumadi"));
heritageAryList.add(new Heritages(R.drawable.ic_launcher_foreground, "Dattatreya", "Taumadi"));
heritageAryList.add(new Heritages(R.drawable.ic_launcher_foreground, "Lu dhwakha", "Lyaaku"));
heritageAryList.add(new Heritages(R.drawable.ic_launcher_foreground, "55 jhyale Durbar", "Lyaaku"));
heritageAryList.add(new Heritages(R.drawable.ic_launcher_foreground, "Taleju Bhawani", "Lyaaku"));
HeritageAdapter heritageAdapter = new HeritageAdapter(KhowpaActivity.this, R.layout.heritages_row, heritageAryList);
lvHeritageList.setAdapter(heritageAdapter);
}
}
Custom Adapter
package np.com.shresthakiran.tourswoniga;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import java.util.ArrayList;
public class HeritageAdapter extends ArrayAdapter<Heritages> {
private Context mContext;
private int mResource;
public HeritageAdapter(#NonNull Context context, int resource, #NonNull ArrayList<Heritages> objects) {
super(context, resource, objects);
this.mContext = context;
this.mResource = resource;
}
#NonNull
#Override
public View getView(int position, #Nullable View convertView, #NonNull ViewGroup parent) {
LayoutInflater layoutInflater =LayoutInflater.from(mContext);
convertView = layoutInflater.inflate(mResource, parent, false);
ImageView ivHeritageImage = convertView.findViewById(R.id.ivHeritage);
TextView tvHeritageName = convertView.findViewById(R.id.tvHeritageName);
TextView tvHeritageAddress = convertView.findViewById(R.id.tvHeritageAddress);
ivHeritageImage.setImageResource(getItem(position).getmImageResourceId());
tvHeritageName.setText(getItem(position).getmHeritageName());
tvHeritageAddress.setText(getItem(position).getmHeritageAddress());
return convertView;
}
}
Object Class
package np.com.shresthakiran.tourswoniga;
public class Heritages {
private int mImageResourceId;
private String mHeritageName;
private String mHeritageAddress;
public Heritages(int heritageImageResourceId, String heritageName, String heritageAddress) {
this.mImageResourceId = heritageImageResourceId;
this.mHeritageName = heritageName;
this.mHeritageAddress = heritageAddress;
}
public int getmImageResourceId() {
return mImageResourceId;
}
public String getmHeritageName() {
return mHeritageName;
}
public String getmHeritageAddress() {
return mHeritageAddress;
}
}
ListView XML
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="100dp"
android:minHeight="100dp">
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/lvHeritage">
</ListView>
</RelativeLayout>
List Row XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:weightSum="100"
android:layout_margin="10dp">
<ImageView
android:id="#+id/ivHeritage"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="33.33"
android:padding="2dp"
android:text="Ngatapol"
android:layout_marginTop="7dp"
android:src="#mipmap/ic_launcher"/>
<LinearLayout
android:id="#+id/llHeritageInfo"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_weight="66.66"
android:padding="8dp" >
<TextView
android:id="#+id/tvHeritageName"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:textSize="18sp"
android:textStyle="bold"
android:text="Ngatapol"
android:textAppearance="?android:textAppearanceMedium"
android:padding="2dp"/>
<TextView
android:id="#+id/tvHeritageAddress"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:textSize="18sp"
android:text="Taumadi"
android:padding="2dp"/>
</LinearLayout>
</LinearLayout>
listview shows the first item only is because you have set height in heritages_row layout to match_parent which will cover the whole screen height and for the next item, you've to scroll down even if the content of the first item is not covering the whole height.
To make each row to only cover the content its displaying, use wrap_content instead of match_parent.

ViewPager and ListView in its layers

I apologize in advance for my terrible English.
My app should have 6 pages, and each of them will have a ListView with its own text.
The list consists of two TextView, so I made my own adapter for it:
BoxAdapter.java
import java.util.ArrayList;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
public class BoxAdapter extends BaseAdapter {
Context ctx;
LayoutInflater lInflater;
ArrayList<Kek> objects;
BoxAdapter(Context context, ArrayList<Kek> products) {
ctx = context;
objects = products;
lInflater = (LayoutInflater) ctx
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
// кол-во элементов
#Override
public int getCount() {
return objects.size();
}
#Override
public Object getItem(int position) {
return objects.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
if (view == null) {
view = lInflater.inflate(R.layout.item, parent, false);
}
Kek p = getProduct(position);
((TextView) view.findViewById(R.id.textView1)).setText(p.pozic);
((TextView) view.findViewById(R.id.textView1_2)).setText(p.name);
return view;
}
Kek getProduct(int position) {
return ((Kek) getItem(position));
}
}
And auxiliary class for him:
Kek.java
public class Kek {
String name;
String pozic;
Kek(String _pozic,String _describe) {
pozic = _pozic;
name = _describe;
}
}
Next for * * ViewPager* * I also created my adapter:
SlideAdapter.java
package com.github.helpme;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentStatePagerAdapter;
import java.util.List;
public class SlideAdapter extends FragmentStatePagerAdapter {
private List<Fragment> fragmentList;
public SlideAdapter (FragmentManager fragmentManager, List<Fragment> fragmentList) {
super(fragmentManager);
this.fragmentList = fragmentList;
}
#Override
public Fragment getItem(int position) {
return fragmentList.get(position);
}
#Override
public int getCount() {
return fragmentList.size();
}
}
So far I have created 2 test sheets:
Activity_main_1.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<androidx.appcompat.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="#color/colorPrimary"
android:minHeight="?attr/actionBarSize"
android:theme="?attr/actionBarTheme"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/textView222"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginLeft="16dp"
android:layout_marginTop="13dp"
android:layout_marginBottom="15dp"
android:text="НУ РАБОТАЙ ЖЕ ТЫ"
android:textColor="#FFFFFF"
android:textSize="20sp"
android:textStyle="bold"
app:layout_constraintBottom_toTopOf="#+id/linearLayout"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.004" />
<LinearLayout
android:id="#+id/linearLayout"
android:layout_width="0dp"
android:layout_height="0dp"
android:orientation="vertical"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent">
<ListView
android:id="#+id/lvMain_1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:divider="#null"></ListView>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
and class
Layout_1.java
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
public class Layout_1 extends Fragment {
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
ViewGroup viewGroup = (ViewGroup) inflater.inflate(R.layout.activity_main_1, container,false);
return viewGroup;
}
}
On the same analogy created 2nd page:
Activity_main_2.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<androidx.appcompat.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="#color/colorPrimary"
android:minHeight="?attr/actionBarSize"
android:theme="?attr/actionBarTheme"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/textView222"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginLeft="16dp"
android:layout_marginTop="13dp"
android:layout_marginBottom="15dp"
android:text="ЧУХ_ЧУХ"
android:textColor="#FFFFFF"
android:textSize="20sp"
android:textStyle="bold"
app:layout_constraintBottom_toTopOf="#+id/linearLayout"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.004" />
<LinearLayout
android:id="#+id/linearLayout"
android:layout_width="0dp"
android:layout_height="0dp"
android:orientation="vertical"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent">
<ListView
android:id="#+id/lvMain_1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:divider="#null"></ListView>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
and class
Layout_2.java
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
public class Layout_2 extends Fragment {
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
ViewGroup viewGroup = (ViewGroup) inflater.inflate(R.layout.activity_main_2, container,false);
return viewGroup;
}
}
And finally the main activity:
MainActivity.java
package com.github.helpme;
import android.content.Context;
import android.os.Bundle;
import android.widget.ListView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private ViewPager viewPager;
private SlideAdapter slideAdapter;
private ListView lvMain;
private BoxAdapter boxAdapter;
ArrayList<Kek> products = new ArrayList<Kek>();
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
List<Fragment> list = new ArrayList<>();
list.add(new Layout_1());
list.add(new Layout_2());
viewPager = findViewById(R.id.rader);
slideAdapter = new SlideAdapter(getSupportFragmentManager(),list);
viewPager.setAdapter(slideAdapter);
fillData();
boxAdapter = new BoxAdapter(this, products);
lvMain = (ListView) findViewById(R.id.lvMain_1);
lvMain.setAdapter(boxAdapter);
}
// генерируем данные для адаптера
void fillData() {
for (int i = 1; i <= 20; i++) {
products.add(new Kek (i + ")", "Product " + i));
}
}
}
If you now run the application it will not start, and in the logs you will see:
2019-08-18 09:26:53.879 7138-7138/com.github.helpme E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.github.helpme, PID: 7138
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.github.helpme/com.github.helpme.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3260)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3396)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2009)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7319)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:934)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference
at com.github.helpme.MainActivity.onCreate(MainActivity.java:45)
at android.app.Activity.performCreate(Activity.java:7783)
at android.app.Activity.performCreate(Activity.java:7772)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1299)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3235)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3396) 
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83) 
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) 
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2009) 
at android.os.Handler.dispatchMessage(Handler.java:107) 
at android.os.Looper.loop(Looper.java:214) 
at android.app.ActivityThread.main(ActivityThread.java:7319) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:934) 
When I saw this, I tried to move the loading of the list to the layer classes * * ViewPager**
private ListView lvMain;
private BoxAdapter boxAdapter;
ArrayList<Kek> products = new ArrayList<Kek>();
fillData();
boxAdapter = new BoxAdapter(this, products);
lvMain = (ListView) findViewById(R.id.lvMain_1);
lvMain.setAdapter(boxAdapter);
void fillData() {
for (int i = 1; i <= 20; i++) {
products.add(new Kek (i + ")", "Product " + i));
}
}
But the compiler directly in the code gave an error:
BoxAdapter (android.content.Context, ArrayList) in
BoxAdapter cannot be applied to (com.github.helpme.Layout_1,
ArrayList)
So we got to the essence of this post: How to make Pager sheets with your list inside and how to fill it all.
UDP:
new error:
08-11 15:21:02.956 3653-3653/? E/ActivityThread: Service
com.android.email.service.EmailBroadcastProcessorService has leaked
ServiceConnection com.android.emailcommon.service.am#92c8ab8 that was
originally bound here android.app.ServiceConnectionLeaked: Service
com.android.email.service.EmailBroadcastProcessorService has leaked
ServiceConnection com.android.emailcommon.service.am#92c8ab8 that was
originally bound here at
android.app.LoadedApk$ServiceDispatcher.(LoadedApk.java:1092) at
android.app.LoadedApk.getServiceDispatcher(LoadedApk.java:986) at
android.app.ContextImpl.bindServiceCommon(ContextImpl.java:1303) at
android.app.ContextImpl.bindService(ContextImpl.java:1286) at
android.content.ContextWrapper.bindService(ContextWrapper.java:604) at
com.android.emailcommon.service.ak.a(SourceFile:181) at
com.android.emailcommon.service.ak.e(SourceFile:224) at
com.android.email.service.n.c(SourceFile:177) at
com.android.email.provider.b.a(SourceFile:198) at
com.android.email.provider.b.a(SourceFile:142) at
com.android.email.service.EmailBroadcastProcessorService.c(SourceFile:349)
at
com.android.email.service.EmailBroadcastProcessorService.onHandleIntent(SourceFile:1334)
at
android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:66)
at android.os.Handler.dispatchMessage(Handler.java:102) at
android.os.Looper.loop(Looper.java:148) at
android.os.HandlerThread.run(HandlerThread.java:61)

Why my GridView cuts off at The End?

In the above picture, the item at the bottom is cut off like a red arrow.
Perhaps the size of the Gridview is the entire screen, and NavigationBottomView seems to cover part of the GridView. I want to specify the height of the GridView from the top of the screen to the top of the BottomNavigationView.
Below is my xml code.
1> Home screen xml code with NavigationBottomView(home.xml)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".home">
<FrameLayout
android:id="#+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="520dp"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="32dp"
android:layout_gravity="left|center_vertical"
android:text="All Of "
android:textColor="#color/colorPrimaryDark"
android:textSize="60sp"
android:textStyle="bold"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="196dp"
android:layout_centerInParent="true"
android:layout_gravity="left|center_vertical"
android:text="G"
android:textColor="#8e1414"
android:textSize="60dp"
android:textStyle="bold"/>
<TextView
android:id="#+id/message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="240dp"
android:layout_gravity="left|center_vertical"
android:text="IST"
android:textColor="#color/colorPrimaryDark"
android:textSize="60dp"
android:textStyle="bold"/>
</FrameLayout>
<android.support.design.widget.BottomNavigationView
android:id="#+id/navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="?android:attr/windowBackground"
app:menu="#menu/navigation">
</android.support.design.widget.BottomNavigationView>
</RelativeLayout>
2> Xml code using GridView(site_list.xml)
<?xml version="1.0" encoding="utf-8"?>
<GridView 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"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="#FFFFFF"
tools:context=".home"
android:id="#+id/gridView"
android:columnWidth="96dp"
android:numColumns="2"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:stretchMode="columnWidth"
android:gravity="center">
</GridView>
3> The xml code that designed the items in the GridView(list_item.xml)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_gravity="center"
>
<ImageView
android:layout_width="144dp"
android:layout_height="144dp"
android:id="#+id/image"
tools:src="#mipmap/ic_launcher"
android:layout_gravity="center_vertical"
/>
<TextView
android:layout_width="144dp"
android:layout_height="wrap_content"
android:id="#+id/name"
tools:text="Gistsite"
android:textAppearance="?android:textAppearanceMedium"
android:textStyle="bold"
android:gravity="center_horizontal"
android:textSize="16sp"
/>
</LinearLayout>
4> The class that created the data type named Site(Site.java)
public class Site {
private String msite_name;
private String msite_url;
private int msite_imagesource;
public Site(String name, String url, int imagesource){
msite_name = name;
msite_url = url;
msite_imagesource = imagesource;
}
public String getMsite_name() {
return this.msite_name;
}
public String getMsite_url(){
return this.msite_url;
}
public int getMsite_imagesource(){
return this.msite_imagesource;
}
}
5> SideAdapter code created by customizing an ArrayAdapter(SiteAdapter.java)
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import java.util.ArrayList;
public class SiteAdapter extends ArrayAdapter<Site> {
private Site currentSite;
public SiteAdapter(Activity context, ArrayList<Site> sites){
super(context,0,sites);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View listItemView = convertView;
if(listItemView == null){
listItemView =
LayoutInflater.from(getContext()).inflate(R.layout.list_item,parent,false);
}
currentSite = getItem(position);
ImageView imageView = (ImageView) listItemView.findViewById(R.id.image);
imageView.setImageResource(currentSite.getMsite_imagesource());
TextView name = (TextView) listItemView.findViewById(R.id.name);
name.setText(currentSite.getMsite_name());
return listItemView;
}
}
6> A fragment code indicating the capture screen attached above(OrganizationFragment.java)
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;
import java.util.ArrayList;
public class OrganizationFragment extends Fragment {
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState){
View organizationView = inflater.inflate(R.layout.site_list, null);
final ArrayList<Site> organizations = new ArrayList<Site>();
organizations.add(new Site("Gist 총학생회", "https://www.facebook.com/gistunion/",R.drawable.gistunion));
organizations.add(new Site("Gist 동아리연합회", "https://www.facebook.com/gistclubunite/",R.drawable.clubnight));
organizations.add(new Site("Gist 하우스", "https://www.facebook.com/GISTcollegeHOUSE/",R.drawable.gisthouse));
organizations.add(new Site("Gist 문화행사위원회", "https://www.facebook.com/Moonhangwe/",R.drawable.moonhangwe));
organizations.add(new Site("Gist 신문", "https://www.facebook.com/pg/GistSinmoon/posts/",R.drawable.gistnews));
organizations.add(new Site("Gist 홍보대사", "http://blog.naver.com/PostList.nhn?blogId=gist1993&from=postList&categoryNo=28",R.drawable.gionnare));
SiteAdapter organizationAdapter = new SiteAdapter(getActivity(), organizations);
GridView gridViewO =
(GridView)organizationView.findViewById(R.id.gridView);
gridViewO.setAdapter(organizationAdapter);
gridViewO.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
Site site = organizations.get(position);
openWebPage(site.getMsite_url());
}
});
return organizationView;
}
public void openWebPage(String url){
Uri webpage = Uri.parse(url);
Intent siteMove = new Intent(Intent.ACTION_VIEW, webpage);
if(siteMove.resolveActivity(getActivity().getPackageManager()) != null)
startActivity(siteMove);
}
}
7> Java code representing the main home screen(home.java)
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.v7.app.AppCompatActivity;
import android.support.v4.app.Fragment;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.MenuItem;
import android.widget.TextView;
public class home extends AppCompatActivity {
private TextView mTextMessage;
private BottomNavigationView.OnNavigationItemSelectedListener
mOnNavigationItemSelectedListener
= new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
Fragment selectedFragment = null;
switch (item.getItemId()) {
case R.id.navigation_officials:
selectedFragment = new OfficialFragment();
break;
case R.id.navigation_organizations:
selectedFragment = new OrganizationFragment();
break;
case R.id.navigation_circles:
selectedFragment = new CircleFragment();
break;
case R.id.navigation_projects:
selectedFragment = new ProjectFragment();
break;
}
return loadFragment(selectedFragment);
}
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.home);
loadFragment(new HomeFragment());
BottomNavigationView navigation = (BottomNavigationView)
findViewById(R.id.navigation);
navigation.setOnNavigationItemSelectedListener
(mOnNavigationItemSelectedListener);
}
private boolean loadFragment(Fragment fragment) {
//switching fragment
if (fragment != null) {
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.fragment_container, fragment).commit();
return true;
}
return false;
}
}
I would be grateful if you could read the questions I have made and give them some advice.
Its happening because you have set the FrameLayout height 520dp in xml
set the frameLayout height match and also use android:layout_above="#+id/navigation"
try this code
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="#+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#+id/navigation"
android:orientation="horizontal">
<OtherViews.../>
</FrameLayout>
<android.support.design.widget.BottomNavigationView
android:id="#+id/navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="?android:attr/windowBackground"
app:menu="#menu/navigation">
</android.support.design.widget.BottomNavigationView>
</RelativeLayout>

RecyclerView with ToolBar

I'm new to material design and every time i try to use RecyclerView every thing gose wrong..
can any one know what is the problem ?
Main Activity class
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends ActionBarActivity {
Toolbar toolbar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar= (Toolbar) findViewById(R.id.toool);
setSupportActionBar(toolbar);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
Main Activity layout
<RelativeLayout 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"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<include
android:id="#+id/toool"
layout="#layout/app_tool" />
<fragment
android:id="#+id/fragment"
android:name="com.example.hothyfa.recyclerandmore.Recycler"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/toool"
android:layout_centerHorizontal="true"
tools:layout="#layout/rere" />
</RelativeLayout>
Recycler fragment
<?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" >
<android.support.v7.widget.RecyclerView
android:id="#+id/recye"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</android.support.v7.widget.RecyclerView>
</RelativeLayout>
Recycler class
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import java.util.ArrayList;
import java.util.List;
public class Recycler extends Fragment {
private RecyclerView recyclerView;
private MyRecyeAdapter adapter;
private View view;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
view = inflater.inflate(R.layout.rere, container, false);
recyclerView = (RecyclerView) getActivity().findViewById(R.id.recye);
adapter = new MyRecyeAdapter(getActivity(), getData());
recyclerView.setAdapter(adapter);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
return view;
}
public static List<info> getData() {
List<info> data = new ArrayList<>();
int photos[] = { R.drawable.images, R.drawable.dreamsmal, R.drawable.images, R.drawable.dreamsmal };
String users[] = { "Hothyfa", "Marwan", "Jamal", "Alaa" };
for (int i = 0; i < photos.length && i < users.length; i++) {
info current = new info();
current.iconId = photos[i];
current.Names = users[i];
data.add(current);
}
return data;
}
}
RecyclerView Adapter class
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.Collections;
import java.util.List;
public class MyRecyeAdapter extends RecyclerView.Adapter<MyRecyeAdapter.myholder> {
List<info> data = Collections.emptyList();
private LayoutInflater inflater;
public MyRecyeAdapter(Context context, List<info> data) {
inflater = LayoutInflater.from(context);
this.data = data;
}
#Override
public myholder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = inflater.inflate(R.layout.custom, parent, false);
myholder holder = new myholder(view);
return holder;
}
#Override
public void onBindViewHolder(myholder holder, int position) {
info current = data.get(position);
holder.imageProfile.setImageResource(current.iconId);
holder.UsersName.setText(current.Names);
}
#Override
public int getItemCount() {
return data.size();
}
public class myholder extends RecyclerView.ViewHolder {
ImageView imageProfile;
TextView UsersName;
public myholder(View itemView) {
super(itemView);
imageProfile = (ImageView) itemView.findViewById(R.id.imageView);
UsersName = (TextView) itemView.findViewById(R.id.textView);
}
}
}
custom row layout (for the RecyclerView)
<?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="match_parent"
android:orientation="horizontal" >
<ImageView
android:id="#+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:padding="8dp"
android:src="#drawable/images" />
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:padding="8dp"
android:text="Large Text" />
</LinearLayout>
and the data class for the Recycler
public class info {
int iconId;
String Names;
}
Toolbar layout
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/primarycolor" >
</android.support.v7.widget.Toolbar>
Note: I have put this in the build.gradle
compile 'com.android.support:recyclerview-v7:22.2.1'
and rebuild the app.
Error:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.hothyfa.recyclerandmore/com.example.hothyfa.recyclerandmore.MainActivity}: android.view.InflateException: Binary XML file line #19: Error inflating class fragment
at
app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)
at
\\\\\\\android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5257)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Caused by: android.view.InflateException: Binary XML file line #19: Error inflating class fragment
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:763)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:249)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:106)
at com.example.hothyfa.recyclerandmore.MainActivity.onCreate(MainActivity.java:19)
at android.app.Activity.performCreate(Activity.java:5990)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
    
Ok, I found the issue, in your onCreateView of Recycler
recyclerView= (RecyclerView) getActivity().findViewById(R.id.recye);
change it to:
recyclerView= (RecyclerView) view.findViewById(R.id.recye);
try to edit this
<fragment
android:id="#+id/fragment"
android:name="com.example.hothyfa.recyclerandmore.Recycler"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/toool"
android:layout_centerHorizontal="true"
/>
<RelativeLayout 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"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:minHeight="?attr/actionBarSize"
android:background="#2196F3"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</android.support.v7.widget.Toolbar>
<fragment
android:id="#+id/fragment"
android:name="com.example.hothyfa.recyclerandmore.Recycler"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/toool"
android:layout_centerHorizontal="true"
tools:layout="#layout/rere" />
</RelativeLayout>
Try This:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="#+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="?attr/actionBarSize"
android:clipToPadding="false"/>
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"/>
</FrameLayout>
You can see these two links How to hide/show toolbar in response to scrolling on Android and Recyclerview with Cards example in Android with AppCompat (V7)
Do lemme knw in any concern

listview does not update with notifydatasetchanged() call

My Android listview does not update with notifydatasetchanged() call.
The Main Code Activity:
package com.example.jokesbook;
import java.util.ArrayList;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
public class MainActivity extends Activity {
public final static String EXTRA_MESSAGE = "com.example.jokesbook.MESSAGE";
CustomAdapter Adapter;
ListView lv;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
JokeDB.jokesList = new ArrayList<Joke>();
JokeDB.jokesList.add(new Joke("DDD"));
lv = (ListView)findViewById(android.R.id.list);
Adapter= new CustomAdapter(MainActivity.this, R.layout.joke_list_item, JokeDB.jokesList);
lv.setAdapter(Adapter);
}//onCreate
#Override
public void onResume() {
super.onResume(); // Always call the superclass method first
Log.d("jokesbook", "onResume ");
Adapter.notifyDataSetChanged();
}
class CustomAdapter extends ArrayAdapter<Joke>{
Context context;
int layoutResourceId;
ArrayList<Joke> data = null;
private LayoutInflater mInflater;
public CustomAdapter(Context customAdapter, int layoutResourceId, ArrayList<Joke> data) {
super(customAdapter, layoutResourceId, data);
Log.d("jokesbook", "CustomAdapter ");
this.layoutResourceId = layoutResourceId;
this.context = customAdapter;
this.data = data;
this.mInflater = LayoutInflater.from(customAdapter);
}
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if (convertView == null) {
//item_list
convertView = mInflater.inflate(R.layout.joke_list_item, null);
holder = new ViewHolder();
//fill the views
holder.joke = (TextView) convertView.findViewById(R.id.ListTextView1);
convertView.setTag(holder);
}
else {
// Get the ViewHolder back to get fast access to the TextView
// and the ImageView.
holder = (ViewHolder) convertView.getTag();//
}
holder.joke.setText(data.get(position).jokeStr);
return convertView;
}
class ViewHolder {
TextView joke;
}
}
/** Called when the user clicks the Add a new joke button */
public void goNewJoke(View view) {
Intent intent = new Intent(this, New_joke.class);
startActivity(intent);
}
}
Its xml:
<LinearLayout 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"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:orientation="vertical"
tools:context=".MainActivity" >
<Button
android:id="#+id/ButtonAboveList"
android:layout_width="500dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:onClick="goNewJoke"
android:text="#string/Add_a_new_joke"/>
<ListView
android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
When adding content to JokeDB.jokesList in another activity that its code is:
package com.example.jokesbook;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
public class New_joke extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_new_joke);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.new_joke, menu);
return true;
}
public void addJoke(View view){
EditText editTextJoke= (EditText)findViewById(R.id.edit_text_jokeToAdd);
JokeDB.jokesList.add(new Joke(editTextJoke.getText().toString()));
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
}
}
and its XML is:
<LinearLayout 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"
android:background="#color/green"
android:orientation="vertical"
android:paddingLeft="3dp"
android:paddingRight="3dp"
android:paddingTop="3dp"
tools:context=".New_joke" >
<EditText
android:id="#+id/edit_text_jokeToAdd"
android:layout_width="fill_parent"
android:layout_height="150dp"
android:layout_marginBottom="40dp"
android:background="#color/white"
android:hint="#string/edit_message" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:orientation="horizontal"
tools:context=".New_joke" >
<TextView
android:id="#+id/AuthorText"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:gravity="left"
android:text="#string/Author"
android:textColor="#android:color/white"
android:textSize="25sp" />
<EditText
android:id="#+id/edit_text_Author"
android:layout_width="fill_parent"
android:layout_height="40dp"
android:background="#color/white"
android:hint="#string/Only_letters_allowed"
android:inputType="textPersonName" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="60dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:orientation="horizontal"
tools:context=".New_joke" >
<TextView
android:id="#+id/DateText"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:gravity="left"
android:text="#string/Date"
android:textColor="#android:color/white"
android:textSize="25sp" />
<EditText
android:id="#+id/edit_text_Date"
android:layout_width="fill_parent"
android:layout_height="40dp"
android:background="#color/white"
android:hint="#string/Only_digits_allowed"
android:inputType="number" />
</LinearLayout>
<Button
android:id="#+id/ButtonAboveList"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:onClick="addJoke"
android:text="#string/Add" />
</LinearLayout>
I cannot see the updated list in the main activity eveb though in onResume I used notifydatasetchanged() function.
Try This....
#Override
public void onResume() {
super.onResume(); // Always call the superclass method first
Log.d("jokesbook", "onResume ");
notifyDataSetChanged();
}

Categories

Resources