Java Android:RNG错误和按钮主题
我有我的随机数生成器,当我在'numberTo'字符串中键入0并给我int:“0”时,它崩溃了。由于TextWatcher的存在,我无法删除editText并显示提示。请帮帮我!伙计们,我怎么加一个不同颜色的按钮,你能解释一下吗 这是我的代码: 活动\u main.xmlJava Android:RNG错误和按钮主题,java,android,android-studio,Java,Android,Android Studio,我有我的随机数生成器,当我在'numberTo'字符串中键入0并给我int:“0”时,它崩溃了。由于TextWatcher的存在,我无法删除editText并显示提示。请帮帮我!伙计们,我怎么加一个不同颜色的按钮,你能解释一下吗 这是我的代码: 活动\u main.xml <android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" xml
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="bedi.gursimran.materialtest.MainActivity">
<include
android:id="@+id/app_bar"
layout="@layout/app_bar" />
<TextView
android:textSize="68dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/random_text"
android:text="Number"
android:layout_marginTop="12dp"
android:layout_centerVertical="true"
android:layout_centerHorizontal="false"
android:layout_marginLeft="50dp"
/>
<EditText
android:layout_width="140dp"
android:layout_height="wrap_content"
android:inputType="number"
android:padding="12dp"
android:ems="10"
android:id="@+id/text_from"
android:maxLength="6"
android:hint="From"
android:layout_below="@+id/app_bar"
android:layout_alignParentStart="true"
android:layout_marginLeft="10dp"
android:layout_marginTop="24dp"
android:backgroundTint="@color/primaryColorDark"
android:textSize="@dimen/edit_text_size"/>
<EditText
android:layout_width="140dp"
android:layout_height="wrap_content"
android:inputType="number"
android:ems="10"
android:padding="12dp"
android:id="@+id/text_to"
android:layout_below="@+id/text_from"
android:layout_alignParentStart="true"
android:maxLength="8"
android:hint="To"
android:backgroundTint="@color/primaryColorDark"
android:layout_marginTop="8dp"
android:layout_marginLeft="10dp"
android:textSize="@dimen/edit_text_size"/>
<Button
android:layout_width="140dp"
android:layout_height="70dp"
android:text="Generate"
android:id="@+id/generate_button"
android:onClick="generate"
android:layout_below="@+id/app_bar"
android:layout_marginLeft="190dp"
android:layout_marginTop="51dp"
android:textColor="#fffafafa"
android:textSize="22dp"
android:backgroundTint="@color/accentColor"
/>
</RelativeLayout>
</android.support.v4.widget.DrawerLayout>
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="AppTheme.Base">
<!-- Customize your theme here. -->
</style>
<!--Base theme-->
<style name="AppTheme.Base" parent="Theme.AppCompat.Light.NoActionBar" >
<item name="colorPrimary">@color/primaryColor</item>
<item name="colorPrimaryDark">@color/primaryColorDark</item>
<item name="colorAccent">@color/accentColor</item>
<!-- Generate Button Color -->
<item name="android:colorButtonNormal">@color/primaryColor</item>
</style>
<!--Action Bar theme-->
<style name="MyCustomToolBarTheme" parent="ThemeOverlay.AppCompat.Light">
<!--The title-->
<item name="android:textColorPrimary">#FFFFFF</item>
<item name="android:textColor">#FFFFFF</item>
<!--The 'overflow icon-->
<item name="android:textColorSecondary">#FFFFFF</item>
</style>
<style name="ActionBarPopupThemeOverlay" parent="ThemeOverlay.AppCompat.Light" >
<item name="android:background">@android:color/white</item>
<item name="android:textColor">#de000000</item>
</style>
</resources>
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="56dp"
android:background="@color/primaryColor"
app:theme="@style/MyCustomToolBarTheme"
android:paddingTop="@dimen/app_bar_top_padding"
android:elevation="@dimen/action_bar_elevation"
app:popupTheme="@style/ActionBarPopupThemeOverlay"
>
</android.support.v7.widget.Toolbar>
styles.xml
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="bedi.gursimran.materialtest.MainActivity">
<include
android:id="@+id/app_bar"
layout="@layout/app_bar" />
<TextView
android:textSize="68dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/random_text"
android:text="Number"
android:layout_marginTop="12dp"
android:layout_centerVertical="true"
android:layout_centerHorizontal="false"
android:layout_marginLeft="50dp"
/>
<EditText
android:layout_width="140dp"
android:layout_height="wrap_content"
android:inputType="number"
android:padding="12dp"
android:ems="10"
android:id="@+id/text_from"
android:maxLength="6"
android:hint="From"
android:layout_below="@+id/app_bar"
android:layout_alignParentStart="true"
android:layout_marginLeft="10dp"
android:layout_marginTop="24dp"
android:backgroundTint="@color/primaryColorDark"
android:textSize="@dimen/edit_text_size"/>
<EditText
android:layout_width="140dp"
android:layout_height="wrap_content"
android:inputType="number"
android:ems="10"
android:padding="12dp"
android:id="@+id/text_to"
android:layout_below="@+id/text_from"
android:layout_alignParentStart="true"
android:maxLength="8"
android:hint="To"
android:backgroundTint="@color/primaryColorDark"
android:layout_marginTop="8dp"
android:layout_marginLeft="10dp"
android:textSize="@dimen/edit_text_size"/>
<Button
android:layout_width="140dp"
android:layout_height="70dp"
android:text="Generate"
android:id="@+id/generate_button"
android:onClick="generate"
android:layout_below="@+id/app_bar"
android:layout_marginLeft="190dp"
android:layout_marginTop="51dp"
android:textColor="#fffafafa"
android:textSize="22dp"
android:backgroundTint="@color/accentColor"
/>
</RelativeLayout>
</android.support.v4.widget.DrawerLayout>
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="AppTheme.Base">
<!-- Customize your theme here. -->
</style>
<!--Base theme-->
<style name="AppTheme.Base" parent="Theme.AppCompat.Light.NoActionBar" >
<item name="colorPrimary">@color/primaryColor</item>
<item name="colorPrimaryDark">@color/primaryColorDark</item>
<item name="colorAccent">@color/accentColor</item>
<!-- Generate Button Color -->
<item name="android:colorButtonNormal">@color/primaryColor</item>
</style>
<!--Action Bar theme-->
<style name="MyCustomToolBarTheme" parent="ThemeOverlay.AppCompat.Light">
<!--The title-->
<item name="android:textColorPrimary">#FFFFFF</item>
<item name="android:textColor">#FFFFFF</item>
<!--The 'overflow icon-->
<item name="android:textColorSecondary">#FFFFFF</item>
</style>
<style name="ActionBarPopupThemeOverlay" parent="ThemeOverlay.AppCompat.Light" >
<item name="android:background">@android:color/white</item>
<item name="android:textColor">#de000000</item>
</style>
</resources>
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="56dp"
android:background="@color/primaryColor"
app:theme="@style/MyCustomToolBarTheme"
android:paddingTop="@dimen/app_bar_top_padding"
android:elevation="@dimen/action_bar_elevation"
app:popupTheme="@style/ActionBarPopupThemeOverlay"
>
</android.support.v7.widget.Toolbar>
@颜色/原色
@颜色/原色深色
@颜色/强调色
@颜色/原色
#FFFFFF
#FFFFFF
#FFFFFF
@android:彩色/白色
#de000000
app_bar.xml
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="bedi.gursimran.materialtest.MainActivity">
<include
android:id="@+id/app_bar"
layout="@layout/app_bar" />
<TextView
android:textSize="68dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/random_text"
android:text="Number"
android:layout_marginTop="12dp"
android:layout_centerVertical="true"
android:layout_centerHorizontal="false"
android:layout_marginLeft="50dp"
/>
<EditText
android:layout_width="140dp"
android:layout_height="wrap_content"
android:inputType="number"
android:padding="12dp"
android:ems="10"
android:id="@+id/text_from"
android:maxLength="6"
android:hint="From"
android:layout_below="@+id/app_bar"
android:layout_alignParentStart="true"
android:layout_marginLeft="10dp"
android:layout_marginTop="24dp"
android:backgroundTint="@color/primaryColorDark"
android:textSize="@dimen/edit_text_size"/>
<EditText
android:layout_width="140dp"
android:layout_height="wrap_content"
android:inputType="number"
android:ems="10"
android:padding="12dp"
android:id="@+id/text_to"
android:layout_below="@+id/text_from"
android:layout_alignParentStart="true"
android:maxLength="8"
android:hint="To"
android:backgroundTint="@color/primaryColorDark"
android:layout_marginTop="8dp"
android:layout_marginLeft="10dp"
android:textSize="@dimen/edit_text_size"/>
<Button
android:layout_width="140dp"
android:layout_height="70dp"
android:text="Generate"
android:id="@+id/generate_button"
android:onClick="generate"
android:layout_below="@+id/app_bar"
android:layout_marginLeft="190dp"
android:layout_marginTop="51dp"
android:textColor="#fffafafa"
android:textSize="22dp"
android:backgroundTint="@color/accentColor"
/>
</RelativeLayout>
</android.support.v4.widget.DrawerLayout>
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="AppTheme.Base">
<!-- Customize your theme here. -->
</style>
<!--Base theme-->
<style name="AppTheme.Base" parent="Theme.AppCompat.Light.NoActionBar" >
<item name="colorPrimary">@color/primaryColor</item>
<item name="colorPrimaryDark">@color/primaryColorDark</item>
<item name="colorAccent">@color/accentColor</item>
<!-- Generate Button Color -->
<item name="android:colorButtonNormal">@color/primaryColor</item>
</style>
<!--Action Bar theme-->
<style name="MyCustomToolBarTheme" parent="ThemeOverlay.AppCompat.Light">
<!--The title-->
<item name="android:textColorPrimary">#FFFFFF</item>
<item name="android:textColor">#FFFFFF</item>
<!--The 'overflow icon-->
<item name="android:textColorSecondary">#FFFFFF</item>
</style>
<style name="ActionBarPopupThemeOverlay" parent="ThemeOverlay.AppCompat.Light" >
<item name="android:background">@android:color/white</item>
<item name="android:textColor">#de000000</item>
</style>
</resources>
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="56dp"
android:background="@color/primaryColor"
app:theme="@style/MyCustomToolBarTheme"
android:paddingTop="@dimen/app_bar_top_padding"
android:elevation="@dimen/action_bar_elevation"
app:popupTheme="@style/ActionBarPopupThemeOverlay"
>
</android.support.v7.widget.Toolbar>
最重要的是MainActivity.java
public class MainActivity extends ActionBarActivity implements TextWatcher {
private Toolbar toolbar;
TextView randomText;
EditText editTextFrom;
EditText editTextTo;
Button generate_button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//toolbar= (Toolbar) findViewById(R.id.app_bar);
toolbar = (Toolbar) findViewById(R.id.app_bar);
setSupportActionBar(toolbar);
//Generated Number
randomText = (TextView) findViewById(R.id.random_text);
Typeface myCustomFontTextView = Typeface.createFromAsset(getAssets(), "fonts/RobotoCondensed-Light.ttf");
randomText.setTypeface(myCustomFontTextView);
//From Text
editTextFrom = (EditText) findViewById(R.id.text_from);
Typeface myCustomFontEditViewFrom = Typeface.createFromAsset(getAssets(), "fonts/RobotoCondensed-Regular.ttf");
editTextFrom.setTypeface(myCustomFontEditViewFrom);
editTextFrom.addTextChangedListener(this);
//To Text
editTextTo = (EditText) findViewById(R.id.text_to);
Typeface myCustomFontEditViewTo = Typeface.createFromAsset(getAssets(), "fonts/RobotoCondensed-Regular.ttf");
editTextTo.setTypeface(myCustomFontEditViewTo);
editTextTo.addTextChangedListener(this);
//Generate Button
generate_button = (Button) findViewById(R.id.generate_button);
Typeface myCustomFontButton = Typeface.createFromAsset(getAssets(), "fonts/Roboto-Bold.ttf");
generate_button.setTypeface(myCustomFontButton);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
Toast.makeText(this, item.getTitle(), Toast.LENGTH_SHORT).show();
return true;
}
// if (id == R.id.navigate) {
// startActivity(new Intent(this, SubActivity.class));
// }
return super.onOptionsItemSelected(item);
}
public void generate(View view) {
//text
EditText from = (EditText) findViewById(R.id.text_from);
EditText to = (EditText) findViewById(R.id.text_to);
String stringFrom, stringTo;
stringFrom = from.getText().toString();
stringTo = to.getText().toString();
int numberFrom, numberTo;
if (stringFrom.equals("")) {
stringFrom = "0";
}
if (stringTo.equals("")) {
stringTo = "0";
}
numberFrom = Integer.parseInt(stringFrom);
numberTo = Integer.parseInt(stringTo);
Random random = new Random();
int number = random.nextInt(numberTo + 1);
if(numberFrom != numberTo) {
while (number < numberFrom || number > numberTo) {
number = random.nextInt(numberTo + 1);
}
} else {
numberFrom = numberTo = number;
}
// } catch (NumberFormatException e) {}
try {
TextView myText = (TextView) findViewById(R.id.random_text);
String myString = String.valueOf(number);
myText.setText(myString);
} catch (NumberFormatException e) {
TextView myText = (TextView) findViewById(R.id.random_text);
myText.setText("Enter a number greater than zero");
}
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable editable) {
int numberto;
try {
numberto = Integer.parseInt(editable.toString());
} catch (NumberFormatException e) {
numberto = 0;
}
if (numberto == 0) {
editable.replace(0, editable.length(), "1");
}
}
}
公共类MainActivity扩展了ActionBarActivity实现了TextWatcher{
专用工具栏;
文本查看随机文本;
编辑文本编辑来自;
编辑文本编辑文本;
按钮生成按钮;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//工具栏=(工具栏)findViewById(R.id.app\u栏);
工具栏=(工具栏)findViewById(R.id.app\u栏);
设置支持操作栏(工具栏);
//生成数
randomText=(TextView)findViewById(R.id.random_text);
Typeface myCustomFontTextView=Typeface.createFromAsset(getAssets(),“fonts/RobotoCondensed Light.ttf”);
randomText.setTypeface(myCustomFontTextView);
//从文本
editTextFrom=(EditText)findViewById(R.id.text\u from);
Typeface myCustomFontEditViewFrom=Typeface.createFromAsset(getAssets(),“fonts/RobotoCondensed Regular.ttf”);
editTextFrom.setTypeface(myCustomFontEditViewFrom);
editTextFrom.addTextChangedListener(此);
//发短信
editTextTo=(EditText)findViewById(R.id.text\u to);
Typeface myCustomFontEditViewTo=Typeface.createFromAsset(getAssets(),“fonts/RobotoCondensed Regular.ttf”);
editTextTo.setTypeface(myCustomFontEditViewTo);
editTextTo.addTextChangedListener(此);
//生成按钮
generate_button=(button)findviewbyd(R.id.generate_button);
Typeface myCustomFontButton=Typeface.createFromAsset(getAssets(),“font/Roboto Bold.ttf”);
生成按钮。设置字体(myCustomFontButton);
}
@凌驾
公共布尔onCreateOptions菜单(菜单){
//为菜单充气;这会将项目添加到操作栏(如果存在)。
getMenuInflater().充气(右菜单菜单菜单主菜单);
返回true;
}
@凌驾
公共布尔值onOptionsItemSelected(菜单项项){
//处理操作栏项目单击此处。操作栏将
//自动处理Home/Up按钮上的点击,只要
//在AndroidManifest.xml中指定父活动时。
int id=item.getItemId();
//noinspection SimplifiableIf语句
if(id==R.id.action\u设置){
Toast.makeText(this,item.getTitle(),Toast.LENGTH_SHORT).show();
返回true;
}
//if(id==R.id.navigate){
//startActivity(新意图(this,SubActivity.class));
// }
返回super.onOptionsItemSelected(项目);
}
公共void生成(视图){
//正文
EditText-from=(EditText)findViewById(R.id.text\u-from);
EditText to=(EditText)findViewById(R.id.text\u to);
字符串stringFrom、stringTo;
stringFrom=from.getText().toString();
stringTo=to.getText().toString();
int numberFrom,numberTo;
if(stringFrom.equals(“”){
stringFrom=“0”;
}
if(stringTo.equals(“”){
stringTo=“0”;
}
numberFrom=Integer.parseInt(stringFrom);
numberTo=Integer.parseInt(stringTo);
随机=新随机();
整数=随机.nextInt(numberTo+1);
如果(numberFrom!=numberTo){
while(numbernumberTo){
数字=随机.nextInt(数字到+1);
}
}否则{
numberFrom=numberTo=number;
}
//}catch(numberformatxe){
试一试{
TextView myText=(TextView)findViewById(R.id.random_text);
String myString=String.valueOf(number);
设置文本(myString);
}捕获(数字格式){
TextView myText=(TextView)findViewById(R.id.random_text);
setText(“输入一个大于零的数字”);
}
}
@凌驾
更改前文本之前的公共void(字符序列s、int start、int count、int after){
}
@凌驾
public void onTextChanged(字符序列、int start、int before、int count){
}
@凌驾
public void PostTextChanged(可编辑){
整数;
试一试{
numberto=Integer.parseInt(可编辑的.toString());
}捕获(数字格式){
numberto=0;
}
如果(numberto==0){
可编辑的.replace(0,可编辑的.length(),“1”);
}
}
}
编辑
当我拿出TextWatcher时,错误消失了。请帮我弄一下按钮的颜色,我仍然不知道如何有两个不同颜色的按钮。谢谢
编辑2在我将两者都设置为空时,会提供此日志。请帮忙
2414-2414/bedi.gursimran.materialtest E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: bedi.gursimran.materialtest, PID: 2414
java.lang.IllegalStateException: Could not execute method of the activity
at android.view.View$1.onClick(View.java:4007)
at android.view.View.performClick(View.java:4756)
at android.view.View$PerformClick.run(View.java:19749)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at android.view.View$1.onClick(View.java:4002)
at android.view.View.performClick(View.java:4756)
at android.view.View$PerformClick.run(View.java:19749)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
Caused by: java.lang.IllegalArgumentException: n <= 0: 0
at java.util.Random.nextInt(Random.java:182)
at bedi.gursimran.materialtest.MainActivity.generate(MainActivity.java:120)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at android.view.View$1.onClick(View.java:4002)
at android.view.View.performClick(View.java:4756)
at android.view.View$PerformClick.run(View.java:19749)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
2414-2414/bedi.gursimran.materialtest E/AndroidRuntime﹕ 致命异常:主
工艺:bedi.gursimran.materialtest,PID:2414
java.lang.IllegalStateException:无法执行活动的方法
在android.view.view