Java findViewById无法解析方法
我尝试使用类似的文本视图,但我总是得到Java findViewById无法解析方法,java,android,Java,Android,我尝试使用类似的文本视图,但我总是得到findById的错误“无法解析方法”。我发现,这是因为我的类没有扩展类活动,但我不知道如何修复它。 setContenview前面的getActivity()和onCreate中的getView只有在我第一次构建应用程序时才起作用。 有没有一种在文本视图中显示字符串和整数的简单方法 我的XML: <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
findById
的错误“无法解析方法”。我发现,这是因为我的类没有扩展类活动,但我不知道如何修复它。
setContenview
前面的getActivity()
和onCreate
中的getView
只有在我第一次构建应用程序时才起作用。
有没有一种在文本视图中显示字符串和整数的简单方法
我的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>
我的代码:
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;
/**
* A placeholder fragment containing a simple view.
*/
public class DeviceSetupActivityFragment extends Fragment implements ServiceConnection {
//private MetaWearBoard mwBoard;
public Bmi160Accelerometer accModule;
TextView counted_steps; //for showing the steps in the 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 the 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 the service when the activity is 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 from the 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 the app has reconnected to the board
*/
public void reconnected() { }
/**
* Called when the mwBoard field is ready to be 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();
}
}
}
package com.mbientlab.metawear.starter;
导入android.app.Activity;
导入android.bluetooth.bluetooth设备;
导入android.content.ComponentName;
导入android.content.Context;
导入android.content.Intent;
导入android.content.ServiceConnection;
导入android.os.IBinder;
导入android.support.v4.app.Fragment;
导入android.os.Bundle;
导入android.view.LayoutInflater;
导入android.view.view;
导入android.view.view.OnClickListener;
导入android.widget.Button;
导入android.os.Bundle;
导入android.view.ViewGroup;
导入android.util.Log;
导入android.widget.TextView;
导入com.mbientlab.metawear.MetaWearBleService;
导入com.mbientlab.metawear.MetaWearBoard;
导入com.mbientlab.metawear.data.CartesianFloat;
导入com.mbientlab.metawear.AsyncOperation;
导入com.mbientlab.metawear.module.Gpio;
导入com.mbientlab.metawear.module.Timer;
导入com.mbientlab.metawear.Message;
导入com.mbientlab.metawear.RouteManager;
导入静态com.mbientlab.metawear.MetaWearBoard.ConnectionStateHandler;
导入静态com.mbientlab.metawear.AsyncOperation.CompletionHandler;
导入com.mbientlab.metawear.UnsupportedModuleException;
导入com.mbientlab.metawear.module.bmi160加速计;
/**
*包含简单视图的占位符片段。
*/
公共类DeviceSetupActivityFragment扩展片段实现ServiceConnection{
//私人超耐磨板;
公共BMI模块;
TextView计数的步骤;//用于在TextView中显示步骤
公共接口碎片设置{
蓝牙设备getBtDevice();
}
私有MetaWearBoard mwBoard=null;
私人碎片设置;
公共设备设置活动片段(){
}
@凌驾
已创建视图上的公共void(视图,捆绑保存状态){
super.onViewCreated(视图,savedInstanceState);
view.findViewById(R.id.acc_start).setOnClickListener(新视图.OnClickListener()){
@凌驾
公共void onClick(视图v){
accModule.readStepCounter(假);
}
});
view.findViewById(R.id.acc_-stop).setOnClickListener(新的view.OnClickListener(){
@凌驾
公共void onClick(视图v){
accModule.resetStepCounter();
}
});
}
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment\u设备\u设置);
counted_steps=(TextView)findViewById(R.id.stepView);
活动所有者=getActivity();
如果(!(碎片设置的所有者实例)){
抛出新的ClassCastException(“拥有活动必须实现FragmentSettings接口”);
}
设置=(碎片设置)所有者;
owner.getApplicationContext().bindService(新意图(owner,MetaWearBleService.class),this,Context.BIND\u AUTO\u CREATE);
}
@凌驾
公共空间{
super.ondestory();
///<在活动被销毁时解除服务绑定
getActivity().getApplicationContext().unbindService(此);
}
@凌驾
CreateView上的公共视图(布局、充气机、视图组容器、捆绑包保存状态){
setRetainInstance(真);
返回充气机。充气(R.layout.fragment\u device\u设置,容器,错误);
}
@凌驾
服务连接上的公共无效(组件名称,IBinder服务){
mwBoard=((MetaWearBleService.LocalBinder)服务).getMetaWearBoard(settings.getBtDevice());
ready();
//从芯片的步进计数器路由数据
accModule.routeData().fromStepCounter(false).stream(“步骤计数器”).commit()
.onComplete(新的CompletionHandler(){
@凌驾
公共作废成功(RouteManager结果){
subscribe(“步骤计数器”,newRouteManager.MessageHandler()){
@凌驾
公共作废处理(消息消息消息){
Log.i(“MainActivity”、“Steps=“+msg.getData(Integer.class));
counted_steps.setText(“steps:+msg.getData(Integer.class));
}
});
}
});
accModule.start();
}
@凌驾
ServiceDisconnected上的公共无效(组件名称){
}
/**
*当应用程序重新连接到电路板时调用
*/
公共无效已重新连接(){}
/**
*当mwBoard字段准备好使用时调用
*/
公共空间就绪(){
试一试{
accModule=mwBoard.getModule(bmi160Accelerator.class);
accModule.configureStepDetection()
.设置灵敏度(BMI160加速计.阶跃灵敏度.正常)
//启用步进计数器
.enableStepCounter()
.commit();
}捕获(未支持的模块异常){
e、 printStackTrace();
}
}
}
我总是得到findById
的错误“无法解析方法”。我发现,这是因为我的类没有扩展类活动
对,您使用的是片段
在