I am currently trying to link up my xml textviews to my Profile Fragment. However, I get a null object reference error on the line
nameAge = getView().findViewById(R.id.nameAge);
Here is the Fragment.java
package com.example.helloworld2;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.view.View;
import android.os.Bundle;
import android.widget.TextView;
import androidx.fragment.app.Fragment;
public class ProfileFragment extends Fragment{
private TextView nameAge;
private TextView occupation;
private TextView description;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
View view = inflater.inflate(R.layout.fragment_profile, null);
nameAge = getView().findViewById(R.id.nameAge);
occupation = getView().findViewById(R.id.occupation);
description = getView().findViewById(R.id.description);
return view;
}
}
Here is the xml file
<?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">
<ImageView
android:id="#+id/profile_image"
android:layout_width="300dp"
android:layout_height="350dp"
android:layout_gravity="center"
android:src="#drawable/nero" />
<TextView
android:id="#+id/nameAge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:textSize="50dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:id="#+id/occupation"
android:textSize="40dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:id="#+id/description"
android:paddingTop="40dp"
android:textSize="30dp"/>
</LinearLayout>
Thank you
do this in onViewCreated() not in onCreateView()
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_profile, null);
return view;;
}
#Override
public void onViewCreated(#NonNull View view, #Nullable Bundle savedInstanceState) {
nameAge =getActivity().findViewById(R.id.nameAge);
occupation = getActivity().findViewById(R.id.occupation);
description =getActivity().findViewById(R.id.description);
}
Related
I have res/layout/main_menu_layout.xml file:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
>
<sidekick.cpp.CMainMenuButton
android:id="#+id/manualMainMenuButton"
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_marginTop="10dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:textSize="18sp"
android:text="#string/main_menu_button_manual_text" />
<sidekick.cpp.CMainMenuButton
android:id="#+id/editorMainMenuButton"
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_marginTop="10dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:textSize="18sp"
android:text="#string/main_menu_button_editor_text" />
<sidekick.cpp.CMainMenuButton
android:id="#+id/exitMainMenuButton"
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_marginTop="10dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:textSize="18sp"
android:text="#string/main_menu_button_exit_text" />
</LinearLayout>
This layout is used in the class below:
package sidekick.cpp;
import android.widget.*;
import android.content.*;
import android.view.*;
public class CMainMenuLayout extends LinearLayout {
public CMainMenuLayout(Context context) {
super(context);
this.setOrientation(VERTICAL);
View view = LayoutInflater.from(getContext()).inflate(
R.layout.main_menu_layout, null);
this.addView(view);
} }
Now I want to use the layout in a fragment. If I use the layout's ID (R.layout.main_menu_layout) in onCreateView() then the below code seems to work:
public class CMainMenuFragment extends Fragment implements View.OnClickListener {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.main_menu_layout, container, false);
}
...
}
But I need to use the CMainLayout class, not R.layout.main_menu_layout. The code below seems to work too but is it correct?
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
CMainMenuLayout layout = new CMainMenuLayout(this.getContext());
return layout;
}
First, you can inflate xml layout in CMainMenuLayout directly
public CMainMenuLayout(Context context) {
super(context);
this.setOrientation(VERTICAL);
LayoutInflater.from(getContext()).inflate(R.layout.main_menu_layout, this, true);
}
then use CMainMenuLayout class in xml for CMainMenuFragment. For example
res/layout/main_fragment_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<sidekick.cpp.CMainMenuLayout
android:id="#+id/mainMenuLayout"
android:layout_height="wrap_content"
android:layout_width="wrap_content" />
</LinearLayout>
and in CMainMenuFragment
public class CMainMenuFragment extends Fragment implements View.OnClickListener {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.main_menu_fragment, container, false);
}
...
}
I want to make Screen in android with multiple layouts. Fox example On the top of the screen there is the header with image slider (this part is done) below that there will be a list view and below that there will be grid view.
Activitymain.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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.root.multipleview.MainActivity">
<!--Header image-->
<android.support.v4.view.ViewPager
android:id="#+id/viewPager"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:autoStart="true"></android.support.v4.view.ViewPager>
<!--ListView Below header -->
<ListView
android:id="#+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:height="10dp" />
</RelativeLayout>
CustomLayout.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:layout_alignParentTop="true"
android:background="#f1f3f7"
android:orientation="vertical">
<!--Header image with slider-->
<ImageView
android:id="#+id/headerimg"
android:layout_width="fill_parent"
android:layout_height="180dp"
android:scaleType="centerCrop"
android:layout_marginTop="4dp"
android:layout_marginLeft="4dp"
android:layout_marginRight="4dp"
android:layout_marginBottom="4dp"
android:src="#mipmap/ic_launcher" />
<!--ListView Below Header-->
<RelativeLayout
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="1"
android:layout_marginTop="20dp"
android:paddingLeft="2dp">
<ImageView
android:id="#+id/imageView"
android:layout_width="100dp"
android:layout_height="90dp"
android:layout_weight="0.05"
app:srcCompat="#mipmap/ic_launcher"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:gravity="center"/>
<TextView
android:id="#+id/textName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginLeft="40dp"
android:layout_marginStart="40dp"
android:layout_marginTop="11dp"
android:layout_toEndOf="#+id/imageView"
android:layout_toRightOf="#+id/imageView"
android:text="TextView" />
<TextView
android:id="#+id/textdesc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
android:layout_marginBottom="20dp"
android:layout_alignBottom="#+id/imageView"
android:layout_alignLeft="#+id/textName"
android:layout_alignStart="#+id/textName"/>
</RelativeLayout>
</LinearLayout>
ListView.java
package com.example.root.multipleview;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
/**
* Created by root on 11/18/2017.
*/
public class ListView extends AppCompatActivity {
int[] Images = {R.drawable.salad,R.drawable.salami,R.drawable.flour,R.drawable.salt,
R.drawable.sandwich,R.drawable.sausage,R.drawable.seeds,R.drawable.cheese,R.drawable.shrimp,R.drawable.cupcake,R.drawable.cup,R.drawable.spices,R.drawable.cabbage
,R.drawable.spoon,R.drawable.apple,R.drawable.asparagus,R.drawable.cupcake,R.drawable.fish,R.drawable.corn,R.drawable.cupcake,R.drawable.spatula,R.drawable.olives
,R.drawable.garlic,R.drawable.taco,R.drawable.broccoli,R.drawable.cabbage};
String[] Names = {"a","B","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"};
String[] Description = {"VVV","DDD","ddd","dddsa","ddsdsds","zsxx","wwwwq","ddwqK","EQWK","FgggFFF","FFFkklF","FghhFFF","FFhhFF","FFhhFF",
"FFmmmFF","FgggFFF","FFFFbbb","FFFFfeee","gfffFFFF","FFFFfff","FFFgffF","FFFFfgffg","FFFfgfgfF","FFFgffF","FFFgfggdF","FFFFdssa"};
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
android.widget.ListView listView= (android.widget.ListView) findViewById(R.id.listView);
CustomAdapter customAdapter = new CustomAdapter();
listView.setAdapter(customAdapter);
}
public class CustomAdapter extends BaseAdapter {
#Override
public int getCount() {
return Images.length;
}
#Override
public Object getItem(int i) {
return null;
}
#Override
public long getItemId(int i) {
return 0;
}
#Override
public View getView(int i, View view, ViewGroup viewGroup) {
view = getLayoutInflater().inflate(R.layout.custom_layout,null);
ImageView imageView = (ImageView)view.findViewById(R.id.imageView);
TextView textView_N = (TextView)view.findViewById(R.id.textName);
TextView textView_D = (TextView)view.findViewById(R.id.textdesc);
imageView.setImageResource(Images[i]);
textView_N.setText(Names[i]);
textView_D.setText(Description[i]);
return view;
}
}
}
MainActivity.java
package com.example.root.multipleview;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
ViewPager viewPager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
viewPager = (ViewPager)findViewById(R.id.viewPager);
ViewPageAdapter viewPageAdapter = new ViewPageAdapter( this);
viewPager.setAdapter(viewPageAdapter);
}
}
ViewPageAdapter
package com.example.root.multipleview;
import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
/**
* Created by root on 11/18/2017.
*/
public class ViewPageAdapter extends PagerAdapter{
private Context context;
private LayoutInflater layoutInflater;
private Integer[] images = {R.drawable.b,R.drawable.c,R.drawable.e,R.drawable.j,R.drawable.r,R.drawable.x,R.drawable.y};
public ViewPageAdapter(Context context){this.context = context;}
#Override
public int getCount() {
return images.length;
}
#Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
#Override
public Object instantiateItem(ViewGroup container, int position) {
layoutInflater =(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = layoutInflater.inflate((R.layout.custom_layout), null);
ImageView imageView = (ImageView) view.findViewById(R.id.headerimg);
imageView.setImageResource((images[position]));
ViewPager vp = (ViewPager) container;
vp.addView(view, 0);
return view;
}
#Override
public void destroyItem(ViewGroup container, int position, Object object) {
ViewPager vp = (ViewPager) container;
View view = (View) object;
vp.removeView(view);
}
}
When I check this all layout separately it works but after adding in one layout it's not working. output screenshot is added
enter image description here
How to make multiple layout for one screen?
my approach for this goal is using Fragments
how?
i do make a single main_layout and set a FrameLayout in order to put fragments with different layouts.so i have a screen with different layouts.
this is my best answer for your question .
I have an application where I have a part of the screen for displaying information about a student and the remaining part with tablayout and viewpager. My problem is tablayout shows up in the design editor but viewpager doesn't. In-spite of the code written nothing is showing up. Tell me where I am getting things wrong?
CAMarksFragment.java
package com.learn.app;
import android.content.Context;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class CAMarksFragment extends Fragment {
TabLayout tabLayout;
ViewPager viewPager;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_camarks, container, false);
}
#Override
public void onViewCreated(View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
//you can set the title for your toolbar here for different fragments different titles
getActivity().setTitle("CA Marks");
viewPager=(ViewPager)getActivity().findViewById(R.id.viewPager);
viewPager.setAdapter(new CustomAdapter(getActivity().getSupportFragmentManager(),getActivity().getApplicationContext()));
tabLayout=(TabLayout)getActivity().findViewById(R.id.tabLayout);
tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
#Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabReselected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
}
});
}
#Override
public void onActivityCreated(#Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
SharedPreferences pref = getActivity().getApplicationContext().getSharedPreferences("AppStatus", 0); // 0 - for private mode
SharedPreferences.Editor editor = pref.edit();
editor.putBoolean("Home", false);
editor.commit();
}
private class CustomAdapter extends FragmentPagerAdapter {
private String[] fragments={"Theory","Practical"};
public CustomAdapter(FragmentManager supportFragmentManager, Context applicationContext) {
super(supportFragmentManager);
}
#Override
public Fragment getItem(int position) {
switch (position){
case 0:
return new Theory();
case 1:
return new Practical();
default:
return null;
}
}
#Override
public int getCount() {
return fragments.length;
}
#Override
public CharSequence getPageTitle(int position) {
return fragments[position];
}
}
}
fragments_camarks.xml
<FrameLayout 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"
tools:context="com.learn.app.TestTimeTableFragment">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/tablebackgrounds"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="100"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="#+id/ca_all"
android:background="#drawable/allbackgrounds"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<ImageButton
android:id="#+id/user_profile_photo"
android:layout_width="120dp"
android:layout_height="120dp"
android:layout_marginTop="10dp"
android:layout_centerHorizontal="true"
android:background="#drawable/profile_circular_border_imageview"
android:padding="20dp"
android:scaleType="centerCrop"
android:src="#drawable/defaultpic" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20dp"
android:text="Hewitt"
android:textStyle="bold"
android:textColor="#ffffff"
android:id="#+id/camarks_name"
android:padding="10dp"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="14dp"
android:id="#+id/camarks_dept"
android:text="Master of Computer Application"
android:textColor="#ffffff"
android:padding="10dp"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="14dp"
android:id="#+id/camarks_rollno"
android:text="15mx13"
android:textColor="#ffffff"
android:padding="10dp"
/>
</LinearLayout>
</LinearLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20dp"
android:textColor="#ffffff"
android:textStyle="bold"
android:background="#80000000"
android:id="#+id/ca_title"
android:text="CA Marks"
android:gravity="center"
android:padding="10dp"
/>
</LinearLayout>
<android.support.design.widget.TabLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/tabLayout"
android:layout_below="#id/ca_all"
android:background="#color/colorPrimaryDark"
app:tabGravity="fill"
app:tabMode="fixed"
>
</android.support.design.widget.TabLayout>
<android.support.v4.view.ViewPager
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/viewPager"
android:layout_below="#id/tabLayout"
android:layout_centerHorizontal="true"
>
</android.support.v4.view.ViewPager>
</RelativeLayout>
</ScrollView>
</FrameLayout>
Practical.java
package com.learn.app;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class Practical extends Fragment {
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_layout1, container, false);
}
}
Theory.java
package com.learn.app;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class Theory extends Fragment {
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_layout, container, false);
}
}
fragment_layout contains linearlayout with a textview in it.
In the emulator viewpager doesn't showup and tablayout remain untouchable.What I am getting wrong here. Help me to proceed further. Thanks in advance.
Emulator Output
Many things are wrong in the code.
in xml : add android:layout_height="match_parent" for viewpager
<android.support.v4.view.ViewPager
android:id="#+id/viewPager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#id/tabLayout"
android:layout_centerHorizontal="true">
in code:
change your code as: you need to use fragment's view to get the viewpager instance. Also your code missing some basic functionalities for viewpager with tablayout
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_camarks, container, false);
viewPager=(ViewPager)view.findViewById(R.id.viewPager);
tabLayout=(TabLayout)view.findViewById(R.id.tabLayout);
tabLayout.addTab(tabLayout.newTab().setText("Theory"));
tabLayout.addTab(tabLayout.newTab().setText("Practical"));
tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
viewPager.setAdapter(new CustomAdapter(getActivity().getSupportFragmentManager(),getActivity().getApplicationContext()));
tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
#Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(TabLayout.Tab tab) {
}
#Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
tabLayout.setupWithViewPager(viewPager);
return view;
}
#Override
public void onViewCreated(View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
//you can set the title for your toolbar here for different fragments different titles
getActivity().setTitle("CA Marks");
}
It could be because you are using views from androidx
Well it does have ViewPager along with ViewPager2
Add the following line in build.gradle(:app) file:
implementation 'androidx.viewpager:viewpager:1.0.0'
I got a fragment that get the data in runtime and I think that what cause the problem, but I'm not sure.
it show the button twice and I cant scroll. if i'm writing false insted of true in View v = inflater.inflate(R.layout.fragment_book_view, container, true);
it shows only the button.
the fragment load in runtime in to FrameLayout at the activity, that layout suppose to contain diffrent fragment each time.
my fragment code:
java:
package javaproject.project5776;
import android.app.AlertDialog;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import entities.Book;
import model.backend.Backend;
import model.backend.BackendFactory;
import util.ImageLoader;
/**
* A simple {#link Fragment} subclass.
*/
public class BookView extends Fragment {
private Bitmap bitmap;
private String name;
public static BookView newInstance(String book,Bitmap bitmap) {
Bundle args = new Bundle();
BookView fragment = new BookView();
fragment.bitmap = bitmap;
fragment.name = book;
fragment.setArguments(args);
return fragment;
}
public BookView() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.fragment_book_view, container, true);
Backend backend = BackendFactory.getInstance();
try {
Book b = backend.readBook(name);
((TextView)v.findViewById(R.id.bookView_name)).setText(b.getBookName());
((TextView)v.findViewById(R.id.bookView_author)).setText(b.getAuthor());
((TextView)v.findViewById(R.id.bookView_category)).setText(b.getCategory().name());
((TextView)v.findViewById(R.id.bookView_description)).setText(b.getDescription());
((TextView)v.findViewById(R.id.bookView_pages)).setText(Integer.toString(b.getPages()));
((TextView)v.findViewById(R.id.bookView_publishingDate)).setText(b.getPublishingDate());
final ImageView imageView = (ImageView)v.findViewById(R.id.bookView_image);
if(bitmap != null){
imageView.setImageBitmap(bitmap);
} else{
ImageLoader imageLoader = new ImageLoader();
imageLoader.setListener(new ImageLoader.ImageTaskListener() {
#Override
public void onActionEnd() {}
#Override
public void onImageDownload(Bitmap bitmap) {
imageView.setImageBitmap(bitmap);
}
});
imageLoader.execute(b.getImage());
}
}
catch (Exception e) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setMessage(e.getMessage());
builder.create().show();
}
return inflater.inflate(R.layout.fragment_book_view, container, false);
}
}
xml:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="5dp"
android:fillViewport="true">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:layout_width="#dimen/bookview.imageWidth"
android:layout_height="250dp"
android:id="#+id/bookView.image"/>
<TextView
android:textSize="#dimen/custom_book_text"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_toEndOf="#id/bookView.image"
android:id="#+id/bookView.name"/>
<TextView
android:textSize="#dimen/custom_book_text"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_toEndOf="#id/bookView.image"
android:layout_below="#id/bookView.name"
android:id="#+id/bookView.author"/>
<TextView
android:textSize="#dimen/custom_book_text"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_toEndOf="#id/bookView.image"
android:layout_below="#id/bookView.author"
android:id="#+id/bookView.pages"/>
<TextView
android:textSize="#dimen/custom_book_text"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_toEndOf="#id/bookView.image"
android:layout_below="#id/bookView.pages"
android:id="#+id/bookView.category"/>
<TextView
android:textSize="#dimen/custom_book_text"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_toEndOf="#id/bookView.image"
android:layout_below="#id/bookView.category"
android:id="#+id/bookView.publishingDate"/>
<TextView
android:id="#+id/bookView.description"
android:layout_width="match_parent"
android:layout_margin="10dp"
android:layout_height="wrap_content"
android:layout_below="#id/bookView.image"/>
<Button
android:background="#color/colorPrimary"
android:textColor="#android:color/white"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end"
android:layout_below="#id/bookView.description"
android:text="#string/bookView_button"/>
</RelativeLayout>
</ScrollView>
Try to change
return inflater.inflate(R.layout.fragment_book_view, container, false) ;
to
return v;
change the first line third parameter to false,and as Lauren.Liuling said neen to change the last line to return v;.
I am new to android development. I have created a List View for displaying items in custom formats. I have given code below that I've used in eclipse. But I'm receiving an error while running the application, "Content has view with id attribute 'android.R.id.list' that is not a ListView class in ListFragment".
This is Activity
import android.app.ListFragment;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
public class CalllogActivity extends ListFragment implements AdapterView.OnItemClickListener{
private CLAdapter adapter;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_list, container, false);
return rootView;
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
//ArrayAdapter adapter = ArrayAdapter.createFromResource(getActivity(), R.array.CallLogs, R.layout.layout_calllogs);
ListView lstDetails= (ListView) getView().findViewById(R.id.lstDtls);
adapter=new CLAdapter(this);
lstDetails.setAdapter(adapter);
lstDetails.setOnItemClickListener(this);
}
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i,
long l) {
Toast.makeText(getActivity(), "Item " + i, Toast.LENGTH_SHORT).show();
}
class CLAdapter extends BaseAdapter{
private Context context;
String[] CallLogNames;
String[] CallNo;
int[] images;
String[] CallDateTime;
public CLAdapter(Context context){
this.context=context;
CallLogNames = context.getResources().getStringArray(R.array.CallLogs);
}
public CLAdapter(CalllogActivity calllogActivity) {
CallLogNames = context.getResources().getStringArray(R.array.CallLogs);
}
#Override
public int getCount() {
return CallLogNames.length;
}
#Override
public Object getItem(int position) {
return CallLogNames[position];
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View row=null;
if(convertView==null)
{
LayoutInflater inflater=(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(R.layout.layout_calllogs, parent, false);
}
else
{
row=convertView;
}
TextView txtName=(TextView) row.findViewById(R.id.txtName);
ImageView imgPhoto=(ImageView) row.findViewById(R.id.imgContact);
TextView txtNo = (TextView) row.findViewById(R.id.txtNo);
TextView txtDateTime = (TextView)row.findViewById(R.id.txtDateTime);
txtName.setText(CallLogNames[position]);
//txtNo.setText(CallLogNames[position]);
//txtDateTime.setText(CallLogNames[position]);
//imgPhoto.setImageResource(images[position]);
return row;
}
}
}
This is the Fragment XML Code
<?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" >
<ListView
android:id="#+id/lstDtls"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</ListView>
</LinearLayout>
This is the custom layout I've created for showing in the List
<?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:orientation="vertical" >
<ImageView
android:id="#+id/imgContact"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:adjustViewBounds="true"
android:maxHeight="60dp"
android:maxWidth="60dp"
android:src="#drawable/unknown_contact" />
<TextView
android:id="#+id/txtName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginLeft="12dp"
android:layout_marginTop="10dp"
android:layout_toRightOf="#+id/imgContact"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/txtNo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/imgContact"
android:layout_alignRight="#+id/txtName"
android:layout_marginBottom="5dp"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/txtDateTime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/txtName"
android:layout_alignBottom="#+id/txtName"
android:layout_alignParentRight="true"
android:layout_marginRight="18dp"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium" />
</RelativeLayout>
When you want to use ListFragment or ListActivity list in your layout has to use predefined id:
android:id="#android:id/list"
In your code you can retrieve this list by using
ListView listview = (ListView)findViewById(android.R.id.list);
ListFragment and ListActivity have hard coded this value and do not work if they cannot find list to use.
Try to change Id of listView
<ListView
android:id="#+id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</ListView>
then use R.id.list not android.R.id.list
To add onto this, it's worth noting that in the ListFragment spec it mentions that the listview named 'list' must have either the sibling android.R.id.empty, or not be empty. My issue was that the listview was both empty and didn't have the empty sibling.
The fix was to add a textview named empty,
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/list">
</ListView>
<TextView
android:id="#android:id/empty"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</TextView>