java - findViewById cannot resolve method -
i tried use textview here error "cannot resolve method" findbyid
. figured out, because class not extends class activity don't know hot fix it. getactivity()
in front of setcontenview
, getview
in oncreate
worked first time built app. there simple way display string , integer in textview?
my xml:
<relativelayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingbottom="@dimen/activity_vertical_margin" android:paddingleft="@dimen/activity_horizontal_margin" android:paddingright="@dimen/activity_horizontal_margin" android:paddingtop="@dimen/activity_vertical_margin" tools:context="com.mbientlab.metawear.starter.devicesetupactivityfragment" tools:showin="@layout/activity_device_setup"> <button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="sync" android:id="@+id/acc_start" android:layout_alignparenttop="true" android:layout_centerhorizontal="true" android:layout_margintop="71dp" /> <button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="reset" android:id="@+id/acc_stop" android:layout_below="@+id/acc_start" android:layout_centerhorizontal="true" android:layout_margintop="59dp" /> <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:textappearance="?android:attr/textappearancelarge" android:text="steps: 0" android:id="@+id/stepview" android:layout_below="@+id/acc_stop" android:layout_centerhorizontal="true" android:layout_margintop="118dp" /> </relativelayout>
my code:
package com.mbientlab.metawear.starter; import android.app.activity; import android.bluetooth.bluetoothdevice; import android.content.componentname; import android.content.context; import android.content.intent; import android.content.serviceconnection; import android.os.ibinder; import android.support.v4.app.fragment; import android.os.bundle; import android.view.layoutinflater; import android.view.view; import android.view.view.onclicklistener; import android.widget.button; import android.os.bundle; import android.view.viewgroup; import android.util.log; import android.widget.textview; import com.mbientlab.metawear.metawearbleservice; import com.mbientlab.metawear.metawearboard; import com.mbientlab.metawear.data.cartesianfloat; import com.mbientlab.metawear.asyncoperation; import com.mbientlab.metawear.module.gpio; import com.mbientlab.metawear.module.timer; import com.mbientlab.metawear.message; import com.mbientlab.metawear.routemanager; import static com.mbientlab.metawear.metawearboard.connectionstatehandler; import static com.mbientlab.metawear.asyncoperation.completionhandler; import com.mbientlab.metawear.unsupportedmoduleexception; import com.mbientlab.metawear.module.bmi160accelerometer; /** * placeholder fragment containing simple view. */ public class devicesetupactivityfragment extends fragment implements serviceconnection { //private metawearboard mwboard; public bmi160accelerometer accmodule; textview counted_steps; //for showing steps in textview public interface fragmentsettings { bluetoothdevice getbtdevice(); } private metawearboard mwboard= null; private fragmentsettings settings; public devicesetupactivityfragment() { } @override public void onviewcreated(view view, bundle savedinstancestate) { super.onviewcreated(view, savedinstancestate); view.findviewbyid(r.id.acc_start).setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { accmodule.readstepcounter(false); } }); view.findviewbyid(r.id.acc_stop).setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { accmodule.resetstepcounter(); } }); } @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.fragment_device_setup); counted_steps = (textview) findviewbyid(r.id.stepview); activity owner = getactivity(); if (!(owner instanceof fragmentsettings)) { throw new classcastexception("owning activity must implement fragmentsettings interface"); } settings = (fragmentsettings) owner; owner.getapplicationcontext().bindservice(new intent(owner, metawearbleservice.class), this, context.bind_auto_create); } @override public void ondestroy() { super.ondestroy(); ///< unbind service when activity destroyed getactivity().getapplicationcontext().unbindservice(this); } @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { setretaininstance(true); return inflater.inflate(r.layout.fragment_device_setup, container, false); } @override public void onserviceconnected(componentname name, ibinder service) { mwboard= ((metawearbleservice.localbinder) service).getmetawearboard(settings.getbtdevice()); ready(); // route data chip's step counter accmodule.routedata().fromstepcounter(false).stream("step_counter").commit() .oncomplete(new completionhandler<routemanager>() { @override public void success(routemanager result) { result.subscribe("step_counter", new routemanager.messagehandler() { @override public void process(message msg) { log.i("mainactivity", "steps= " + msg.getdata(integer.class)); counted_steps.settext("steps: " + msg.getdata(integer.class)); } }); } }); accmodule.start(); } @override public void onservicedisconnected(componentname name) { } /** * called when app has reconnected board */ public void reconnected() { } /** * called when mwboard field ready used */ public void ready() { try { accmodule = mwboard.getmodule(bmi160accelerometer.class); accmodule.configurestepdetection() .setsensitivity(bmi160accelerometer.stepsensitivity.normal) // enable step counter .enablestepcounter() .commit(); } catch (unsupportedmoduleexception e) { e.printstacktrace(); } } }
i error "cannot resolve method"
findbyid
. figured out, because class not extends classactivity
.
right, using fragment
...
a
getactivity()
in front ofsetcontentview
,getview
inoncreate
worked first time built app.
you shouldn't using oncreate
method fragment. @ least not modify state of activity, when code can in activity class. example, fragment shouldn't changing content view of activity.
the majority of code used fragment can placed in oncreateview
(which 1 place can use findviewbyid
), leads to...
is there simple way display string , integer in textview?
yes, there many places can it. example, can use view.findviewbyid
onviewcreated
method , set respective textview
layout file used within oncreateview
.
Comments
Post a Comment