Android app stops working after moving to using creating the layout with java -
i making app predicts winner of soccer game. new android programming , recyclerview tutorials little confusing, need put information each user prediction cardview, transfered xml code java, when got activity, says shutting down vm right away, , not log messages put in. users data predictions in sqlite database, read information. below java , xml files, not have use xml file, if possible.
history activity java file:
package com.winansbros.soccerpredictor; import android.app.activity; import android.content.context; import android.content.intent; import android.content.res.typedarray; import android.database.cursor; import android.os.bundle; import android.support.v7.widget.cardview; import android.text.method.scrollingmovementmethod; import android.util.log; import android.view.view; import android.view.viewgroup; import android.widget.button; import android.widget.imageview; import android.widget.relativelayout; import android.widget.textview; import java.util.arraylist; import java.util.list; import com.google.android.gms.ads.adrequest; import com.google.android.gms.ads.adsize; import com.google.android.gms.ads.adview; public class history extends activity { textview historytextview; context ctx = this; adview madview; adrequest adrequest; button clearbutton; button tipsbutton; stringbuilder sb; databaseoperations dop; integer team1image = 1; integer team2image = 1; adview adview = new adview(ctx); button clearhistory = new button(ctx); button tips = new button(ctx); relativelayout mainrl = new relativelayout(ctx); typedarray imgs = getresources().obtaintypedarray(r.array.images); public void oncreate(final bundle savedinstancestate) { super.oncreate(savedinstancestate); dop = new databaseoperations(ctx); cursor cr = dop.getinformation(dop); cr.movetofirst(); if( cr != null && cr.movetofirst() ){ relativelayout.layoutparams mainrlp = new relativelayout.layoutparams(viewgroup.layoutparams.match_parent, viewgroup.layoutparams.match_parent); mainrl.setlayoutparams(mainrlp); string adid = getresources().getstring(r.string.banner_ad_unit_id); relativelayout.layoutparams adviewlayoutparams = new relativelayout.layoutparams(viewgroup.layoutparams.wrap_content, relativelayout.layoutparams.wrap_content); adviewlayoutparams.addrule(relativelayout.center_horizontal); adviewlayoutparams.addrule(relativelayout.align_parent_bottom); relativelayout.layoutparams clearhistorylp = new relativelayout.layoutparams(viewgroup.layoutparams.wrap_content, relativelayout.layoutparams.wrap_content); clearhistorylp.addrule(relativelayout.above, adview.getid()); clearhistorylp.addrule(relativelayout.align_parent_left); relativelayout.layoutparams tipsbuttonlp = new relativelayout.layoutparams(viewgroup.layoutparams.wrap_content, viewgroup.layoutparams.wrap_content); tipsbuttonlp.addrule(relativelayout.above, adview.getid()); tipsbuttonlp.addrule(relativelayout.align_parent_right); adview.setadsize(adsize.banner); adview.setadunitid(adid); adview.setlayoutparams(adviewlayoutparams); clearhistory.setlayoutparams(clearhistorylp); tips.setlayoutparams(tipsbuttonlp); clearhistory.settext("clear history"); tips.settext("tips"); mainrl.addview(adview); mainrl.addview(clearhistory); mainrl.addview(tips); setcontentview(mainrl); cr.close(); gethistory(); } else { setcontentview(r.layout.activity_history); clearbutton = (button) findviewbyid(r.id.clearsqlite); tipsbutton = (button) findviewbyid(r.id.tipsbutton); } madview = (adview) this.findviewbyid(r.id.adview); adrequest = new adrequest.builder() .addtestdevice("8ac41e108cd62b7703ff28358aeec8bc") .build(); madview.loadad(adrequest); clearbutton.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { databaseoperations dob = new databaseoperations(ctx); dob.deleteinformation(ctx); dob.close(); finish(); } }); //sb = new stringbuilder(); historytextview = (textview) findviewbyid(r.id.historytext); historytextview.setmovementmethod(new scrollingmovementmethod()); tipsbutton.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { intent intent = new intent(getapplicationcontext(), tipsactivity.class); startactivity(intent); } }); dop.close(); } public void gethistory() { log.i("gethistory", "initialized"); cursor cr = dop.getinformation(dop); cr.movetofirst(); list<string> winners = new arraylist<>(); list<string> hometeams = new arraylist<>(); list<string> awayteams = new arraylist<>(); list<string> scores = new arraylist<>(); { winners.add(cr.getstring(0)); hometeams.add(cr.getstring(1)); awayteams.add(cr.getstring(2)); scores.add(cr.getstring(3)); log.d("cloud files", "object id set"); } while (cr.movetonext()); log.d("gethistory", "lists set"); setimages(hometeams, awayteams); log.d("gethistory", "images set"); int size = winners.size(); log.d("cloud files", integer.tostring(size)); (int = 0; < size; i++) { log.d("gethistory", "starting loop"); cardview cv = new cardview(ctx); relativelayout ll = new relativelayout(ctx); textview team1name = new textview(ctx); textview team2name = new textview(ctx); imageview team1imageview = new imageview(ctx); imageview team2imageview = new imageview(ctx); textview cardscore = new textview(ctx); log.d("gethistory", "views set up"); relativelayout.layoutparams rlp = new relativelayout.layoutparams(viewgroup.layoutparams.match_parent, viewgroup.layoutparams.match_parent); relativelayout.layoutparams lp = new relativelayout.layoutparams(relativelayout.layoutparams.match_parent, relativelayout.layoutparams.wrap_content); lp.addrule(relativelayout.center_in_parent); relativelayout.layoutparams imageview1 = new relativelayout.layoutparams(60, 60); imageview1.addrule(relativelayout.align_parent_left); imageview1.addrule(relativelayout.align_parent_start); imageview1.addrule(relativelayout.center_vertical); relativelayout.layoutparams imageview2 = new relativelayout.layoutparams(60, 60); imageview2.addrule(relativelayout.align_parent_right); imageview2.addrule(relativelayout.align_parent_end); imageview2.addrule(relativelayout.center_vertical); relativelayout.layoutparams textview1 = new relativelayout.layoutparams(100, relativelayout.layoutparams.wrap_content); textview1.addrule(relativelayout.center_vertical); textview1.addrule(relativelayout.right_of, team1imageview.getid()); textview1.addrule(relativelayout.end_of, team1imageview.getid()); textview1.addrule(relativelayout.below, team2name.getid()); relativelayout.layoutparams textview2 = new relativelayout.layoutparams(100, relativelayout.layoutparams.wrap_content); textview2.addrule(relativelayout.center_vertical); textview2.addrule(relativelayout.left_of, team2imageview.getid()); textview2.addrule(relativelayout.start_of, team2imageview.getid()); textview2.addrule(relativelayout.below, team2name.getid()); relativelayout.layoutparams textview3 = new relativelayout.layoutparams(20, relativelayout.layoutparams.wrap_content); textview3.addrule(relativelayout.center_vertical); textview3.addrule(relativelayout.right_of, team1name.getid()); textview3.addrule(relativelayout.end_of, team1name.getid()); textview3.addrule(relativelayout.below, team2name.getid()); log.d("gethistory", "layout params set"); team1name.settext(hometeams.get(i)); team2name.settext(awayteams.get(i)); team1imageview.setimageresource(imgs.getresourceid(team1image, -1)); cardscore.settext(scores.get(i)); team2imageview.setimageresource(imgs.getresourceid(team2image, -1)); team1imageview.setlayoutparams(imageview1); team2imageview.setlayoutparams(imageview2); team2name.setlayoutparams(textview1); team1name.setlayoutparams(textview2); cardscore.setlayoutparams(textview3); log.d("gethistory", "views set parents"); mainrl.addview(cv); cv.setlayoutparams(lp); ll.setlayoutparams(rlp); cv.addview(ll); ll.addview(team1imageview); ll.addview(team1name); ll.addview(team2imageview); ll.addview(team2name); ll.addview(cardscore); log.d("gethistory", "views set objects"); log.d("gethistory", "views values set"); setcontentview(mainrl); imgs.recycle(); /**if (appendseparator) sb.append("\n"); appendseparator = true; sb.append(hometeams.get(i)); sb.append(" "); sb.append(scores.get(i)); sb.append(" "); sb.append(awayteams.get(i)); historytextview.settext(sb.tostring());*/ } } public void setimages(list<string> hometeams, list<string> awayteams) { string[] bplteams = new string[20]; bplteams[0] = "arsenal"; bplteams[1] = "aston villa"; bplteams[2] = "burnley"; bplteams[3] = "chelsea"; bplteams[4] = "crystal palace"; bplteams[5] = "everton"; bplteams[6] = "hull city"; bplteams[7] = "leicester city"; bplteams[8] = "liverpool"; bplteams[9] = "man city"; bplteams[10] = "man united"; bplteams[11] = "newcastle"; bplteams[12] = "qpr"; bplteams[13] = "southampton"; bplteams[14] = "stoke city"; bplteams[15] = "sunderland"; bplteams[16] = "swansea city"; bplteams[17] = "tottenham"; bplteams[18] = "west brom"; bplteams[19] = "west ham"; string[] laligateams = new string[20]; laligateams[0] = "almería"; laligateams[1] = "athletic bilbao"; laligateams[2] = "athlético madrid"; laligateams[3] = "barcalona"; laligateams[4] = "celta vigo"; laligateams[5] = "córdoba"; laligateams[6] = "deportivo la coruña"; laligateams[7] = "eibar"; laligateams[8] = "elche"; laligateams[9] = "espanyol"; laligateams[10] = "getafe"; laligateams[11] = "granada"; laligateams[12] = "levante"; laligateams[13] = "málaga"; laligateams[14] = "rayo vallecano"; laligateams[15] = "real madrid"; laligateams[16] = "real sociedad"; laligateams[17] = "sevilla"; laligateams[18] = "valencia"; laligateams[19] = "villarreal"; (int = 0; < hometeams.size(); i++) { if (hometeams.get(i) == bplteams[i]) { team1image = i; } else if (hometeams.get(i) == laligateams[i]) { team1image = i; } else if (awayteams.get(i) == bplteams[i]) { team1image = i; } else if (awayteams.get(i) == laligateams[i]) { team2image = i; } } } }
xml file history activity:
<com.google.android.gms.ads.adview android:id="@+id/adview" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerhorizontal="true" android:layout_alignparentbottom="true" ads:adsize="banner" ads:adunitid="@string/banner_ad_unit_id"> </com.google.android.gms.ads.adview> <button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="clear history" android:id="@+id/clearsqlite" android:layout_above="@+id/adview" android:layout_alignparentleft="true" android:layout_alignparentstart="true" android:width="150dp" /> <button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="tips" android:id="@+id/tipsbutton" android:width="150dp" android:layout_above="@+id/adview" android:layout_alignparentright="true" android:layout_alignparentend="true"/> <android.support.v7.widget.cardview tools:context="com.winansbros.soccerpredictor.history" android:id="@+id/card_view" android:layout_gravity="center" card_view:cardcornerradius="4dp" card_view:cardelevation="10dp" android:layout_width="match_parent" android:layout_height="wrap_content"> <relativelayout android:layout_width="match_parent" android:layout_height="wrap_content"> <imageview android:layout_width="60dp" android:layout_height="60dp" android:id="@+id/team1imageview" android:layout_alignparenttop="true" /> <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/team1name" android:layout_centervertical="true" android:layout_below="@+id/team2name" android:layout_torightof="@+id/team1imageview" android:layout_toendof="@+id/team1imageview" android:width="100dp"/> <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/card1score" android:width="20dp" android:layout_centervertical="true" android:layout_aligntop="@+id/team1name" android:layout_torightof="@+id/team1name" android:layout_toendof="@+id/team1name"/> <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/team2name" android:width="100dp" android:layout_centervertical="true" android:layout_toleftof="@+id/team2imageview" android:layout_tostartof="@+id/team2imageview"/> <imageview android:layout_width="60dp" android:layout_height="60dp" android:id="@+id/team2imageview" android:layout_centervertical="true" android:layout_alignparentend="true" android:layout_alignparentright="true"/> </relativelayout> </android.support.v7.widget.cardview> </relativelayout>
any suggestions? again new android development, may easy. thank in advance.
all view instances should created in oncreated()
method.
since doing
adview adview = new adview(ctx); button clearhistory = new button(ctx); button tips = new button(ctx); relativelayout mainrl = new relativelayout(ctx);
the views being created in activity
default constructor, when not ready inflate view. therefore breaks application.
so... should do?
i suggest should move here referenced code + call typedarray imgs = getresources().obtaintypedarray(r.array.images);
oncreate()
method, when context
of activity
ready.
hope helps.
Comments
Post a Comment