Populate spinner with key value pair in android fragment - java

I am trying to create a spinner with key value pair in fragment. I am getting this logcat output:
FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.ica.icadashboard/com.ica.icadashboard.HomeActivity}: java.lang.NullPointerException
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
android.app.ActivityThread.access$600(ActivityThread.java:141)
android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
android.os.Handler.dispatchMessage(Handler.java:99)
android.os.Looper.loop(Looper.java:137)
android.app.ActivityThread.main(ActivityThread.java:5041)
java.lang.reflect.Method.invokeNative(Native Method)
java.lang.reflect.Method.invoke(Method.java:511)
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
dalvik.system.NativeStart.main(Native Method)
05-20 08:06:36.247: E/AndroidRuntime(2746): Caused by: java.lang.NullPointerException
android.widget.ArrayAdapter.init(ArrayAdapter.java:310)
android.widget.ArrayAdapter.<init>(ArrayAdapter.java:128)
com.ica.placement.PlacementFragment.setSpinnerContent(PlacementFragment.java:44)
com.ica.placement.PlacementFragment.onCreateView(PlacementFragment.java:29)
android.app.Fragment.performCreateView(Fragment.java:1695)
android.app.FragmentManagerImpl.moveToState(FragmentManager.java:885)
android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1057)
android.app.BackStackRecord.run(BackStackRecord.java:682)
android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1435)
android.app.Activity.performStart(Activity.java:5113)
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2153)
05-20 08:06:36.247: E/AndroidRuntime(2746): ... 11 more
The fragment class:
public class PlacementFragment extends Fragment {
Spinner adm_spinner;
Activity activity;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_placement, container, false);
// Now use the above view to populate the spinner.
setSpinnerContent( view );
return container;
}
private void setSpinnerContent( View view )
{
adm_spinner = (Spinner) view.findViewById( R.id.adm_spinner );
final MyData items[] = new MyData[4];
for (int i = 0; i <= 3; i++) {
items[i] = new MyData("value " + i, "" + i);
}
ArrayAdapter<MyData> adapter = new ArrayAdapter<MyData>(activity,
android.R.layout.simple_spinner_item, items);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
adm_spinner.setAdapter(adapter);
// spinner.setAdapter( adapter );
}
class MyData {
public MyData(String spinnerText, String value) {
this.spinnerText = spinnerText;
this.value = value;
}
public String getSpinnerText() {
return spinnerText;
}
public String getValue() {
return value;
}
public String toString() {
return spinnerText;
}
String spinnerText;
String value;
}
}
The layout file:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Placement Analysis"
android:textAppearance="?android:attr/textAppearanceLarge" />
<ScrollView
android:id="#+id/scrollView1"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_below="#+id/textView1"
android:layout_centerHorizontal="true"
android:paddingTop="5dp" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TableLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true" >
<TableRow
android:id="#+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="1" >
<Spinner
android:id="#+id/tp_spinner"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight=".5" />
<Spinner
android:id="#+id/adm_spinner"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight=".5" />
</LinearLayout>
</TableRow>
<TableRow
android:id="#+id/tableRow2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="1" >
<Spinner
android:id="#+id/centre_spinner"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight=".5" />
<Spinner
android:id="#+id/vertical_spinner"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight=".5" />
</LinearLayout>
</TableRow>
<TableRow
android:id="#+id/tableRow3"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="1" >
<Button
android:id="#+id/calendar_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Calendar" />
<Button
android:id="#+id/process_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Process" />
</LinearLayout>
</TableRow>
</TableLayout>
</RelativeLayout>
</ScrollView>
</RelativeLayout>
What is the problem? Where am I going wrong? How should I populate the spinner?

You should
return view;
instead of
return container;
in your onCreateView(.....) in PlacementFragment
also change this
ArrayAdapter<MyData> adapter = new ArrayAdapter<MyData>(activity,
android.R.layout.simple_spinner_item, items);
to
ArrayAdapter<MyData> adapter = new ArrayAdapter<MyData>(getActivity(),
android.R.layout.simple_spinner_item, items);

Caused by: java.lang.NullPointerException
android.widget.ArrayAdapter.init(ArrayAdapter.java:310)
Apart form the other answer you need to change to
ArrayAdapter<MyData> adapter = new ArrayAdapter<MyData>(getActivity(),
android.R.layout.simple_spinner_item, items);
Activity activity; activity is not initialized.

Try this..
You have not initialize Activity activity initialize it as
activity = getActivity();

Related

Inflating Images on ImageView on a RecylerView

