出来。main不能解析为变量

时间:2021-03-14 20:52:39

I've found this code from Raghav Sood's Pro Android Augmented Reality book. I've tried to execute the code as it is written in the book but yet i am facing so much of difficulties.i've tried cleaning and building of project. Put the checkbox tick in the project's property dialogue box. Checked import Android.R presence in my code. Even i uninstalled my AndroidSDK and reinstalled it with different set of Android versions and APIs. Uninstalled and reinstalled Eclipse.

我从Raghav Sood的Pro Android增强现实书中找到了这段代码。我已经尝试执行书中所写的代码,但是我仍然面临着很多困难。我尝试过清洁和建设项目。将复选框在项目的属性对话框中勾选。检查导入安卓。R出现在我的代码中。甚至我也卸载了Android sdk,并重新安装了不同的Android版本和api集。卸载和重新安装Eclipse。

Here is my ProAndroidAR3Activity.java file

这是我的ProAndroidAR3Activity。java文件

    package com.paar.ch3widgetoverlay;

    import android.app.Activity;
    import android.hardware.Camera;
    import android.hardware.Sensor;
    import android.hardware.SensorEvent;
    import android.hardware.SensorEventListener;
    import android.hardware.SensorManager;
    import android.location.Location;
    import android.location.LocationListener;
    import android.location.LocationManager;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.SurfaceHolder;
    import android.view.SurfaceView;
    import android.widget.TextView;

    public class ProAndroidAR3Activity extends Activity{
SurfaceView cameraPreview;
SurfaceHolder previewHolder;
Camera camera;
boolean inPreview;

final static String TAG = "PAAR";   
SensorManager sensorManager;

int orientationSensor;
float headingAngle;
float pitchAngle;
float rollAngle;

int accelerometerSensor;
float xAxis;
float yAxis;
float zAxis;

LocationManager locationManager;
double latitude;
double longitude;
double altitude;

TextView xAxisValue;
TextView yAxisValue;
TextView zAxisValue;
TextView headingValue;
TextView pitchValue;
TextView rollValue;
TextView altitudeValue;
TextView latitudeValue;
TextView longitudeValue;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main); //here is the problem. every part of the code with       R. is having problem


    locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 2000, 2, locationListener);

    sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
    orientationSensor = Sensor.TYPE_ORIENTATION;
    accelerometerSensor = Sensor.TYPE_ACCELEROMETER;
    sensorManager.registerListener(sensorEventListener, sensorManager.getDefaultSensor(orientationSensor), SensorManager.SENSOR_DELAY_NORMAL);
    sensorManager.registerListener(sensorEventListener, sensorManager.getDefaultSensor(accelerometerSensor), SensorManager.SENSOR_DELAY_NORMAL);

    inPreview = false;

    cameraPreview = (SurfaceView)findViewById(R.id.cameraPreview);
    previewHolder = cameraPreview.getHolder();
    previewHolder.addCallback(surfaceCallback);
    previewHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

    xAxisValue = (TextView) findViewById(R.id.xAxisValue);
    yAxisValue = (TextView) findViewById(R.id.yAxisValue);
    zAxisValue = (TextView) findViewById(R.id.zAxisValue);
    headingValue = (TextView) findViewById(R.id.headingValue);
    pitchValue = (TextView) findViewById(R.id.pitchValue);
    rollValue = (TextView) findViewById(R.id.rollValue);
    altitudeValue = (TextView) findViewById(R.id.altitudeValue);
    longitudeValue = (TextView) findViewById(R.id.longitudeValue);
    latitudeValue = (TextView) findViewById(R.id.latitudeValue);

This is the AndroidManifest.xml file of my project:

这是AndroidManifest。项目xml文件:

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.paar.ch3widgetoverlay"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="7" />

    <application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name" >
    <activity
        android:label="@string/app_name"
        android:name=".ProAndroidAR3Activity" 
        android:screenOrientation = "landscape"
        android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
        android:configChanges = "keyboardHidden|orientation">
        <intent-filter >
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    </application>
    <uses-feature android:name="android.hardware.camera" />  
    <uses-permission android:name="android.permission.CAMERA" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    </manifest>

The following is the res/layout/main.xml file

下面是res/layout/main。xml文件

    <RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/relativeLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<SurfaceView
    android:id="@+id/cameraPreview"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" />

<TextView
    android:id="@+id/xAxisLabel"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_marginLeft="18dp"
    android:layout_marginTop="15dp"
    android:text="@string/xAxis" />

<TextView
    android:id="@+id/yAxisLabel"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/xAxisLabel"
    android:layout_below="@+id/xAxisLabel"
    android:text="@string/yAxis" />

<TextView
    android:id="@+id/zAxisLabel"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/yAxisLabel"
    android:layout_below="@+id/yAxisLabel"
    android:text="@string/zAxis" />

