2016-08-23 5 views
-1

を解決できませんhereのようなテキストビューを使用しようとしましたが、私はいつも "解決できません"というエラーをfindByIdに受け取ります。私は、クラスがアクティビティを拡張していないためですが、それを修正するために熱心ではないと分かりました。 setContenviewの前のgetActivity()onCreategetViewは、初めてアプリケーションを作成しました。 テキストビューに文字列と整数を表示する簡単な方法はありますか?findViewByIdはメソッド

マイ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(); 
     } 

    } 
} 
+2

まず、ActivityクラスをFragmentに「結合」したようです。「setContentView」はどちらも解決しません –

+0

'view'をグローバル属性として保存し、' findViewById'を呼び出すために使用します –

+0

@SantiGil - それはあなたが断片を使うべき方法ではありません。両方とも、この回答を見てください。http://stackoverflow.com/a/6496013/2308683 –

答えて

0

私は常にエラーがfindByIdのための "方法を解決することはできません" を取得。私のクラスはクラスActivityを拡張していないので、それが分かりました。

右、あなただけonCreatesetContentViewgetViewの前でgetActivity()は私がアプリを構築して初めて働いたFragment ...

を使用しています。

フラグメンテーションにはonCreateメソッドを使用しないでください。少なくとも、アクティビティの状態を変更しないようにしてください。そのコードが実際にアクティビティクラスにあることができます。たとえば、フラグメントはアクティビティのコンテンツビューを変更してはいけません。

断片のために使用されるコードの大半がにつながる、(あなたがfindViewByIdを使用することができ、一箇所である)onCreateViewに配置することができます...

表示するための簡単な方法はありますテキストビュー内の文字列と整数?

はい、できることはたくさんあります。たとえば、onViewCreatedメソッドのview.findViewByIdを使用して、onCreateView内で使用されるレイアウトファイルのそれぞれTextViewを取得して設定することができます。

関連する問題