I have been having a problem inflating an images on an Image view in a recyclerview.The images fetched from firebase database into the recyclerview, Precisely i get the following error.
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.cnatra_measure, PID: 21096
android.view.InflateException: Binary XML file line #24: Binary XML file line #24: Error inflating class android.widget.ImageView
Caused by: android.view.InflateException: Binary XML file line #24: Error inflating class android.widget.ImageView
Caused by: java.lang.reflect.InvocationTargetException
and a further in the error i get
at com.example.cnatra_measure.Adapters.Trads_StyleAdapter.onCreateViewHolder(Trads_StyleAdapter.java:54)
at com.example.cnatra_measure.Adapters.Trads_StyleAdapter.onCreateViewHolder(Trads_StyleAdapter.java:21)
my Adapter code is given below
public class Trads_StyleAdapter extends RecyclerView.Adapter<Trads_StyleAdapter.Trads_ViewHolder> {
private Context mCtx;
private List<Styles> stylesList;
public Trads_StyleAdapter(Context mCtx, List<Styles> stylesList) {
this.mCtx = mCtx;
this.stylesList = stylesList;
}
public Trads_StyleAdapter(ArrayList<Styles> stylesList) {
this.stylesList = stylesList;
}
public static class Trads_ViewHolder extends RecyclerView.ViewHolder {
ImageView styleImage;
TextView style_name, style_price;
//List<Food> foods;
//Context ctx;
public Trads_ViewHolder(View v) {
super(v);
styleImage = v.findViewById(R.id.styleIcon);
style_name = v.findViewById(R.id.style_names);
style_price = v.findViewById(R.id.style_price);
}
}
#NonNull
#Override
public Trads_ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(mCtx);
View v = inflater.inflate(R.layout.styles_items, parent, false);
return new Trads_ViewHolder(v);
}
#Override
public void onBindViewHolder(#NonNull Trads_ViewHolder holder, int position) {
Styles mStyles = stylesList.get(position);
holder.style_name.setText(mStyles.getStyle_name());
holder.style_price.setText(mStyles.getPrice());
Picasso.get()
.load(mStyles.getStyle_image())
.placeholder(R.drawable.img_placeholder)
.fit()
.centerCrop()
.into(holder.styleImage);
}
#Override
public int getItemCount() {
return stylesList.size();
}
}
and the XML code for the list items is
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/card_view"
android:layout_gravity="center"
android:layout_margin="5dp"
android:elevation="15dp"
card_view:cardCornerRadius="8dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="250dp"
android:id="#+id/iconWrapper"
android:orientation="vertical">
<ImageView
android:layout_width="130dp"
android:layout_height="130dp"
android:id="#+id/styleIcon"
android:background="?attr/selectableItemBackgroundBorderless"
android:clickable="true"
android:layout_gravity="center_horizontal|center_vertical"
android:scaleType="fitXY"
android:layout_marginTop="15dp"
android:layout_marginBottom="15dp"
android:focusable="true" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/style_names"
android:fontFamily="#font/nunito_semibold"
android:padding="5dp"
android:textSize="20sp"
android:textColor="#color/blue"
android:textAlignment="center"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/naira"
android:fontFamily="#font/nunito_bold"
android:padding="5dp"
android:textSize="15sp"
android:textColor="#color/blue"
android:textAlignment="center"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/style_price"
android:padding="5dp"
android:textSize="15sp"
android:fontFamily="#font/nunito_bold"
android:textColor="#color/black"
android:textAlignment="center"/>
</LinearLayout>
</LinearLayout>
</androidx.cardview.widget.CardView>
</RelativeLayout>
Thanks in advance
Use this xml :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/card_view"
android:layout_gravity="center"
android:layout_margin="5dp"
android:elevation="15dp"
card_view:cardCornerRadius="8dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="250dp"
android:id="#+id/iconWrapper"
android:orientation="vertical">
<ImageView
android:layout_width="130dp"
android:layout_height="130dp"
android:id="#+id/styleIcon"
android:clickable="true"
android:layout_gravity="center"
android:scaleType="fitXY"
android:layout_marginTop="15dp"
android:layout_marginBottom="15dp"
android:focusable="true" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/style_names"
android:fontFamily="#font/nunito_semibold"
android:padding="5dp"
android:textSize="20sp"
android:textColor="#color/blue"
android:textAlignment="center"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/naira"
android:fontFamily="#font/nunito_bold"
android:padding="5dp"
android:textSize="15sp"
android:textColor="#color/blue"
android:textAlignment="center"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/style_price"
android:padding="5dp"
android:textSize="15sp"
android:fontFamily="#font/nunito_bold"
android:textColor="#color/black"
android:textAlignment="center"/>
</LinearLayout>
</LinearLayout>
</androidx.cardview.widget.CardView>
</RelativeLayout>

I put a button inside the fragment marker in my navigation drawer. The application crashes when the button is clicked