<TextView
    android:id="@+id/headingLabel"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/zAxisLabel"
    android:layout_below="@+id/zAxisLabel"
    android:layout_marginTop="19dp"
    android:text="@string/heading" />

<TextView
    android:id="@+id/pitchLabel"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/headingLabel"
    android:layout_below="@+id/headingLabel"
    android:text="@string/pitch" />

<TextView
    android:id="@+id/rollLabel"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/pitchLabel"
    android:layout_below="@+id/pitchLabel"
    android:text="@string/roll" />

<TextView
    android:id="@+id/latitudeLabel"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/rollLabel"
    android:layout_below="@+id/rollLabel"
    android:layout_marginTop="34dp"
    android:text="@string/latitude" />

<TextView
    android:id="@+id/longitudeLabel"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/latitudeLabel"
    android:layout_below="@+id/latitudeLabel"
    android:text="@string/longitude" />

<TextView
    android:id="@+id/altitudeLabel"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/longitudeLabel"
    android:layout_below="@+id/longitudeLabel"
    android:text="@string/altitude" />

<TextView
    android:id="@+id/xAxisValue"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/xAxisLabel"
    android:layout_marginLeft="56dp"
    android:layout_toRightOf="@+id/longitudeLabel"
    android:text="@string/empty" />

<TextView
    android:id="@+id/yAxisValue"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/yAxisLabel"
    android:layout_alignBottom="@+id/yAxisLabel"
    android:layout_alignLeft="@+id/xAxisValue"
    android:text="@string/empty" />

<TextView
    android:id="@+id/zAxisValue"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/headingLabel"
    android:layout_alignLeft="@+id/yAxisValue"
    android:text="@string/empty" />

<TextView
    android:id="@+id/headingValue"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/headingLabel"
    android:layout_alignBottom="@+id/headingLabel"
    android:layout_alignLeft="@+id/zAxisValue"
    android:text="@string/empty" />

<TextView
    android:id="@+id/pitchValue"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/pitchLabel"
    android:layout_alignBottom="@+id/pitchLabel"
    android:layout_alignLeft="@+id/headingValue"
    android:text="@string/empty" />

<TextView
    android:id="@+id/rollValue"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/latitudeLabel"
    android:layout_alignLeft="@+id/pitchValue"
    android:text="@string/empty" />

<TextView
    android:id="@+id/latitudeValue"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/latitudeLabel"
    android:layout_alignLeft="@+id/rollValue"
    android:text="@string/empty" />

<TextView
    android:id="@+id/longitudeValue"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/longitudeLabel"
    android:layout_alignBottom="@+id/longitudeLabel"
    android:layout_alignLeft="@+id/latitudeValue"
    android:text="@string/empty" />

<TextView
    android:id="@+id/altitudeValue"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/altitudeLabel"
    android:layout_alignBottom="@+id/altitudeLabel"
    android:layout_alignLeft="@+id/longitudeValue"
    android:text="@string/empty" />

    </RelativeLayout>`

The following is the res/values/strings.xml file

下面是res/values/string。xml文件

    <?xml version="1.0" encoding="utf-8"?>
    <resources>

    <string name="hello">Hello World, ProAndroidAR3Activity!</string>
    <string name="app_name">Pro Android AR 3 Widget Overlay</string>
    <string name="xAxis">X Axis:</string>
    <string name="yAxis">Y Axis:</string>
<string name="zAxis">Z Axis:</string>
<string name="heading">Heading:</string>
<string name="pitch">Pitch:</string>
<string name="roll">Roll:</string>
<string name="altitude">Altitude:</string>
<string name="longitude">Longitude:</string>
<string name="latitude">Latitude:</string>
<string name="empty"></string>
    </resources>

I have set the minsdk version to be 2.1 that is API 7 and the compile SDK version is 4.1 that is API 16...

我将minsdk版本设置为2.1,即API 7,而compile SDK版本为4.1,即API 16…

please help me out...

请帮帮我…

4 个解决方案

#1


1  

Do you have your properly generated fresh R.java class under the "gen" folder in Eclipse (/gen/[yourpackage]/R.java)? If it is in the same package as your activity, you do not need to import it manually!

你有合适的新鲜R吗?Eclipse中“gen”文件夹下的java类(/gen/[yourpackage]/R.java)?如果它与您的活动在同一个包中,您不需要手动导入它!

It should be there if the XMLs are correct! If there is a syntax error in the XMLs, then Eclipse will not genereate the R.java.

如果xml是正确的,它应该在那里!如果xml中有语法错误,那么Eclipse将不会生成R.java。

Besieds, there is a strange '`' character at the end of your layout file, I don't know if it is accident in your post, but this can be a problem:

除此之外,在你的布局文件的末尾有一个奇怪的“”字符,我不知道这是不是你文章中的意外,但这可能是个问题:

