[英]Trying to display to a view from another class
因此,當我嘗試在屏幕上顯示一些文本時,我得到一個錯誤,該錯誤將在下面發布。 該將顯示的文本假定實際上是顯示一個變量。 它將顯示玩家獲得的積分。 很久以前,我就離開了互聯網。 它可以編譯,但是出現很多LogCat錯誤。 這是我嘗試顯示文本的主要課程。
public class Main extends Activity {
DrawingView v;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
LinearLayout layout1 = new LinearLayout (this);
FrameLayout game = new FrameLayout(this);
DrawingView v = new DrawingView (this);
TextView myText = new TextView(this);
Button redCircle = new Button(this);
redCircle.setWidth(300);
redCircle.setText(DrawingView.addPoints);
layout1.addView(myText);
layout1.addView(redCircle);
redCircle.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
game.addView(myText);
game.addView(v);
game.addView(layout1);
setContentView(v);
//redCircle.setOnClickListener((OnClickListener) this);
}
public void onClick(View v) {
Intent intent = new Intent(this, Main.class);
startActivity(intent);
// re-starts this activity from game-view. add this.finish(); to remove from stack
}
@Override
public boolean onCreateOptionsMenu(Menu menu){
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
這是我得到的LogCat錯誤。 請不要對這個問題進行詳細的描述,很少有語法錯誤。 我知道您所有的互聯網巨魔,納粹將竭盡所能。
04-20 01:51:42.998: E/AndroidRuntime(3154): FATAL EXCEPTION: main
04-20 01:51:42.998: E/AndroidRuntime(3154): Process: com.Tripps.thesimplegame, PID: 3154
04-20 01:51:42.998: E/AndroidRuntime(3154): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.Tripps.thesimplegame/com.Tripps.thesimplegame.Main}: android.content.res.Resources$NotFoundException: String resource ID #0x0
04-20 01:51:42.998: E/AndroidRuntime(3154): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
04-20 01:51:42.998: E/AndroidRuntime(3154): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
04-20 01:51:42.998: E/AndroidRuntime(3154): at android.app.ActivityThread.access$800(ActivityThread.java:144)
04-20 01:51:42.998: E/AndroidRuntime(3154): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
04-20 01:51:42.998: E/AndroidRuntime(3154): at android.os.Handler.dispatchMessage(Handler.java:102)
04-20 01:51:42.998: E/AndroidRuntime(3154): at android.os.Looper.loop(Looper.java:135)
04-20 01:51:42.998: E/AndroidRuntime(3154): at android.app.ActivityThread.main(ActivityThread.java:5221)
04-20 01:51:42.998: E/AndroidRuntime(3154): at java.lang.reflect.Method.invoke(Native Method)
04-20 01:51:42.998: E/AndroidRuntime(3154): at java.lang.reflect.Method.invoke(Method.java:372)
04-20 01:51:42.998: E/AndroidRuntime(3154): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
04-20 01:51:42.998: E/AndroidRuntime(3154): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
04-20 01:51:42.998: E/AndroidRuntime(3154): Caused by: android.content.res.Resources$NotFoundException: String resource ID #0x0
04-20 01:51:42.998: E/AndroidRuntime(3154): at android.content.res.Resources.getText(Resources.java:274)
04-20 01:51:42.998: E/AndroidRuntime(3154): at android.widget.TextView.setText(TextView.java:4122)
04-20 01:51:42.998: E/AndroidRuntime(3154): at com.Tripps.thesimplegame.Main.onCreate(Main.java:38)
04-20 01:51:42.998: E/AndroidRuntime(3154): at android.app.Activity.performCreate(Activity.java:5933)
04-20 01:51:42.998: E/AndroidRuntime(3154): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
04-20 01:51:42.998: E/AndroidRuntime(3154): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
04-20 01:51:42.998: E/AndroidRuntime(3154): ... 10 more
這是另一種可能會很有幫助或令人困惑的方法
public DrawingView(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
RectF rectf = new RectF(0, 0, 200, 0);
private static final int w = 100;
public static int lastColor = Color.BLACK;
private final Random random = new Random();
private final Paint paint = new Paint();
private final int radius = 230;
private final Handler handler = new Handler();
public static int redColor = Color.RED;
public static int greenColor = Color.GREEN;
int randomWidth = 0;
int randomHeight = 0;
public static int addPoints = 0;
//TextView myTextView = new TextView(this);
//redCircle.setWidth(300);
//redCircle.setText("Start Game");
private final Runnable updateCircle = new Runnable() {
@Override
public void run() {
lastColor = random.nextInt(2) == 1 ? redColor : greenColor;
paint.setColor(lastColor);
invalidate();
handler.postDelayed(this, 1000);
}
};
@Override
protected void onAttachedToWindow() {
super.onAttachedToWindow();
handler.post(updateCircle);
}
@Override
protected void onDetachedFromWindow() {
super.onDetachedFromWindow();
handler.removeCallbacks(updateCircle);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
// your other stuff here
if(random == null){
randomWidth =(int) (random.nextInt(Math.abs(getWidth()-radius/2)) + radius/2f);
randomHeight = (random.nextInt((int)Math.abs((getHeight()-radius/2 + radius/2f))));
}else {
randomWidth =(int) (random.nextInt(Math.abs(getWidth()-radius/2)) + radius/2f);
randomHeight = (random.nextInt((int)Math.abs((getHeight()-radius/2 + radius/2f))));
}
canvas.drawCircle(randomWidth, randomHeight + radius/2f, radius, paint);
}
public boolean onTouch(View v, MotionEvent event) {
int x = (int) event.getX();
int y = (int) event.getY();
if(isInsideCircle(x, y) == true){
//Do your things here
if(redColor == lastColor){
Intent i = new Intent(v.getContext(), YouFailed.class);
v.getContext().startActivity(i);
} else {
addPoints++;
}
}else {
}
return true;
}
public boolean isInsideCircle(int x, int y){
if ((((x - randomWidth)*(x - randomWidth)) + ((y - randomHeight)*(y - randomHeight))) < ((radius)*(radius)))
return true;
return false;
}
}
錯誤在
int w = getResources().getInteger(Color.RED);
getInteger()
需要一個res
值。 Color.RED
值為0xffff0000
,在res中找不到。 因此,錯誤。
Color.RED
已經是一個整數。 您的資源值也是整數。 因此,當它嘗試查找值為Color.RED
或0xffff0000
資源時,由於沒有這樣的資源,它給出了錯誤。
從您的代碼來看,您似乎沒有使用XML來設置布局。 所以你的使用方式
int w = getResources().getInteger(Color.RED);
Button redCircle = (Button) findViewById(w);
似乎不正確。 因為您的布局沒有任何此類對象。 您應該只創建一個新按鈕。
Button redCircle = new Button(context);
並將其添加到布局中。
也,
redCircle.setText(DrawingView.addPoints);
setText()
需要String或char[]
。 如果addPoints
不是String,也應該更改它。 您還可以使用R.string.some_string
傳遞一些res
值,您應該在res / values文件夾中定義該值。 僅出於演示目的,將其更改為redCicle.setText("Red Circle");
DrawingView.addPoints
也是整數。 因此,當您傳遞redCircle.setText(DrawingView.addPoints);
,編譯器假定您正在傳遞一些res
值,因為它們是基於整數映射的。 在這里, addPoints
值似乎為0,因此,編譯器將查找未找到映射值為0
的資源,因此會android.content.res.Resources$NotFoundException: String resource ID #0x0
錯誤android.content.res.Resources$NotFoundException: String resource ID #0x0
。
TL; DR: Resource
值是基於整數映射的。 setText()
, findViewById()
, getResources().getInteger()
等等期望一個整數,該整數映射到該特定資源。 如果傳遞一些隨機整數(錯誤地),它將尋找該資源,因為編譯器很笨(也許不是),並且在找不到該資源時會引發異常。
canvas.drawText("Score: " + addPoints, 120, 300, paint);
如果其他人有此問題,這是正確的方法!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.