我已经开始在android studio中学习android开发并正在做这个tutorial。我按照步骤复制了代码但是当我到达最后时我遇到了错误
"无法解析符号''","无法解析符号'水平'"和"无法解析符号' vertical'"在我的main.java文件中。我将添加下面的代码并评论错误发生的位置,但有人知道如何解决这个问题吗?
package com.example.fujitsu.test3;
import android.app.Activity;
import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
public class Main extends Activity implements SensorEventListener {
private float mLastX, mLastY, mLastZ;
private boolean mInitialized; private SensorManager mSensorManager; private Sensor mAccelerometer; private final float NOISE = (float) 2.0;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main); //ERROR HERE
mInitialized = false;
mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
mAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
mSensorManager.registerListener(this, mAccelerometer, SensorManager.SENSOR_DELAY_NORMAL);
}
protected void onResume() {
super.onResume();
mSensorManager.registerListener(this, mAccelerometer, SensorManager.SENSOR_DELAY_NORMAL);
}
protected void onPause() {
super.onPause();
mSensorManager.unregisterListener(this);
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
// can be safely ignored for this demo
}
@Override
public void onSensorChanged(SensorEvent event) {
TextView tvX= (TextView)findViewById(R.id.x_axis);
TextView tvY= (TextView)findViewById(R.id.y_axis);
TextView tvZ= (TextView)findViewById(R.id.z_axis);
ImageView iv = (ImageView)findViewById(R.id.image);
float x = event.values[0];
float y = event.values[1];
float z = event.values[2];
if (!mInitialized) {
mLastX = x;
mLastY = y;
mLastZ = z;
tvX.setText("0.0");
tvY.setText("0.0");
tvZ.setText("0.0");
mInitialized = true;
} else {
float deltaX = Math.abs(mLastX - x);
float deltaY = Math.abs(mLastY - y);
float deltaZ = Math.abs(mLastZ - z);
if (deltaX < NOISE) deltaX = (float)0.0;
if (deltaY < NOISE) deltaY = (float)0.0;
if (deltaZ < NOISE) deltaZ = (float)0.0;
mLastX = x;
mLastY = y;
mLastZ = z;
tvX.setText(Float.toString(deltaX));
tvY.setText(Float.toString(deltaY));
tvZ.setText(Float.toString(deltaZ));
iv.setVisibility(View.VISIBLE);
if (deltaX > deltaY) {
iv.setImageResource(R.drawable.horizontal); //ERROR HERE
} else if (deltaY > deltaX) {
iv.setImageResource(R.drawable.vertical); //ERROR HERE
} else {
iv.setVisibility(View.INVISIBLE);
}
}
}
}
答案 0 :(得分:1)
无法解析R.layout.main
表示系统希望main.xml
文件夹中存在名为res/layout
的布局。
R.drawable.*
表示您需要res/drawable
文件夹下的图像等可绘制资源。
您可能无法复制教程中的内容。那些在教程的步骤1和步骤3中提到:)做到这一点,你将成为金。
答案 1 :(得分:1)
无法解析符号&#39;水平,垂直&#39;
vertical
和horizontal
是图片。问题正在发生,因为在res/drawable-mdpi
文件夹中找不到图像。
修复res/drawable-mdpi
文件夹
无法解析符号&#39; main&#39;
main
是布局文件问题,因为在main
文件夹中找不到res/layout
创建名称为main.xml
的布局,其中包含要在Main
活动中显示的所有视图