我有这个例外,我在这里搜索答案,但它对我不起作用。所以我有这个错误:
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,但我认为我做错了什么。如果我只写字符串,它工作正常,但当我点击(点击),应用程序停止工作 附:对不起我的英文
答案 0 :(得分:3)
您使用不是String的参数调用textView.setText(String s)
方法。当您使用randomed
或textView.setText(String s)
致电textView.setText(new String(randomed))
时,请将您的参数textView.setText(""+randomed)
转换为字符串。
答案 1 :(得分:0)
textView.setText(randomed+"");