我一直在使用Eclipse并导入了一个旧项目,但是出现了以下错误:
R无法解析为变量
所以我添加了:import android.R;现在我得到了所有这些错误:
主要无法解决或不是一个领域 主题无法解决或不是一个领域 IVDisplay无法解析或不是字段 IVimage1无法解析或不是字段 bSetWallpaper无法解析或不是字段
请你告诉我我做错了什么。
爪哇:
package com.vamp6x6x6x.rusty;
import java.io.IOException;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
public class rustyactivity extends Activity implements MediaPlayer.OnCompletionListener, View.OnClickListener {
/** Called when the activity is first created. */
ImageView display;
int toPhone;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.main);
Button ending = (Button) findViewById(R.id.theme);
ending.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
playSound(R.raw.theme);
}
});
display = (ImageView) findViewById(R.id.IVDisplay);
ImageView image1 = (ImageView) findViewById(R.id.IVimage1);
ImageView image2 = (ImageView) findViewById(R.id.IVimage2);
ImageView image3 = (ImageView) findViewById(R.id.IVimage3);
ImageView image4 = (ImageView) findViewById(R.id.IVimage4);
ImageView image5 = (ImageView) findViewById(R.id.IVimage5);
Button setWall = (Button) findViewById(R.id.bSetWallpaper);
toPhone = R.drawable.guy1;
image1.setOnClickListener(this);
image2.setOnClickListener(this);
image3.setOnClickListener(this);
image4.setOnClickListener(this);
image5.setOnClickListener(this);
setWall.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.IVimage1:
display.setImageResource(R.drawable.guy1);
toPhone = R.drawable.guy1;
break;
case R.id.IVimage2:
display.setImageResource(R.drawable.guy2);
toPhone = R.drawable.guy2;
break;
case R.id.IVimage3:
display.setImageResource(R.drawable.guy3);
toPhone = R.drawable.guy3;
break;
case R.id.IVimage4:
display.setImageResource(R.drawable.guy4);
toPhone = R.drawable.guy4;
break;
case R.id.IVimage5:
display.setImageResource(R.drawable.guy5);
toPhone = R.drawable.guy5;
break;
case R.id.bSetWallpaper:
Bitmap whatever = BitmapFactory.decodeStream(getResources().openRawResource(toPhone));
try{
getApplicationContext().setWallpaper(whatever);
}catch(IOException e){
e.printStackTrace();
}
}
}
private void playSound(int resId) {
MediaPlayer mp = MediaPlayer.create(this, resId);
mp.setOnCompletionListener(this);
mp.start();
}
@Override
public void onCompletion(MediaPlayer mp) {
// TODO Auto-generated method stub
}
}
main.xml中:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:myapp="http://schemas.android.com/apk/res/com.vamp6x6x6x.rusty"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ImageView android:id="@+id/IVDisplay" android:src="@drawable/guy1" android:layout_width="200dp" android:layout_height="200dp" android:layout_gravity="center"></ImageView>
<Button android:text="Set Wallpaper" android:id="@+id/bSetWallpaper"
android:layout_width="fill_parent" android:layout_height="wrap_content"></Button>
<HorizontalScrollView android:layout_width="250dp" android:layout_height="wrap_content" android:layout_gravity="center">
<LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal">
<ImageView android:id="@+id/IVimage1"
android:src="@drawable/guy1"
android:layout_width="125dp" android:layout_height="125dp" android:padding="15dp"></ImageView>
<ImageView android:id="@+id/IVimage2"
android:src="@drawable/guy2"
android:layout_width="125dp" android:layout_height="125dp" android:padding="15dp"></ImageView>
<ImageView android:id="@+id/IVimage3"
android:src="@drawable/guy3"
android:layout_width="125dp" android:layout_height="125dp" android:padding="15dp"></ImageView>
<ImageView android:id="@+id/IVimage4"
android:src="@drawable/guy4"
android:layout_width="125dp" android:layout_height="125dp" android:padding="15dp"></ImageView>
<ImageView android:id="@+id/IVimage5"
android:src="@drawable/guy5"
android:layout_width="125dp" android:layout_height="125dp" android:padding="15dp"></ImageView>
</LinearLayout>
</HorizontalScrollView>
<Button android:layout_width="wrap_content" android:layout_gravity="center" android:id="@+id/theme" android:layout_height="wrap_content" android:text="Big Guy and Rusty the Boy Robot"></Button>
</LinearLayout>
答案 0 :(得分:1)
删除Android.r并执行清理然后构建。
答案 1 :(得分:1)
当您的某些资源(xmls,图像等)出现问题时,Eclipse通常无法生成R文件。
缺少的R不是android.R,而是映射项目资源的R文件。
您应该查看您的控制台(window-&gt; show view-&gt;控制台)并查看是否收到错误,如果您什么都看不到,则应该在DDMS透视图中查看LogCat日志
答案 2 :(得分:1)