The application crashes when I add the setText into the onclickListener.
buttonlar = container.findViewById(R.id.buttonlar);
text = container.findViewById(R.id.text);
View view = inflater.inflate(R.layout.fragment_first, container, false);
buttonlar = view.findViewById(R.id.buttonlar);
buttonlar.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
text.setText("test");
}
});
return view;
}
Logcat
2019-07-14 23:49:09.953 16842-16842/com.medicalsix.doctorsix E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.medicalsix.doctorsix, PID: 16842
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
at com.medicalsix.doctorsix.First$1.onClick(First.java:50)
at android.view.View.performClick(View.java:7251)
at android.view.View.performClickInternal(View.java:7228)
at android.view.View.access$3500(View.java:802)
at android.view.View$PerformClick.run(View.java:27843)
at android.os.Handler.handleCallback(Handler.java:883)
at android.os.Handler.dispatchMessage(Handler.java:100)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7116)
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:925)
instead of
text.setText("test");
It happens if I use it.
Log.i("Test", "Test.");
You can reach the details of my project from github link.
https://github.com/prensmiskin/DoctorSix/blob/master/app/src/main/java/com/medicalsix/doctorsix/First.java
fragment.xml code
If you know, can you help?
<?xml version="1.0" encoding="utf-8"?>
<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="wrap_content"
tools:context=".First"
android:orientation="vertical"
android:id="#+id/bir">
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<Button
android:id="#+id/buttonlar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Dentofasiyal deformite"
android:textSize="20sp"
android:background="#color/colorPrimary"
android:textColor="#android:color/black"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="ssss"
android:id="#+id/text"
android:textSize="20sp"/>
<Button
android:id="#+id/legcdurlyvideo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Dumbbell Fly "
android:textSize="20sp"
android:background="#color/colorPrimary"
android:textColor="#android:color/black"/>
<Button
android:id="#+id/legcurlydvideo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Dumbbell Fly "
android:textSize="20sp"
android:background="#color/colorPrimary"
android:textColor="#android:color/black"/>
<Button
android:id="#+id/legcurlyvidedo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Dumbbell Fly "
android:textSize="20sp"
android:background="#color/colorPrimary"
android:textColor="#android:color/black"/>
<Button
android:id="#+id/legcurlyvidseo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Dumbbell Fly "
android:textSize="20sp"
android:background="#color/colorPrimary"
android:textColor="#android:color/black"/>
<Button
android:id="#+id/legcurlyvideso"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Dumbbell Fly "
android:textSize="20sp"
android:background="#color/colorPrimary"
android:textColor="#android:color/black"/>
<Button
android:id="#+id/legcurlyvaideos"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Dumbbell Fly "
android:textSize="20sp"
android:background="#color/colorPrimary"
android:textColor="#android:color/black"/>
<Button
android:id="#+id/legcurlsyvideos"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Dumbbell Fly "
android:textSize="20sp"
android:background="#color/colorPrimary"
android:textColor="#android:color/black"/>
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="#drawable/cene"
android:adjustViewBounds="true"/>
<WebView
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:ignore="WebViewLayout"
android:id="#+id/webview"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/cene"
android:textSize="20sp"/>
</LinearLayout>
</ScrollView>
</FrameLayout>
I believe you should be getting the TextView from the inflated view instead of the container (whatever that may be). Try the following,
//buttonlar = container.findViewById(R.id.buttonlar);
//text = container.findViewById(R.id.text);
View view = inflater.inflate(R.layout.fragment_first, container, false);
buttonlar = view.findViewById(R.id.buttonlar);
//Use the inflated view to get the text (just like you did for buttonlar)
text = view.findViewById(R.id.text);
buttonlar.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
text.setText("test");
}
});
return view;
}

Can't run App on Note 2 because of too many images (only 20 images)