</RelativeLayout>`

I hope it helps!

我希望它可以帮助!

#2


1  

(Expanding on user1649030's post because this is too big to be a comment:)

(扩展到user1649030的贴子上,因为这个太大了,不适合评论:)

OK, there are generally two causes for the 'R not found' error. One is an error in any of your xml files which cause R.java to not build at all. Look under your 'gen' directory for R.java. If not found, then that's your problem. Look for bad xml files.

通常有两个原因导致“R未找到”错误。一个是导致R的xml文件中的一个错误。java根本无法构建。在“gen”目录下查找R.java。如果找不到,那就是你的问题。查找糟糕的xml文件。

If R.java is being built, note what the directory path to it is. This will match the package name specified in your manifest. In your case, it would be gen/com/paar/ch3widgetoverlay/R.java.

如果R。正在构建java,请注意它的目录路径。这将匹配您的清单中指定的包名。在您的例子中,应该是gen/com/paar/ch3widgetoverlay/R.java。

This class is implicitly imported by all source files in your src/com/paar/ch3widgetoverlay/

这个类由src/com/paar/ch3widgetoverlay/中的所有源文件隐式导入

Any source files in any other directory which need to access R will need to explicitly import com.paar.ch3widgetoverlay.R

需要访问R的任何其他目录中的任何源文件都需要显式地导入com.paar.ch3widgetoverlay.R

That should pretty much cover it.

这几乎可以涵盖它。

#3


0  

Try importing com.paar.ch3widgetoverlay.R

尝试导入com.paar.ch3widgetoverlay.R

#4


0  

You need to import your R class from java, not Android's. If you import android.R it will cause conflicts. The R class is generated and may or may not be in the same package as ch3widgetoverlay especially if you got the code from a tutorial. Look in the gen folder in eclipse to find out what package the R class is in.

您需要从java导入R类,而不是从Android导入。如果你导入安卓。它会引起冲突。生成R类,并且可能与ch3widgetoverlay位于同一个包中,也可能不在同一个包中,特别是如果您从教程中获得代码的话。查看eclipse中的gen文件夹,找出R类所在的包。

#1


1  

Do you have your properly generated fresh R.java class under the "gen" folder in Eclipse (/gen/[yourpackage]/R.java)? If it is in the same package as your activity, you do not need to import it manually!

你有合适的新鲜R吗?Eclipse中“gen”文件夹下的java类(/gen/[yourpackage]/R.java)?如果它与您的活动在同一个包中,您不需要手动导入它!

It should be there if the XMLs are correct! If there is a syntax error in the XMLs, then Eclipse will not genereate the R.java.

如果xml是正确的,它应该在那里!如果xml中有语法错误,那么Eclipse将不会生成R.java。

Besieds, there is a strange '`' character at the end of your layout file, I don't know if it is accident in your post, but this can be a problem:

除此之外,在你的布局文件的末尾有一个奇怪的“”字符,我不知道这是不是你文章中的意外,但这可能是个问题:

</RelativeLayout>`

I hope it helps!

我希望它可以帮助!

#2


1  

(Expanding on user1649030's post because this is too big to be a comment:)

(扩展到user1649030的贴子上,因为这个太大了,不适合评论:)

OK, there are generally two causes for the 'R not found' error. One is an error in any of your xml files which cause R.java to not build at all. Look under your 'gen' directory for R.java. If not found, then that's your problem. Look for bad xml files.

通常有两个原因导致“R未找到”错误。一个是导致R的xml文件中的一个错误。java根本无法构建。在“gen”目录下查找R.java。如果找不到,那就是你的问题。查找糟糕的xml文件。

If R.java is being built, note what the directory path to it is. This will match the package name specified in your manifest. In your case, it would be gen/com/paar/ch3widgetoverlay/R.java.

如果R。正在构建java,请注意它的目录路径。这将匹配您的清单中指定的包名。在您的例子中,应该是gen/com/paar/ch3widgetoverlay/R.java。

This class is implicitly imported by all source files in your src/com/paar/ch3widgetoverlay/

这个类由src/com/paar/ch3widgetoverlay/中的所有源文件隐式导入

Any source files in any other directory which need to access R will need to explicitly import com.paar.ch3widgetoverlay.R

需要访问R的任何其他目录中的任何源文件都需要显式地导入com.paar.ch3widgetoverlay.R

That should pretty much cover it.

这几乎可以涵盖它。

#3


0  

Try importing com.paar.ch3widgetoverlay.R

尝试导入com.paar.ch3widgetoverlay.R

#4


0  

You need to import your R class from java, not Android's. If you import android.R it will cause conflicts. The R class is generated and may or may not be in the same package as ch3widgetoverlay especially if you got the code from a tutorial. Look in the gen folder in eclipse to find out what package the R class is in.

您需要从java导入R类,而不是从Android导入。如果你导入安卓。它会引起冲突。生成R类,并且可能与ch3widgetoverlay位于同一个包中,也可能不在同一个包中,特别是如果您从教程中获得代码的话。查看eclipse中的gen文件夹,找出R类所在的包。