尝试在RecyclerView
中列出加速度计x,y,z数据。以下是我的代码
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
senSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
senAccelerometer = senSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
senSensorManager.registerListener(this, senAccelerometer, SensorManager.SENSOR_DELAY_NORMAL);
RecyclerView rv = (RecyclerView) findViewById(R.id.rv); // layout reference
LinearLayoutManager llm = new LinearLayoutManager(this);
llm.setOrientation(LinearLayoutManager.VERTICAL);
rv.setLayoutManager(llm);
// rv.setHasFixedSize(true); // to improve performance
rv.setAdapter(dataAdapter = new DataManager()); // the data manager is assigner to the RV
rv.addOnItemTouchListener( // and the click is handled
new RecyclerClickListener(this, new RecyclerClickListener.MyOnItemClickListener() {
@Override public void onItemClick(View view, int position) {
// STUB:
// The click on the item must be handled
}
}));
}
DataManager
类存储数据
public class DataManager extends RecyclerView.Adapter<DataManager.RecyclerViewHolder>
{
ArrayList<AccelData> myDataStore = new ArrayList<AccelData>();
public ArrayList<AccelData> getMyDataStore() {
return myDataStore;
}
public static class RecyclerViewHolder extends RecyclerView.ViewHolder
{
TextView content;
RecyclerViewHolder(View itemView)
{
super(itemView);
content = (TextView)itemView.findViewById(R.id.textView);
}
}
@Override
public RecyclerViewHolder onCreateViewHolder(ViewGroup viewGroup, int i)
{
View v= LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.row_element ,viewGroup,false);
return new RecyclerViewHolder(v);
}
@Override public void onBindViewHolder(RecyclerViewHolder viewHolder,int i)
{
viewHolder.content.setText("X = "+myDataStore.get(i).getX()+" ; Y = "+myDataStore.get(i).getY()+" ; Z = "+myDataStore.get(i).getZ());
}
@Override
public int getItemCount()
{
return myDataStore.size();
}
static class AccelData {
private double x,y,z;
AccelData(double x,double y, double z){
this.x = x;
this.y = y;
this.z = z;
}
public void setX(double x) {
this.x = x;
}
public void setY(double y) {
this.y = y;
}
public void setZ(double z) {
this.z = z;
}
public double getX() {
return x;
}
public double getY() {
return y;
}
public double getZ() {
return z;
}
}
}
onSensorChanged数据填充如下
@Override
public void onSensorChanged(SensorEvent sensorEvent)
{
Sensor mySensor = sensorEvent.sensor;
if(mySensor.getType() == Sensor.TYPE_ACCELEROMETER)
{
float x = sensorEvent.values[0];
float y = sensorEvent.values[1];
float z = sensorEvent.values[2];
Log.e("changed",+x+":"+y+":"+z);
long curTime = System.currentTimeMillis();
if(curTime-lastUpdate>100)
{
diffTime = (curTime-lastUpdate);
lastUpdate = curTime;
}
float speed = Math.abs(x + y + z - last_x - last_y - last_z)/ diffTime * 10000;
if(speed > SHAKE_THRESHOLD)
{
dataAdapter.getMyDataStore().add(new DataManager.AccelData(x,y,z));
dataAdapter.notifyItemInserted(dataAdapter.getMyDataStore().size()-1);
// dataAdapter.notifyDataSetChanged();
}
last_x = x;
last_y = y;
last_z = z;
}
}
这是日志
04-21 10:04:03.818: E/changed(11766): -507.006:-586.466:-476.383
04-21 10:04:03.912: I/Choreographer(11766): Skipped 63 frames! The application may be doing too much work on its main thread.
04-21 10:04:03.972: I/Choreographer(11766): Skipped 64 frames! The application may be doing too much work on its main thread.
04-21 10:04:04.620: E/changed(11766): -60.9322:-408.35:-580.506
04-21 10:04:05.067: I/Choreographer(11766): Skipped 94 frames! The application may be doing too much work on its main thread.
04-21 10:04:05.156: I/Choreographer(11766): Skipped 97 frames! The application may be doing too much work on its main thread.
04-21 10:04:05.247: I/Choreographer(11766): Skipped 101 frames! The application may be doing too much work on its main thread.
04-21 10:04:05.280: E/InputEventReceiver(11766): Exception dispatching input event.
04-21 10:04:05.299: D/AndroidRuntime(11766): Shutting down VM
04-21 10:04:05.358: E/AndroidRuntime(11766): FATAL EXCEPTION: main
04-21 10:04:05.358: E/AndroidRuntime(11766): Process: com.example.nagarajan.myapplication, PID: 11766
04-21 10:04:05.358: E/AndroidRuntime(11766): java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object android.support.v4.view.AccessibilityDelegateCompat.getBridge()' on a null object reference
04-21 10:04:05.358: E/AndroidRuntime(11766): at android.support.v4.view.ViewCompat$ICSViewCompatImpl.setAccessibilityDelegate(ViewCompat.java:923)
04-21 10:04:05.358: E/AndroidRuntime(11766): at android.support.v4.view.ViewCompat.setAccessibilityDelegate(ViewCompat.java:1343)
04-21 10:04:05.358: E/AndroidRuntime(11766): at android.support.v7.widget.RecyclerView$Recycler.addViewHolderToRecycledViewPool(RecyclerView.java:3900)
04-21 10:04:05.358: E/AndroidRuntime(11766): at android.support.v7.widget.RecyclerView$Recycler.recycleCachedViewAt(RecyclerView.java:3843)
04-21 10:04:05.358: E/AndroidRuntime(11766): at android.support.v7.widget.RecyclerView$Recycler.recycleViewHolderInternal(RecyclerView.java:3880)
04-21 10:04:05.358: E/AndroidRuntime(11766): at android.support.v7.widget.RecyclerView$Recycler.recycleView(RecyclerView.java:3804)
04-21 10:04:05.358: E/AndroidRuntime(11766): at android.support.v7.widget.RecyclerView$LayoutManager.removeAndRecycleViewAt(RecyclerView.java:5529)
04-21 10:04:05.358: E/AndroidRuntime(11766): at android.support.v7.widget.LinearLayoutManager.recycleChildren(LinearLayoutManager.java:1139)
04-21 10:04:05.358: E/AndroidRuntime(11766): at android.support.v7.widget.LinearLayoutManager.recycleViewsFromStart(LinearLayoutManager.java:1180)
04-21 10:04:05.358: E/AndroidRuntime(11766): at android.support.v7.widget.LinearLayoutManager.recycleByLayoutState(LinearLayoutManager.java:1244)
04-21 10:04:05.358: E/AndroidRuntime(11766): at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1268)
04-21 10:04:05.358: E/AndroidRuntime(11766): at android.support.v7.widget.LinearLayoutManager.scrollBy(LinearLayoutManager.java:1102)
04-21 10:04:05.358: E/AndroidRuntime(11766): at android.support.v7.widget.LinearLayoutManager.scrollVerticallyBy(LinearLayoutManager.java:959)
04-21 10:04:05.358: E/AndroidRuntime(11766): at android.support.v7.widget.RecyclerView.scrollByInternal(RecyclerView.java:1047)
04-21 10:04:05.358: E/AndroidRuntime(11766): at android.support.v7.widget.RecyclerView.onTouchEvent(RecyclerView.java:1768)
04-21 10:04:05.358: E/AndroidRuntime(11766): at android.view.View.dispatchTouchEvent(View.java:8388)
04-21 10:04:05.358: E/AndroidRuntime(11766): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2424)
04-21 10:04:05.358: E/AndroidRuntime(11766): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2158)
04-21 10:04:05.358: E/AndroidRuntime(11766): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2430)
04-21 10:04:05.358: E/AndroidRuntime(11766): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2172)
04-21 10:04:05.358: E/AndroidRuntime(11766): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2430)
04-21 10:04:05.358: E/AndroidRuntime(11766): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2172)
04-21 10:04:05.358: E/AndroidRuntime(11766): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2430)
04-21 10:04:05.358: E/AndroidRuntime(11766): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2172)
04-21 10:04:05.358: E/AndroidRuntime(11766): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2430)
04-21 10:04:05.358: E/AndroidRuntime(11766): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2172)
04-21 10:04:05.358: E/AndroidRuntime(11766): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2430)
04-21 10:04:05.358: E/AndroidRuntime(11766): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2172)
04-21 10:04:05.358: E/AndroidRuntime(11766): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2430)
04-21 10:04:05.358: E/AndroidRuntime(11766): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2172)
04-21 10:04:05.358: E/AndroidRuntime(11766): at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:2314)
04-21 10:04:05.358: E/AndroidRuntime(11766): at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1692)
04-21 10:04:05.358: E/AndroidRuntime(11766): at android.app.Activity.dispatchTouchEvent(Activity.java:2739)
04-21 10:04:05.358: E/AndroidRuntime(11766): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:2275)
04-21 10:04:05.358: E/AndroidRuntime(11766): at android.view.View.dispatchPointerEvent(View.java:8578)
04-21 10:04:05.358: E/AndroidRuntime(11766): at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:4021)
04-21 10:04:05.358: E/AndroidRuntime(11766): at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:3887)
04-21 10:04:05.358: E/AndroidRuntime(11766): at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3449)
04-21 10:04:05.358: E/AndroidRuntime(11766): at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3502)
04-21 10:04:05.358: E/AndroidRuntime(11766): at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3468)
04-21 10:04:05.358: E/AndroidRuntime(11766): at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3578)
04-21 10:04:05.358: E/AndroidRuntime(11766): at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3476)
04-21 10:04:05.358: E/AndroidRuntime(11766): at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:3635)
04-21 10:04:05.358: E/AndroidRuntime(11766): at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3449)
04-21 10:04:05.358: E/AndroidRuntime(11766): at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3502)
04-21 10:04:05.358: E/AndroidRuntime(11766): at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3468)