I am getting the following error when I go from my Main Activity page to my New Activity page. The New Activity page crashes unless it has <10 images or so, which is pathetic (the app runs fine emulated on my computer, however):
06-22 16:53:04.841 15058-15058/? D/AndroidRuntime: Shutting down VM
06-22 16:53:04.841 15058-15058/? W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0x41e35c08)
06-22 16:53:04.851 15058-15058/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.matthewscompany.matthew.test1, PID: 15058
java.lang.OutOfMemoryError
at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:683)
at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:513)
at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:889)
at android.content.res.Resources.loadDrawable(Resources.java:3436)
at android.content.res.TypedArray.getDrawable(TypedArray.java:602)
at android.widget.ImageView.<init>(ImageView.java:133)
at android.support.v7.widget.AppCompatImageView.<init>(AppCompatImageView.java:57)
at android.support.v7.widget.AppCompatImageView.<init>(AppCompatImageView.java:53)
at android.support.v7.app.AppCompatViewInflater.createView(AppCompatViewInflater.java:102)
at android.support.v7.app.AppCompatDelegateImplV7.createView(AppCompatDelegateImplV7.java:972)
at android.support.v7.app.AppCompatDelegateImplV7.onCreateView(AppCompatDelegateImplV7.java:1030)
at android.support.v4.view.LayoutInflaterCompatHC$FactoryWrapperHC.onCreateView(LayoutInflaterCompatHC.java:44)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:690)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:761)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:769)
at android.view.LayoutInflater.inflate(LayoutInflater.java:498)
at android.view.LayoutInflater.inflate(LayoutInflater.java:398)
at android.view.LayoutInflater.inflate(LayoutInflater.java:354)
at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:276)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:136)
at com.example.matthew.test1.NewActivity.onCreate(NewActivity.java:23)
at android.app.Activity.performCreate(Activity.java:5451)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1093)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2377)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2471)
at android.app.ActivityThread.access$900(ActivityThread.java:175)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1308)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:146)
at android.app.ActivityThread.main(ActivityThread.java:5602)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
at dalvik.system.NativeStart.main(Native Method)
06-22 16:53:04.861 2430-2769/? W/ActivityManager: Force finishing activity com.matthewscompany.matthew.test1/com.example.matthew.test1.NewActivity
06-22 16:53:04.866 2430-2769/? W/ActivityManager: Force finishing activity com.matthewscompany.matthew.test1/com.example.matthew.test1.MainActivity
I understand that the out of memory error occurs when the ram for a phone runs out when processing too many bitmaps.
Yes, I have read the Android Developer webpages on tips for processing bitmaps. However, I still don't understand which groups of code from their guides I'm supposed to insert, and where I'm supposed to insert it. I've tried inserting some of their stuff into my .java files but I just end up getting errors. I owe this to me being a beginner at Java and not understanding how to apply the example codes.
Again, I am a beginner to Java and Android Studio so please go easy on me and explain things as simply as you can, if possible.
Here's my code:
MainActivity.java:
package com.example.matthew.test1;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageButton;
public class MainActivity extends AppCompatActivity {
//Main Menu
//Main Activity Mario Icon:
ImageButton Mario;
ImageButton Luigi;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Mario= (ImageButton) findViewById(R.id.Mario);
Mario.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intentLoadNewActivity = new Intent(MainActivity.this, NewActivity.class);
startActivity(intentLoadNewActivity);
}
});
Luigi= (ImageButton) findViewById(R.id.Luigi);
Luigi.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intentLoadNewActivity = new Intent(MainActivity.this, Main2Activity.class);
startActivity(intentLoadNewActivity);
}
});
}
}
NewActivity.java:
public class NewActivity extends AppCompatActivity implements View.OnClickListener {
ViewFlipper viewFlipper;
Button next;
Button previous;
TextView pageCounter;
int count = 1;
private static final int TOTAL_IMG_COUNT = 23;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_new);
viewFlipper = (ViewFlipper)findViewById(R.id.viewFlipper);
next = (Button) findViewById(R.id.next);
previous = (Button) findViewById(R.id.previous);
pageCounter = (TextView)findViewById(R.id.textView2);
pageCounter.setText(String.valueOf(count));
next.setOnClickListener(this);
previous.setOnClickListener(this);
}
#Override
public void onClick(View v) {
if (v == next) {
viewFlipper.showNext();
if (next.getId() == (R.id.ImageView23)) {
count = 1;
} else {
count++;
}
pageCounter.setText(String.valueOf(count) + "/" + TOTAL_IMG_COUNT);
}
else if (v == previous) {
viewFlipper.showPrevious();
if(previous.getId() == (R.id.ImageView1)) {
count = TOTAL_IMG_COUNT;
} else {
count--;
}
pageCounter.setText(String.valueOf(count) + "/" + TOTAL_IMG_COUNT);
}
}
}
content_main.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: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"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="com.example.matthew.test1.MainActivity"
tools:showIn="#layout/activity_main">
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/Mario"
android:src="#drawable/mario_select"
android:layout_above="#+id/samus_select"
android:layout_alignLeft="#+id/samus_select"
android:layout_alignStart="#+id/samus_select" />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/Luigi"
android:src="#drawable/luigi_select"
android:layout_above="#+id/samus_select"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/dk_select"
android:src="#drawable/dk_select"
android:layout_above="#+id/falcon_select"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/link_select"
android:src="#drawable/link_select"
android:layout_below="#+id/fox_select"
android:layout_alignParentLeft="true" />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/samus_select"
android:src="#drawable/samus_select"
android:layout_above="#+id/kirby_select"
android:layout_centerHorizontal="true" />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/falcon_select"
android:src="#drawable/falcon_select"
android:layout_alignTop="#+id/samus_select"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/ness_select"
android:src="#drawable/ness_select"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/yoshi_select"
android:src="#drawable/yoshi_select"
android:layout_below="#+id/falcon_select"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/kirby_select"
android:src="#drawable/kirby_select"
android:layout_above="#+id/pika_select"
android:layout_alignLeft="#+id/samus_select"
android:layout_alignStart="#+id/samus_select" />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/fox_select"
android:src="#drawable/fox_select"
android:layout_alignParentStart="true"
android:layout_below="#+id/Luigi"
android:layout_alignParentLeft="true" />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/pika_select"
android:src="#drawable/pika_select"
android:layout_alignParentBottom="true"
android:layout_alignLeft="#+id/kirby_select"
android:layout_alignStart="#+id/kirby_select" />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/jiggly_select"
android:src="#drawable/jiggly_select"
android:layout_below="#+id/yoshi_select"
android:layout_alignLeft="#+id/yoshi_select"
android:layout_alignStart="#+id/yoshi_select" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Hitbox64"
android:id="#+id/textView"
android:textSize="48dp"
android:layout_above="#+id/Mario"
android:layout_centerHorizontal="true" />
</RelativeLayout>
activity_new.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.matthew.test1.NewActivity">
<ViewFlipper
android:layout_width="480dp"
android:layout_height="480dp"
android:id="#+id/viewFlipper">
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/ImageView1"
android:scaleType="centerCrop"
android:src="#drawable/b1"/>
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/ImageView2"
android:scaleType="centerCrop"
android:src="#drawable/b2"/>
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/ImageView3"
android:scaleType="centerCrop"
android:src="#drawable/b3"/>
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/ImageView4"
android:scaleType="centerCrop"
android:src="#drawable/b4"/>
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/ImageView5"
android:scaleType="centerCrop"
android:src="#drawable/b5"/>
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/ImageView6"
android:scaleType="centerCrop"
android:src="#drawable/b6"/>
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/ImageView7"
android:scaleType="centerCrop"
android:src="#drawable/b7"/>
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/ImageView8"
android:scaleType="centerCrop"
android:src="#drawable/b8"/>
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/ImageView9"
android:scaleType="centerCrop"
android:src="#drawable/b9"/>
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/ImageView10"
android:scaleType="centerCrop"
android:src="#drawable/b10"/>
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/ImageView11"
android:scaleType="centerCrop"
android:src="#drawable/b11"/>
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/ImageView12"
android:scaleType="centerCrop"
android:src="#drawable/b12"/>
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/ImageView13"
android:scaleType="centerCrop"
android:src="#drawable/b13"/>
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/ImageView14"
android:scaleType="centerCrop"
android:src="#drawable/b14"/>
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/ImageView16"
android:scaleType="centerCrop"
android:src="#drawable/b17"/>
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/ImageView18"
android:scaleType="centerCrop"
android:src="#drawable/b18"/>
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/ImageView19"
android:scaleType="centerCrop"
android:src="#drawable/b19"/>
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/ImageView20"
android:scaleType="centerCrop"
android:src="#drawable/b20"/>
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/ImageView21"
android:scaleType="centerCrop"
android:src="#drawable/b21"/>
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/ImageView22"
android:scaleType="centerCrop"
android:src="#drawable/b22"/>
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/ImageView23"
android:scaleType="centerCrop"
android:src="#drawable/b23"/>
</ViewFlipper>
<Button
android:id="#+id/next"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Next"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"/>
<Button
android:id="#+id/previous"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Prev"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentBottom="true"
android:layout_alignTop="#+id/next"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Frame Counter"
android:id="#+id/textView2"
android:layout_below="#+id/textView3"
android:layout_centerHorizontal="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Bair"
android:id="#+id/textView3"
android:layout_alignTop="#+id/previous"
android:layout_centerHorizontal="true" />
</RelativeLayout>
If your images are too high-resolution, they can eat up a LOT of memory, especially if you have 20 of them as you say. Consider using Picasso or some other library to load your images.

