簡體   English   中英

Eclipse for android:R 問題

[英]Eclipse for android: Issues with R

我正在嘗試做一個定位應用程序,現在正處於故障排除階段。 我歸結為最后兩個問題,它們都與 R. blah blah 有關。 它說 main 要么不是字段,要么無法解析,並且顯示相同的消息,但使用 mapview 而不是 main。 以下是我的導入和問題所在的代碼行。

代碼:

    setContentView(R.layout.main);
    mapview = (MapView)findViewById(R.id.mapview);

進口:

    import android.R;
    import android.R.layout;
    import android.R.id;
    import android.location.Location;
    import android.location.LocationListener;
    import android.location.LocationManager;
    import android.os.Bundle;
    import android.widget.Toast;

    import com.google.android.maps.GeoPoint;
    import com.google.android.maps.MapActivity;
    import com.google.android.maps.MapController;
    import com.google.android.maps.MapView;

我什么都試過了。 我知道人們一直說要刪除 android.R 但這就是在 jar 文件中找到 R.layout 和 R.id 的地方。 我已右鍵單擊該文件夾並驗證了代碼。 我已經去了 Project/Clean 並做到了。 沒運氣。 請幫忙? 這是我的第一篇文章,所以如果格式錯誤,我很抱歉。 我預先感謝您為我提供的任何幫助。 這是我的 main.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        >
    <TextView android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Waiting for location..."
        android:id="@+id/lblLocationInfo"
    />
    <com.google.android.maps.MapView
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/mapview"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:clickable="true"
        android:apiKey="0vFrUOhHMkbahT9zXqiz_DuNVWfPqlEyqcO8ftg"
    />
    </LinearLayout>

嘗試刪除該行:

import android.R;

然后做Build Clean,這可以解決問題。

除非您使用來自 android 的資源,否則您不需要這些行

import android.R; 
import android.R.Id;
import android.R.layout;

刪除那些行

R 正在解析為 androids R 而不是com.yourpackage.yourproject.R

如果不需要,請刪除導入。 如果您需要在代碼中明確表示。 前任。

ImageView.setDrawable(android.R.ic_menu_search); //uses an android resource

findViewById(R.id.mapView); //uses your resource file

setContentView(R.layout.activity_main); // must have an activity_main.xml file in YourProject/res/layout

編輯:

更改以下代碼:

//import android.R;
//import android.R.layout;    //comment or delete these lines
//import android.R.id;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.widget.Toast;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;

setContentView(R.layout.activity_main);  //changed main to activity_main
mapview = (MapView)findViewById(R.id.mapview);

編輯 2

在 MainActivity.java 的最頂部,它應該有一行以package開頭,該行的其余部分是什么意思?

在您的 AndroidManifest.xml 中還有一個 package 條目,它說的是什么?

R.java它是一個自生成的文件,您需要重新構建它才能生成它,忽略警告並“構建清理”您的文件

這也可能與您的項目設置有關。 創建新項目時,我通常會設置“訂購和導出”選項。 這樣您就不必導入任何 R.* 包。

在 Eclipse 中,右鍵單擊您的項目,轉到Properties -> Java Build Path -> Order and Export 只需確保 yourprojectname/gen 文件夾位於 yourprojectname/src 文件夾上方

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM