java - App Crashing while passing data from activity to fragment -
i trying pass data "training2.java" class "training1.java class", , app crashing when this. "training2" activity, , "training1" fragment, believe issue stemming from. getting error
"android.content.activitynotfoundexception: unable find explicit activity class {com.hardingsoftware.hrcfitness/com.hardingsoftware.hrcfitness.training1}; have declared activity in androidmanifest.xml?",
so i'm issue. idea how pass off data training 1, while maintaining settings using intent?
training 1:
package com.hardingsoftware.hrcfitness; /** * created john on 2/7/16. */ import android.support.v4.app.fragment; import android.content.intent; import android.content.sharedpreferences; import android.os.bundle; import android.support.annotation.nonnull; import android.support.v7.app.appcompatactivity; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; import android.widget.adapterview; import android.widget.arrayadapter; import android.widget.button; import android.widget.listview; import android.widget.textview; import android.widget.toast; public class training1 extends fragment implements view.onclicklistener, adapterview.onitemclicklistener{ private listview lwitems; private button btnchangeitems; private string default_items_values_string = "item 1; item 2"; private string customitemstring; private static sharedpreferences preferenceitems; @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate){ view rootview = inflater.inflate(r.layout.content_main, container, false); button btnchangeitems = (button) rootview.findviewbyid(r.id.btnchangeitems); btnchangeitems.setonclicklistener(this); lwitems = (listview) rootview.findviewbyid(r.id.lwitems); preferenceitems = getactivity().getsharedpreferences("items", 0); return rootview; } @override public void onresume() { super.onresume(); bundle setitemsintent = getactivity().getintent().getextras(); if (setitemsintent != null){ customitemstring = getactivity().getintent().getextras().getstring("customvalues"); } string[] items; string preferenceitemsstring = preferenceitems.getstring("customvalues", null); if( preferenceitemsstring != null){ items = getsplitandtrim(preferenceitemsstring); }else{ items = getsplitandtrim(default_items_values_string); } arrayadapter<string> arrayadapter = new arrayadapter<>(getactivity(), android.r.layout.simple_expandable_list_item_1, items); lwitems.setadapter(arrayadapter); lwitems.setonitemclicklistener(this); } @override public void onpause() { super.onpause(); if(customitemstring != null){ putstringinpreferences("customvalues", customitemstring); } } @override public void onclick(view v) { intent intent = new intent(getactivity(), training2.class); startactivity(intent); } @override public void onitemclick(adapterview<?> parent, view view, int position, long id) { string textofitem = string.valueof(((textview) view).gettext()); toast.maketext(getactivity(), textofitem, toast.length_short).show(); } public static void putstringinpreferences(string key, string customitemstring) { sharedpreferences.editor editor = preferenceitems.edit(); editor.putstring(key, customitemstring); editor.commit(); } @nonnull private string[] getsplitandtrim(string preferenceitemsstring) { return preferenceitemsstring.trim().split("\\s*;\\s*"); } }
training 2:
package com.hardingsoftware.hrcfitness; /** * created john on 2/7/16. */ import android.content.intent; import android.content.sharedpreferences; import android.os.bundle; import android.support.design.widget.floatingactionbutton; import android.support.design.widget.snackbar; import android.support.v4.app.fragment; import android.support.v7.app.appcompatactivity; import android.support.v7.widget.toolbar; import android.view.view; import android.widget.button; import android.widget.edittext; import android.widget.toast; public class training2 extends appcompatactivity implements view.onclicklistener{ edittext etinputvalues; button btnsetvales; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_set_items); etinputvalues = (edittext) findviewbyid(r.id.etinputvalues); btnsetvales = (button) findviewbyid(r.id.btnsetvalues); btnsetvales.setonclicklistener(this); } @override public void onclick(view v) { string etinputvaluestext = string.valueof(etinputvalues.gettext()); if (!etinputvaluestext.equals("")){ training1.putstringinpreferences("customvalues", etinputvaluestext); intent intent = new intent(this, training1.class); intent.putextra("customvalues", etinputvaluestext); intent.setflags(intent.flag_activity_clear_top); intent.putextra("exit", true); startactivity(intent); }else{ toast.maketext(training2.this, "no values added!", toast.length_short).show(); } } }
you need load training1 fragment fragmenttransaction, , not if activity...
in main activity layout might have place load framgments :
<framelayout android:id="@+id/content" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1"/>
then attach training1 fragment container use:
fragmenttransaction ft = getsupportfragmentmanager().begintransaction(); ft.replace(r.id.content, new training1(), "training_1_tag"); ft.commit();
now fragment tag, there setup variables and/or trigger method:
training1 training1frament = (training1) getsupportfragmentmanager().findfragmentbytag("training_1_tag"); training1frament.mypublicvar = "my variable"; training1frament.mypublicmethod();
Comments
Post a Comment