简体   繁体   中英

Could not execute method for android:onClick (TextView.setText)

I have that exception, and I've searching answers here, but it didn't works for me. So i have that error:

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) 

And this is my MainActivity code(ye, it bad, but i'm only learning that):

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);

}

So it stops work in this part:

textView.setText(randomed);

I've initialize this textView, and set android:onClick, but i think i've done something wrong. If i write only string, it works fine, but when i clicked(tap), app stops working PS Sorry for my english

You're calling the textView.setText(String s) method with a parameter that is not a String. Please just convert your argument randomed to a String when you call textView.setText(String s) either by using textView.setText(new String(randomed)) or textView.setText(""+randomed) .

 textView.setText(randomed+"");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM