2017-03-31 650 views
1

我已經開始學習本週的代碼了。我一直在關注向Google日曆添加事件的教程,我遇到了無法找到方法的問題。錯誤是:Android onClick事件找不到方法

找不到方法onAddEventClicked(查看)中的Android父母或祖先上下文:onclick屬性上定義視圖類android.support.v7.widget.AppCompatButton ID爲「addEventButton」

這是我的代碼:

package com.example.user.plannerv2; 

import android.support.v7.app.AppCompatActivity; 
import android.content.Intent; 
import android.provider.CalendarContract; 
import android.os.Bundle; 
import java.util.Calendar; 
import android.provider.CalendarContract.Events; 
import android.view.View; 

public class sub_page01 extends AppCompatActivity { 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.fragment_sub_page01); 
    } 

    public void onAddEventClicked(View view) { 
     Intent intent = new Intent(Intent.ACTION_INSERT); 
     intent.setType("vnd.android.cursor.item/event"); 

     Calendar cal = Calendar.getInstance(); 
     long startTime = cal.getTimeInMillis(); 
     long endTime = cal.getTimeInMillis() + 60 * 60 * 1000; 

     intent.putExtra(CalendarContract.EXTRA_EVENT_BEGIN_TIME, startTime); 
     intent.putExtra(CalendarContract.EXTRA_EVENT_END_TIME,endTime); 
     intent.putExtra(CalendarContract.EXTRA_EVENT_ALL_DAY, true); 

     intent.putExtra(Events.TITLE, "Customer Cabin"); 
     intent.putExtra(Events.DESCRIPTION, "Cabin is leased out"); 
     intent.putExtra(Events.EVENT_LOCATION, "Cabin"); 

     startActivity(intent); 
    } 
} 

這裏是我的XML;

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <Button 
     android:id="@+id/addEventButton" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:onClick="onAddEventClicked" 
     android:text="Add Event" /> 
</RelativeLayout> 

這是我得到的錯誤:

E/AndroidRuntime: FATAL EXCEPTION: main 
        Process: com.example.user.plannerv2, PID: 16127 
        java.lang.IllegalStateException: Could not find method onAddEventClicked(View) in a parent or ancestor Context for android:onClick attribute defined on view class android.support.v7.widget.AppCompatButton with id 'addEventButton' 
         at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.resolveMethod(AppCompatViewInflater.java:327) 
         at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:284) 
         at android.view.View.performClick(View.java:6207) 
         at android.widget.TextView.performClick(TextView.java:11094) 
         at android.view.View$PerformClick.run(View.java:23639) 
         at android.os.Handler.handleCallback(Handler.java:751) 
         at android.os.Handler.dispatchMessage(Handler.java:95) 
         at android.os.Looper.loop(Looper.java:154) 
         at android.app.ActivityThread.main(ActivityThread.java:6688) 
         at java.lang.reflect.Method.invoke(Native Method) 
         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1468) 
         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1358) 

正如我已經說過,我不是100%肯定,我覺得該方法被調用。任何幫助都會很棒。

+2

您已經加載'R.layout.fragment_sub_page01'到一些其他類......除此之外,你爲什麼加載* *片段**佈局到一個活動?嘗試命名您的資源更好 –

回答

0

做這樣的

findViewById(R.id.addEventButton).setOnClickListener(new View.OnClickListener() { 
    @Override 
    public void onClick(View v) { 
     //Perform your action here 
    } 
}); 

你完成了:)

+1

這是一個Java問題,而不是Xamarin –

+1

@ cricket_007您的投票速度非常快:)無論如何,允許我在Java中添加答案以及:) – Geek

+1

爲什麼要添加它「以及」? –