java - 安卓 Java : App Crashes when onClick button pressed

标签 java android

我有一个倒计时应用程序,其中有 3 个额外的倒计时页面。每个页面上的倒计时都按其应有的方式工作。我在第一页的 java 代码中添加了一个选择图像按钮,这意味着我可以从图库中选择要显示的不同图像。

当我现在单击该特定页面的按钮时,应用程序崩溃。

来自 Logcat 的错误消息 -

java.lang.IllegalStateException: Could not find method my40th(View) in a parent or ancestor Context for android:onClick attribute defined on view class androidx.appcompat.widget.AppCompatButton with id 'my40th'

我哪里出错了?我在另一个带有单一页面的测试倒计时上有相同的代码,并且工作正常。

这是该特定页面的 java 代码:

package com.example.countdown;

import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import java.text.SimpleDateFormat;
import java.util.Date;

public class my40th extends AppCompatActivity {

private TextView txtDay, txtHour, txtMinute, txtSecond;
private TextView tvEventStart;
private Handler handler;
private Runnable runnable;
private Bundle savedInstanceState;

@Override
protected void onCreate(Bundle savedInstanceState) {
    this.savedInstanceState = savedInstanceState;
    super.onCreate( savedInstanceState );
    setContentView( R.layout.activity_main );
    myImage = findViewById( R.id.imgView );
    sp = getSharedPreferences( "setback", MODE_PRIVATE );
    if (sp.contains( "imagepath" )) {
        storedpath = sp.getString( "imagepath", "" );
        myImage.setImageBitmap( BitmapFactory.decodeFile( storedpath ) );
    }

    txtDay = (TextView) findViewById( R.id.txtDay );
    txtHour = (TextView) findViewById( R.id.txtHour );
    txtMinute = (TextView) findViewById( R.id.txtMinute );
    txtSecond = (TextView) findViewById( R.id.txtSecond );
    tvEventStart = (TextView) findViewById( R.id.tveventStart );

    countDownStart();
}

private void countDownStart() {
    handler = new Handler();
    runnable = new Runnable() {

        public void run() {
            handler.postDelayed( this, 1000 );
            try {
                SimpleDateFormat dateFormat = new SimpleDateFormat(
                        "dd-MM-yyyy" );
                Date futureDate = dateFormat.parse( "31-8-2020" );
                Date currentDate = new Date();
                if (!currentDate.after( futureDate )) {
                    long diff = futureDate.getTime()
                            - currentDate.getTime();
                    long days = diff / (24 * 60 * 60 * 1000);
                    diff -= days * (24 * 60 * 60 * 1000);
                    long hours = diff / (60 * 60 * 1000);
                    diff -= hours * (60 * 60 * 1000);
                    long minutes = diff / (60 * 1000);
                    diff -= minutes * (60 * 1000);
                    long seconds = diff / 1000;
                    txtDay.setText( "" + String.format( "%02d", days ) );
                    txtHour.setText( "" + String.format( "%02d", hours ) );
                    txtMinute.setText( "" + String.format( "%02d", minutes ) );
                    txtSecond.setText( "" + String.format( "%02d", seconds ) );
                } else {
                    tvEventStart.setVisibility( View.VISIBLE );
                    tvEventStart.setText( "Holiday is over, get a new one booked ASAP!" );
                    textViewGone();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    };
    handler.postDelayed( runnable, 1 * 1000 );
}

public void textViewGone() {
    findViewById( R.id.LinearLayout1 ).setVisibility( View.GONE );
    findViewById( R.id.LinearLayout2 ).setVisibility( View.GONE );
    findViewById( R.id.LinearLayout3 ).setVisibility( View.GONE );
    findViewById( R.id.LinearLayout4 ).setVisibility( View.GONE );
    findViewById( R.id.textViewheader2 ).setVisibility( View.GONE );
}

public void loadImagefromGallery(View view) {
    Intent galleryIntent = new Intent( Intent.ACTION_PICK,
            android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI );
    startActivityForResult( galleryIntent, RESULT_LOAD_IMG );
}
String mFilePath;
private static int RESULT_LOAD_IMG = 1;
String imgpath, storedpath;
SharedPreferences sp;
ImageView myImage;


@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult( requestCode, resultCode, data );
    try {
        if (requestCode == RESULT_LOAD_IMG && resultCode == RESULT_OK
                && null != data) {

            Uri selectedImage = data.getData();
            String[] filePathColumn = {MediaStore.MediaColumns.DATA};

            Cursor cursor = getContentResolver().query( selectedImage,
                    filePathColumn, null, null, null );
            cursor.moveToFirst();

            int columnIndex = cursor.getColumnIndex( filePathColumn[0] );
            imgpath = cursor.getString( columnIndex );
            Log.d( "path", imgpath );
            cursor.close();

            SharedPreferences.Editor edit = sp.edit();
            edit.putString( "imagepath", imgpath );
            edit.commit();


            Bitmap myBitmap = BitmapFactory.decodeFile( imgpath );

            myImage.setImageBitmap( myBitmap );
        } else {
            Toast.makeText( this, "You haven't picked an Image",
                    Toast.LENGTH_LONG ).show();
        }
    } catch (Exception e) {
        Toast.makeText( this, "Oops something's went wrong", Toast.LENGTH_LONG )
                .show();
    }
    }
 }

最佳答案

在您的 activity_main.xml 文件中:

替换

android:onClick="my40th"

android:onClick="loadImagefromGallery"

关于java - 安卓 Java : App Crashes when onClick button pressed,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58537170/

相关文章:

java - 如何通过单击 JMenuItem 来执行批处理文件以在 Windows 上重新启动服务?

java - 如何使用 Platform.runLater 使 JavaFX 线程安全

java - Apache Felix 文件安装无法正常工作

java - 无法使用 Android 客户端 APP 从 TCP 服务器读取数据

android - 带有 CameraUpdateFactory.newLatLngBounds 的 moveCamera 崩溃

java - 电子邮件地址中的撇号引发异常

java - 需要通知/仪表板系统的框架吗?

接收流的Android单元测试 View 模型

android - 无法加载游戏 : findLibrary returned null: with cocos2d-x, 尽管尝试了很多次仍未成功?

android - 无法获取资源'http ://. ..firebase-measurement-connector-impl-17.0.5-javadoc.jar