gpt4 book ai didi

java - 无法执行 android :onClick (TextView. setText 的方法)

转载 作者:搜寻专家 更新时间:2023-11-01 08:34:53 24 4
gpt4 key购买 nike

我有那个异常(exception),我在这里搜索答案,但它对我不起作用。所以我有那个错误:

java.lang.IllegalStateException: Could not execute method for android:onClick
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:293)
at android.view.View.performClick(View.java:4084)
at android.view.View$PerformClick.run(View.java:16966)

at android.os.Handler.handleCallback(Handler.java:615)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4745)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
at android.view.View.performClick(View.java:4084) 
at android.view.View$PerformClick.run(View.java:16966) 
at android.os.Handler.handleCallback(Handler.java:615) 
at android.os.Handler.dispatchMessage(Handler.java:92) 
at android.os.Looper.loop(Looper.java:137) 
at android.app.ActivityThread.main(ActivityThread.java:4745) 
at java.lang.reflect.Method.invokeNative(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:511) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 
at dalvik.system.NativeStart.main(Native Method) 
Caused by: android.content.res.Resources$NotFoundException: String resource ID #0xb
at android.content.res.Resources.getText(Resources.java:229)
at android.support.v7.widget.ResourcesWrapper.getText(ResourcesWrapper.java:52)
at android.widget.TextView.setText(TextView.java:3620)
at com.henichaer.randomizer.MainActivity.onClick(MainActivity.java:64)
at java.lang.reflect.Method.invokeNative(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:511) 
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288) 
at android.view.View.performClick(View.java:4084) 
at android.view.View$PerformClick.run(View.java:16966) 
at android.os.Handler.handleCallback(Handler.java:615) 
at android.os.Handler.dispatchMessage(Handler.java:92) 
at android.os.Looper.loop(Looper.java:137) 
at android.app.ActivityThread.main(ActivityThread.java:4745) 
at java.lang.reflect.Method.invokeNative(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:511) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 
at dalvik.system.NativeStart.main(Native Method) 

这是我的 MainActivity 代码(是的,它很糟糕,但我只是在学习):

public class MainActivity extends AppCompatActivity {
private ImageButton settingsButon;
private ImageButton infoButon;
private SharedPreferences sp;
TextView textView;
private int min;
private int max;
private RelativeLayout relativeLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
settingsButon = (ImageButton)findViewById(R.id.settingsButton);
infoButon = (ImageButton)findViewById(R.id.infoButton);
relativeLayout = (RelativeLayout)findViewById(R.id.relLayout);
textView = (TextView)findViewById(R.id.randomedView) ;
sp = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
// textView.setPaintFlags(textView.getPaintFlags() | Paint.SUBPIXEL_TEXT_FLAG);


}

@Override
protected void onResume() {
super.onResume();
boolean darkTheme = sp.getBoolean("darkTheme",false);
if(darkTheme){
relativeLayout.setBackgroundColor(getResources().getColor(R.color.colorPrimaryDark));
settingsButon.setImageResource(R.drawable.settings_white);
infoButon.setImageResource(R.drawable.information_white);
}
else {
relativeLayout.setBackgroundColor(getResources().getColor(R.color.colorPrimary));
settingsButon.setImageResource(R.drawable.settings_black);
infoButon.setImageResource(R.drawable.information_black);
}

min = Integer.parseInt( sp.getString("minNumber", "1"));
max = Integer.parseInt( sp.getString("maxNumber", "100"));
}

public void settingsClick(View view) {
Intent intent = new Intent(this,Preferences.class);
startActivity(intent);
}


public void onClick(View view) {
Randomizer randomizer = new Randomizer();
int randomed = randomizer.randomNext(min,max);
System.out.println(randomed);
textView.setText(randomed);

}

所以它停止了这部分的工作:

textView.setText(randomed);

我已经初始化了这个 textView,并设置了 android:onClick,但我想我做错了什么。如果我只写字符串,它工作正常,但是当我点击(点击)时,应用程序停止工作附言对不起我的英语

最佳答案

您使用非字符串参数调用 textView.setText(String s) 方法。请在调用 textView.setText(String s) 时通过使用 textView.setText(new String(randomed))< 将参数 randomed 转换为字符串textView.setText(""+randomed)

关于java - 无法执行 android :onClick (TextView. setText 的方法),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37592822/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com