每当我在手机上运行应用程序时,它都可以正常工作并正确打开,但是一旦我单击TextView在其中键入一些文本,应用程序就会崩溃。我在论坛上收到了类似的帖子,但是它帮不了我。完全不知道实际发生了什么。 Android平台的新功能。 (请问我是否需要有关“问题”的信息)


       import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.RelativeLayout.LayoutParams;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    public void ButtonClicked(View view){

        EditText EnteredAmount = (EditText) findViewById(R.id.EnteredAmount);
        Log.i("Amount Entered", EnteredAmount.getText().toString());
  }


       --------- beginning of crash
09-08 22:29:11.790 25239-25239/com.example.admin.convertcurrency E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.admin.convertcurrency, PID: 25239
    java.lang.IllegalStateException: Could not find method clickTextView(View) in a parent or ancestor Context for android:onClick attribute defined on view class android.support.v7.widget.AppCompatEditText with id 'EnteredAmount'
        at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.resolveMethod(AppCompatViewInflater.java:423)
        at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:380)
        at android.view.View.performClick(View.java:5215)
        at android.view.View$PerformClick.run(View.java:21196)
        at android.os.Handler.handleCallback(Handler.java:742)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:157)
        at android.app.ActivityThread.main(ActivityThread.java:5603)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:774)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:652)


    --------- beginning of system
09-08 22:29:11.858 25239-25239/com.example.admin.convertcurrency I/Process: Sending signal. PID: 25239 SIG: 9

最佳答案

java.lang.IllegalStateException: Could not find method clickTextView(View) in a parent or ancestor Context for android:onClick attribute defined on view class android.support.v7.widget.AppCompatEditText with id 'EnteredAmount'

在您的布局中,您似乎有android:onClick="clickTextView"。在您的 Activity 中,您没有名为clickTextView()的方法。您需要在 Activity 中实现该方法:
public void clickTextView(View v) {
 // whatever you want to do
}

或者,您可以从布局资源中的android:onClick="clickTextView"中删除<EditText>属性。

07-24 07:50