How to create a custom adapter for a listview? Getting RessourceNotFoundException

I am trying to make a custom adapter for my listview, but I keep getting a RessourceNotFoundException on the same line.
Firstly, here is my adapter class code:
public class CreateProgramAdapter extends BaseAdapter {
private final LayoutInflater inflater;
private ArrayList<TrainingPass> trainingPasses;
private List<TrainingPass> items;
private int layoutResourceId;
private Context context;
public CreateProgramAdapter(Context context, List<TrainingPass> items) {
this.context = context;
this.items = items;
inflater = LayoutInflater.from(this.context);
}
#Override
public int getCount() {
return items.size();
}
#Override
public Object getItem(int position) {
return items.get(position);
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
TrainingPassHolder holder;
if(convertView == null) {
convertView = inflater.inflate(R.layout.adapter_createprogram_trainingpasses_item, null);
holder = new TrainingPassHolder();
convertView.setTag(holder);
} else {
holder = (TrainingPassHolder) convertView.getTag();
}
holder.trainingPass = this.items.get(position);
holder.trainingPassRelativeLayout = (RelativeLayout) row.findViewById(R.id.trainingpass_item_relativelayout);
holder.headlineTxt = (TextView) row.findViewById(R.id.trainingpass_headline);
holder.setNoTxt = (TextView) row.findViewById(R.id.txt_trainingpass_setno);
holder.showHideIcon = (ImageView) row.findViewById(R.id.trainingpass_showhide_imageview);
holder.exerciseSearchField = (EditText) row.findViewById(R.id.trainingpass_exercise_searchfield);
holder.repsTextField = (EditText) row.findViewById(R.id.trainingpass_reps_txtfield);
holder.addSetButton = (Button) row.findViewById(R.id.add_set_button);
holder.addExerciseButton = (Button) row.findViewById(R.id.trainingpass_addexercise_button);
holder.setsListView = (ListView) row.findViewById(R.id.trainingpass_sets_listview);
holder.exercisesListView = (ListView) row.findViewById(R.id.trainingpass_exercises_listview);
holder.trainingPassRelativeLayout.setTag(holder.trainingPass);
holder.headlineTxt.setTag(holder.trainingPass);
holder.setNoTxt.setTag(holder.trainingPass);
holder.showHideIcon.setTag(holder.trainingPass);
holder.exerciseSearchField.setTag(holder.trainingPass);
holder.repsTextField.setTag(holder.trainingPass);
holder.addSetButton.setTag(holder.trainingPass);
holder.addExerciseButton.setTag(holder.trainingPass);
holder.setsListView.setTag(holder.trainingPass);
holder.exercisesListView.setTag(holder.trainingPass);
setupItems(holder);
return convertView;
}
private void setupItems(TrainingPassHolder trainingPassHolder) {
// TODO: set default values for items
}
public static class TrainingPassHolder {
TrainingPass trainingPass;
RelativeLayout trainingPassRelativeLayout;
TextView headlineTxt;
TextView setNoTxt;
ImageView showHideIcon;
EditText exerciseSearchField;
EditText repsTextField;
Button addSetButton;
Button addExerciseButton;
ListView setsListView;
ListView exercisesListView;
}
}
And here is the xml for the view, containing the listview:
<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:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context="com.mobileplatformexam.activities.CreateProgramActivity"
android:id="#+id/relativeLayoutCreateProgram">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add training pass"
android:id="#+id/btn_add_trainingpass"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_toLeftOf="#+id/btn_add_trainingpass_help"
android:layout_toStartOf="#+id/btn_add_trainingpass_help"
android:onClick="addTrainingPass"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="\?"
android:id="#+id/btn_add_trainingpass_help"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:onClick="addTrainingPassHelp"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Create program"
android:id="#+id/btn_create_program"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignRight="#+id/btn_add_trainingpass_help"
android:layout_alignEnd="#+id/btn_add_trainingpass_help"
android:onClick="createProgram"/>
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/listView_trainingpasses"
android:layout_above="#+id/btn_create_program"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="#+id/btn_add_trainingpass" />
</RelativeLayout>
And the xml file with the item layout for the above listview:
<?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="match_parent">
<RelativeLayout
android:id="#+id/trainingpass_item_relativelayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal">
<TextView
android:id="#+id/trainingpass_headline"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="false"
android:layout_alignParentTop="true"
android:text="Trainingpass"
android:textSize="26dp"
android:textColor="#android:color/white"
android:background="#android:color/holo_blue_dark"
android:height="50dp"
android:gravity="center_vertical"
android:paddingLeft="20dp"
android:layout_toLeftOf="#+id/trainingpass_showhide_imageview"
android:layout_toStartOf="#+id/trainingpass_showhide_imageview"
android:textIsSelectable="true"
android:textStyle="bold" />
<ImageView
android:id="#+id/trainingpass_showhide_imageview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:src="#drawable/abc_spinner_mtrl_am_alpha"
android:layout_alignParentTop="true"
android:background="#android:color/holo_blue_dark"
android:minWidth="50dp"
android:minHeight="50dp"
android:layout_alignBottom="#+id/trainingpass_headline" />
<EditText
android:id="#+id/trainingpass_exercise_searchfield"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/trainingpass_headline"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignRight="#+id/trainingpass_showhide_imageview"
android:layout_alignEnd="#+id/trainingpass_showhide_imageview"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:hint="Name of exercise"
android:padding="15dp"
android:layout_marginTop="10dp" />
<TextView
android:id="#+id/txt_trainingpass_setno"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Set 1"
android:textSize="20dp"
android:textColor="#android:color/black"
android:paddingRight="30dp"
android:paddingLeft="10dp"
android:paddingTop="20dp"
android:paddingBottom="10dp"
android:layout_below="#+id/trainingpass_exercise_searchfield"
android:layout_toLeftOf="#+id/trainingpass_reps_txtfield"
android:layout_toStartOf="#+id/trainingpass_reps_txtfield" />
<EditText
android:id="#+id/trainingpass_reps_txtfield"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="Reps"
android:padding="15dp"
android:width="160dp"
android:layout_alignBottom="#+id/txt_trainingpass_setno"
android:layout_toLeftOf="#+id/add_set_button"
android:layout_toStartOf="#+id/add_set_button"
android:layout_marginRight="10dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/add_set_button"
android:text="Add set"
android:layout_alignBottom="#+id/trainingpass_reps_txtfield"
android:layout_alignRight="#+id/trainingpass_exercise_searchfield"
android:layout_alignEnd="#+id/trainingpass_exercise_searchfield"
android:layout_below="#+id/trainingpass_exercise_searchfield" />
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/trainingpass_sets_listview"
android:layout_alignLeft="#+id/trainingpass_exercise_searchfield"
android:layout_alignStart="#+id/trainingpass_exercise_searchfield"
android:layout_below="#+id/txt_trainingpass_setno" />
<Button
android:text="Add exercise"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/trainingpass_addexercise_button"
android:layout_below="#+id/trainingpass_sets_listview"
android:layout_alignRight="#+id/add_set_button"
android:layout_alignEnd="#+id/add_set_button"
android:layout_alignLeft="#+id/trainingpass_sets_listview"
android:layout_alignStart="#+id/trainingpass_sets_listview" />
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/trainingpass_exercises_listview"
android:layout_below="#+id/trainingpass_addexercise_button"
android:layout_alignLeft="#+id/trainingpass_addexercise_button"
android:layout_alignStart="#+id/trainingpass_addexercise_button" />
</RelativeLayout>
</LinearLayout>
And this is the code for setting the adapter in my activity (extends Activity)
ArrayList<TrainingPass> trainingPasses = new ArrayList<TrainingPass>();
TrainingPass trainingPass = new TrainingPass();
trainingPass.setTrainingPassName("wefwef");
TrainingPass trainingPass2 = new TrainingPass();
trainingPass.setTrainingPassName("rwefhtr");
trainingPasses.add(trainingPass);
trainingPasses.add(trainingPass2);
ListView trainingPassesListView = (ListView)findViewById(R.id.listView_trainingpasses);
CreateProgramAdapter createProgramAdapter = new CreateProgramAdapter(CreateProgramActivity.this, trainingPasses);
trainingPassesListView.setAdapter(createProgramAdapter);
The error i am getting is this:
12661-12661/com.mobileplatformexam E/AndroidRuntime﹕ FATAL EXCEPTION: main
android.content.res.Resources$NotFoundException: Resource ID #0x7f04001a
at android.content.res.Resources.getValue(Resources.java:1019)
at android.content.res.Resources.loadXmlResourceParser(Resources.java:2107)
at android.content.res.Resources.getLayout(Resources.java:858)
at android.view.LayoutInflater.inflate(LayoutInflater.java:394)
at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
at com.mobileplatformexam.Adapters.CreateProgramAdapter.getView(CreateProgramAdapter.java:56)
at android.widget.AbsListView.obtainView(AbsListView.java:2466)
at android.widget.ListView.measureHeightOfChildren(ListView.java:1244)
at android.widget.ListView.onMeasure(ListView.java:1155)
at android.view.View.measure(View.java:12807)
at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:642)
at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:424)
at android.view.View.measure(View.java:12807)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4698)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)
at android.view.View.measure(View.java:12807)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4698)
at android.support.v7.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:453)
at android.view.View.measure(View.java:12807)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4698)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)
at android.view.View.measure(View.java:12807)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4698)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1399)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:690)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:583)
at android.view.View.measure(View.java:12807)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4698)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)
at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2218)
at android.view.View.measure(View.java:12807)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1068)
at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2452)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4476)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:816)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:583)
at dalvik.system.NativeStart.main(Native Method)
The error refers to line 56 in my adapterclass, which is the following line:
convertView = inflater.inflate(R.layout.adapter_createprogram_trainingpasses_item, null);
I really can't seem to find out what may be wrong with the code, so I hope someone can help me out.
Thanks
Thanks to JafarKhQ, I solved the problem.
By changing the layout "adapter_createprogram_trainingpasses_item .xml" to the following, it worked:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:padding="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:orientation="horizontal">
<TextView
android:id="#+id/trainingpass_headline"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="false"
android:layout_alignParentTop="true"
android:text="Trainingpass"
android:textSize="20dp"
android:textColor="#android:color/white"
android:background="#android:color/holo_blue_dark"
android:height="30dp"
android:gravity="center_vertical"
android:paddingLeft="20dp"
android:layout_toLeftOf="#+id/trainingpass_showhide_imageview"
android:layout_toStartOf="#+id/trainingpass_showhide_imageview"
android:textIsSelectable="true"
android:textStyle="bold" />
<ImageView
android:id="#+id/trainingpass_showhide_imageview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:src="#drawable/abc_spinner_mtrl_am_alpha"
android:layout_alignParentTop="true"
android:background="#android:color/holo_blue_dark"
android:height="30dp"
android:minWidth="50dp"
android:layout_alignBottom="#+id/trainingpass_headline" />
<EditText
android:id="#+id/trainingpass_exercise_searchfield"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/trainingpass_headline"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignRight="#+id/trainingpass_showhide_imageview"
android:layout_alignEnd="#+id/trainingpass_showhide_imageview"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:hint="Name of exercise"
android:padding="15dp"
android:layout_marginTop="10dp" />
<TextView
android:id="#+id/txt_trainingpass_setno"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Set 1"
android:textSize="20dp"
android:textColor="#android:color/black"
android:paddingRight="30dp"
android:paddingLeft="10dp"
android:paddingTop="20dp"
android:paddingBottom="10dp"
android:layout_below="#+id/trainingpass_exercise_searchfield"
android:layout_toLeftOf="#+id/trainingpass_reps_txtfield"
android:layout_toStartOf="#+id/trainingpass_reps_txtfield" />
<EditText
android:id="#+id/trainingpass_reps_txtfield"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="Reps"
android:padding="15dp"
android:width="160dp"
android:layout_alignBottom="#+id/txt_trainingpass_setno"
android:layout_toLeftOf="#+id/add_set_button"
android:layout_toStartOf="#+id/add_set_button"
android:layout_marginRight="10dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/add_set_button"
android:text="Add set"
android:layout_alignBottom="#+id/trainingpass_reps_txtfield"
android:layout_alignRight="#+id/trainingpass_exercise_searchfield"
android:layout_alignEnd="#+id/trainingpass_exercise_searchfield"
android:layout_below="#+id/trainingpass_exercise_searchfield" />
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/trainingpass_sets_listview"
android:layout_alignLeft="#+id/trainingpass_exercise_searchfield"
android:layout_alignStart="#+id/trainingpass_exercise_searchfield"
android:layout_below="#+id/txt_trainingpass_setno" />
<Button
android:text="Add exercise"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/trainingpass_addexercise_button"
android:layout_below="#+id/trainingpass_sets_listview"
android:layout_alignRight="#+id/add_set_button"
android:layout_alignEnd="#+id/add_set_button"
android:layout_alignLeft="#+id/trainingpass_sets_listview"
android:layout_alignStart="#+id/trainingpass_sets_listview" />
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/trainingpass_exercises_listview"
android:layout_below="#+id/trainingpass_addexercise_button"
android:layout_alignLeft="#+id/trainingpass_addexercise_button"
android:layout_alignStart="#+id/trainingpass_addexercise_button" />
</RelativeLayout>

