Android 台灣中文網
標題:
新手練習按鈕卻當掉的問題
[打印本頁]
作者:
samy_chen
時間:
2018-5-23 11:15
標題:
新手練習按鈕卻當掉的問題
大家好我是最近開始自學的新手
在練習設計按鈕的時候
一開始從書上找的讓按鈕改變Textbox 文字大小及內容是成功的
但是當我做第二個練習的時候
我要做
變色練習
按下按鈕
文字變色 然後把數值給000(紅藍綠分開)做顯示
下方的
LinearLayout用背景變色
雖然能丟到模擬器上開起來但是一但按下按鈕
就會變這樣
直接當掉了
我完全找不到方法去修正他.....
我還需要附上什麼資料嗎?
第一次使用這個討論區所以連貼圖都不會..
作者:
muska1116
時間:
2018-5-23 21:48
貼一下錯誤訊息或是程式碼來看看
作者:
samy_chen
時間:
2018-5-25 10:50
我用大寫X屏蔽掉我自己訊息喔
package com.example.XXXXXXX.linearlayout_testc001;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;
import java.util.Random;
public class MainActivity extends AppCompatActivity {
TextView RRR,GGG,BBB,toptitle;
View colorB;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RRR = (TextView) findViewById(R.id.RRR);
GGG = (TextView) findViewById(R.id.GGG);
BBB = (TextView) findViewById(R.id.BBB);
toptitle = (TextView) findViewById(R.id.toptitle);
colorB = findViewById(R.id.colorB);
}
public void setcol(View v) {
Random x = new Random();
int red = x.nextInt(256);
RRR.setText(red);
RRR.setBackgroundColor(Color.rgb(red,0,0));
int green = x.nextInt(256);
RRR.setText(green);
RRR.setBackgroundColor(Color.rgb(0,green,0));
int blue = x.nextInt(256);
RRR.setText(blue);
RRR.setBackgroundColor(Color.rgb(0,0,blue));
toptitle.setTextColor(Color.rgb(red,green,blue));
colorB.setBackgroundColor(Color.rgb(red,green,blue));
}
}
然後
程式當掉完全沒有錯誤碼
直到把AVD關掉
Emulator: qemu-system-armel.exe: Unable to open C:XXXXXXXX.androidavd4.65_720p_Galaxy_Nexus_API_21.avddatamiscpstorepstore.bin: Permission denied
作者:
muska1116
時間:
2018-5-25 23:54
問題出在下面這三行,
RRR.setText(red);
RRR.setText(green);
RRR.setText(blue);
setText 如果你希望他顯示 int,你應該要用
RRR.setText(
String.valueOf(
red
)
);
RRR.setText(
String.valueOf
(
green
)
);
RRR.setText(
String.valueOf(
blue
)
);
如果你直接傳數字進去他會當成是 resource id,然後因為找不到對應的 resource 就會出現
Caused by: android.content.res.Resources$NotFoundException
而造成閃退…
還有你應該本來是要分別改變 RRR GGG BBB 的顏色的,只是複製完忘了改吧XD
RRR.setText(red);
GGG
.setText(green);
BBB
.setText(blue);
作者:
samy_chen
時間:
2018-5-26 13:32
本帖最後由 samy_chen 於 2018-5-26 13:43 編輯
lol...下面那幾行....還真的忘記改
謝謝前輩
那我再問一下
我這邊的書上
他是
Random x = new Random();
int red = x.nextInt(256);
RRR.setText("紅"+red);
改Textview背景色是我自己加的XD
其中
RRR.setText("紅"+red);
也沒有對red做String.valueOf()那麼為啥書上會這樣寫?
RRR在書上是別的id沒那麼智障但順手代換不是問題吧XD
作者:
muska1116
時間:
2018-5-26 13:43
在 java 裡面,String 與 int 相加,int 會自動轉型成 String,
所以除了你可以寫 RRR.setText(
String.valueOf(red)
) 以外,
你也可以寫成 RRR.setText(
red + ""
)
歡迎光臨 Android 台灣中文網 (https://apk.tw/)
Powered by Discuz! X3.1