2017-12-12 49 views
-1

既存のプロジェクトのXMLを変更するときにこのエラーが発生しました。これは私のXMLはonClickプロパティは、このエラーを示し 対応するメソッドハンドラが見つかりません - Android XML

public class MonthViewActivity extends AppCompatActivity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_month_view); 
    } 

    public void changeMonth(View view) { 
     //Some methods here 
    } 

} 

MonthViewActivity.java

activity_month_view.xml

<android.support.design.widget.CoordinatorLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context=".activities.YearViewActivity"> 

    <ImageView 
     android:id="@+id/month_view_prev_month" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:src="@drawable/ic_keyboard_arrow_left_black_24dp" 
     android:onClick="changeMonth"/> 

</android.support.design.widget.CoordinatorLayout> 

私のJavaクラス

のように見えたものです

Corresponding method handler 'public void changeMonth(android.view.View)' not found 

私はthisthisを見ましたが、助けにはなりませんでした。

答えて

2

私はXMLのコンテキストを宣言するのに間違いを犯しました。 .activities.YearViewActivityを使用しましたが、異なるアクティビティでchangeMonthメソッドが宣言されましたが、これが原因でエラーが発生していました。

<android.support.design.widget.CoordinatorLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context=".activities.MonthViewActivity"> //Changed here 

    <ImageView 
     android:id="@+id/month_view_prev_month" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:src="@drawable/ic_keyboard_arrow_left_black_24dp" 
     android:onClick="changeMonth"/> 

</android.support.design.widget.CoordinatorLayout> 

tools:contextを呼び出し側アクティビティのクラスに変更すると、エラーが修正されました。

+1

.javaファイルに記述したメソッドがデザインウィンドウのonClickフィールドに表示されない場合は、そのXMLファイルのtools:contextを必ずチェックしてください。これは非常に重要なポインターでした。ありがとう – user3833732

関連する問題