如何在Android中单击按钮更改ImageView?

时间:2019-06-11 03:39:31

标签: android

我尝试创建一个练习应用,将点击按钮上的state = { text: '', words: 0, reset: false, }; updateInput(event) { if (this.state.reset) { this.setState({ reset: false, }); return; } this.setState({ text: event.nativeEvent.text, }); } <TextInput value={this.state.text} onChange={this.updateInput.bind(this)} onKeyPress={({ nativeEvent }) => { if (nativeEvent.key === ' ') { this.setState({ text: '', words: this.state.words + 1, reset: true, }); console.log(this.state.text); console.log('onKeyPress worked!'); console.log( 'there are a total of ' + this.state.words + ' words!' ); } }} blurOnSubmit={false} autoFocus={true} autoCorrect={false} autoCompleteType={false} allowFontScaling={true} keyboardType="default" keyboardAppearance="dark" onSubmitEditing={event => { event.preventDefault(); this.setState({ text: '', words: this.state.words + 1, }); }} /> 更改为另一张图片。该应用程序崩溃了。我希望应用程序在单击按钮时将屏幕上的图像更改为另一图像。

LOGCAT:-

ImageView

Java代码:-

06-11 09:15:17.860 10063-10063/com.example.myapplication3 E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.myapplication3, PID: 10063
android.content.res.Resources$NotFoundException: Resource ID #0x7f060055
    at android.content.res.Resources.getValue(Resources.java:1366)
    at android.support.v7.widget.AppCompatDrawableManager.loadDrawableFromDelegates(AppCompatDrawableManager.java:331)
    at android.support.v7.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:198)
    at android.support.v7.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:191)
    at android.support.v7.content.res.AppCompatResources.getDrawable(AppCompatResources.java:102)
    at android.support.v7.widget.AppCompatImageHelper.setImageResource(AppCompatImageHelper.java:86)
    at android.support.v7.widget.AppCompatImageView.setImageResource(AppCompatImageView.java:94)
    at com.example.myapplication3.MainActivity$1.onClick(MainActivity.java:29)
    at android.view.View.performClick(View.java:5233)
    at android.view.View$PerformClick.run(View.java:21211)
    at android.os.Handler.handleCallback(Handler.java:739)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:148)
    at android.app.ActivityThread.main(ActivityThread.java:5539)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
    06-11 09:15:17.860 10063-10063/com.example.myapplication3 D/AppTracker: App Event: crash

以下是XML文件:-

public class MainActivity extends AppCompatActivity {
Button btn;
TextView txt;
ImageView imgView1 ;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    btn = findViewById(R.id.button);
    txt = findViewById(R.id.textView);
    imgView1 = (ImageView) findViewById(R.id.imageView1);


    btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            txt.setText(getString(R.string.ChangedValue));
            imgView1.setImageResource(R.drawable.add);
        }
    });




}
}

1 个答案:

答案 0 :(得分:0)

  

错误:-Resources $ NotFoundException:资源ID#0x7f060055

ResourcesNotFoundException意味着当找不到请求的资源时,资源API抛出Developer site

简单来说,您尝试使用的可绘制对象,尺寸,布局,字符串或任何其他资源将不会出现在您的项目中。

解决方案:-请仔细阅读所有资源并检查其名称的拼写。任何小错误都会给我们这样的异常。

随时询问更多查询。