Dialog Fragment is crashing

i have a Dialog fragment that i show using. Btw i have updated my support libraries to include material design theme and new classes. Is it possible that the new fragmentDialog fragments are buggy
fragment.show(getSupportedFragmentManager(),null);
here is the code for the fragment
public class NewTimedEvent extends DialogFragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_new_timed_event, container, false);
}
#Override // show User interface in a dialog
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the Builder class for convenient dialog construction
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
LayoutInflater inflater = getActivity().getLayoutInflater();
View view = inflater.inflate(R.layout.fragment_new_timed_event, null);
builder.setView(view);
builder.setPositiveButton(getString(android.R.string.ok), new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
}
});
builder.setNegativeButton(getString(android.R.string.cancel), new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
}
});
// Create the AlertDialog object and return it
return builder.create();
}
}
here is the error is get
11-20 19:05:17.665 14612-14612/prototyping.materialdesign E/AndroidRuntime﹕ FATAL EXCEPTION: main
android.util.AndroidRuntimeException: requestFeature() must be called before adding content
at com.android.internal.policy.impl.PhoneWindow.requestFeature(PhoneWindow.java:267)
at com.android.internal.app.AlertController.installContent(AlertController.java:235)
at android.app.AlertDialog.onCreate(AlertDialog.java:336)
at android.app.Dialog.dispatchOnCreate(Dialog.java:351)
at android.app.Dialog.show(Dialog.java:256)
at android.support.v4.app.DialogFragment.onStart(DialogFragment.java:398)
at android.support.v4.app.Fragment.performStart(Fragment.java:1810)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:977)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1126)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:739)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1489)
at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:454)
at android.os.Handler.handleCallback(Handler.java:615)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4898)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1008)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:775)
at dalvik.system.NativeStart.main(Native Method)
finally here is the code for Fragment UI
<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:orientation="vertical"
tools:context="prototyping.materialdesign.NewTimedEvent">
<LinearLayout
android:paddingTop="10dp"
android:paddingBottom="#dimen/activity_vertical_margin"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:showDividers="middle"
android:divider="?android:dividerVertical">
<TextView
android:id="#+id/action_time"
android:text="Action time"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:textStyle="bold"
/>
<TextView
android:id="#+id/time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="00:00"
android:paddingRight="#dimen/activity_horizontal_margin"
/>
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#d0d0d0"/>
<TextView
android:id="#+id/days"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
android:textStyle="bold"
android:text="Select Days for action to take place"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<RadioButton
android:id="#+id/sound"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/sound" />
<RadioButton
android:id="#+id/vibrate"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/vibrate" />
<RadioButton
android:id="#+id/silent"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/silent" />
</RadioGroup>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<CheckBox
android:id="#+id/wifi"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/wifi" />
<CheckBox
android:id="#+id/bluetooth"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/bluetooth" />
</LinearLayout>
<CheckBox
android:id="#+id/airplane"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/airplane_mode"/>
<EditText
android:id="#+id/event_name"
android:hint="Event name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>

Categories