2012-03-20 39 views
15

Nút bấm trong bố cục thổi phồng không thành công khi chạy vì không thể tìm thấy phương thức. E/AndroidRuntime (921): java.lang.IllegalStateException: Không thể tìm thấy phương thức onClick (View) trong hoạt động nhưng, nếu nhấp vào nút từ bố cục chính, hãy làm việc tốt. Tôi đã làm gì sai?Không thể tìm thấy phương thức onClick (View) trong hoạt động

CẬP NHẬT: thay đổi mã ... stil fail ... thêm nhật ký lỗi Tôi muốn tất cả các nút của tôi được xử lý bằng phương pháp ome.

lớp hoạt động chính

package com.android.insertview; 


import android.app.Activity; 
import android.content.Context; 
import android.os.Bundle; 
import android.view.Gravity; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.view.ViewGroup; 
import android.widget.Toast; 

public class InsertViewActivity extends Activity { 
    /** Called when the activity is first created. */ 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     /*LayoutInflater inflater = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     ViewGroup parent = (ViewGroup)findViewById(R.id.main_view); 
     inflater.inflate(R.layout.volume_control, parent);*/ 

     LayoutInflater vi = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);   
     View v = vi.inflate(R.layout.volume_control, null);  
     View insertPoint = findViewById(R.id.main_view); 
     ((ViewGroup) insertPoint).addView(v, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT 
                      , ViewGroup.LayoutParams.WRAP_CONTENT)); 
    } 


    public void btnclick(View v) { 
     // TODO Auto-generated method stub 
     Context context = getApplicationContext(); 
     Toast toast = Toast.makeText(context, 
      "This is Toast Notification", Toast.LENGTH_SHORT); 
     toast.setGravity(Gravity.CENTER, 0, 0); 
     toast.show();  
    } 
} 

main.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" android:id="@+id/main_view"> 
</LinearLayout> 

lạm phát bố trí

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:orientation="vertical" android:background="@drawable/service_button_container_background" 
    android:id="@+id/volume_view"> 

    <Button 
     android:id="@+id/volume_up" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:background="@drawable/button_volume_up" 
     android:onClick="btnclick" /> 

    <Button 
     android:id="@+id/button2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:background="@drawable/button_volume_mute_off" 
     android:onClick="btnclick"/> 

    <Button 
     android:id="@+id/volume_down" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:background="@drawable/button_volume_down" 
     android:onClick="btnclick"/> 

</LinearLayout> 

log lỗi

03-19 15:31:45.184: W/dalvikvm(1071): threadid=3: thread exiting with uncaught exception (group=0x4001b188) 
03-19 15:31:45.184: E/AndroidRuntime(1071): Uncaught handler: thread main exiting due to uncaught exception 
03-19 15:31:45.194: E/AndroidRuntime(1071): java.lang.IllegalStateException: Could not find a method genclick(View) in the activity 
03-19 15:31:45.194: E/AndroidRuntime(1071):  at android.view.View$1.onClick(View.java:2020) 
03-19 15:31:45.194: E/AndroidRuntime(1071):  at android.view.View.performClick(View.java:2364) 
03-19 15:31:45.194: E/AndroidRuntime(1071):  at android.view.View.onTouchEvent(View.java:4179) 
03-19 15:31:45.194: E/AndroidRuntime(1071):  at android.widget.TextView.onTouchEvent(TextView.java:6541) 
03-19 15:31:45.194: E/AndroidRuntime(1071):  at android.view.View.dispatchTouchEvent(View.java:3709) 
03-19 15:31:45.194: E/AndroidRuntime(1071):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884) 
03-19 15:31:45.194: E/AndroidRuntime(1071):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884) 
03-19 15:31:45.194: E/AndroidRuntime(1071):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884) 
03-19 15:31:45.194: E/AndroidRuntime(1071):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884) 
03-19 15:31:45.194: E/AndroidRuntime(1071):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884) 
03-19 15:31:45.194: E/AndroidRuntime(1071):  at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1659) 
03-19 15:31:45.194: E/AndroidRuntime(1071):  at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1107) 
03-19 15:31:45.194: E/AndroidRuntime(1071):  at android.app.Activity.dispatchTouchEvent(Activity.java:2061) 
03-19 15:31:45.194: E/AndroidRuntime(1071):  at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1643) 
03-19 15:31:45.194: E/AndroidRuntime(1071):  at android.view.ViewRoot.handleMessage(ViewRoot.java:1691) 
03-19 15:31:45.194: E/AndroidRuntime(1071):  at android.os.Handler.dispatchMessage(Handler.java:99) 
03-19 15:31:45.194: E/AndroidRuntime(1071):  at android.os.Looper.loop(Looper.java:123) 
03-19 15:31:45.194: E/AndroidRuntime(1071):  at android.app.ActivityThread.main(ActivityThread.java:4363) 
03-19 15:31:45.194: E/AndroidRuntime(1071):  at java.lang.reflect.Method.invokeNative(Native Method) 
03-19 15:31:45.194: E/AndroidRuntime(1071):  at java.lang.reflect.Method.invoke(Method.java:521) 
03-19 15:31:45.194: E/AndroidRuntime(1071):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860) 
03-19 15:31:45.194: E/AndroidRuntime(1071):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618) 
03-19 15:31:45.194: E/AndroidRuntime(1071):  at dalvik.system.NativeStart.main(Native Method) 
03-19 15:31:45.194: E/AndroidRuntime(1071): Caused by: java.lang.NoSuchMethodException: genclick 
03-19 15:31:45.194: E/AndroidRuntime(1071):  at java.lang.ClassCache.findMethodByName(ClassCache.java:308) 
03-19 15:31:45.194: E/AndroidRuntime(1071):  at java.lang.Class.getMethod(Class.java:1014) 
03-19 15:31:45.194: E/AndroidRuntime(1071):  at android.view.View$1.onClick(View.java:2017) 
03-19 15:31:45.194: E/AndroidRuntime(1071):  ... 22 more 

UPDATE 2:

ok, nếu tôi làm như vậy, tôi có thể xử lý tất cả các nút bấm trong một phương pháp genericClick()

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/front_end" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" > 

    <!-- Main --> 
    <LinearLayout 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     android:id="@+id/main_view" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:layout_centerHorizontal="true" 
     android:background="@drawable/service_button_container_background" > 

     <ImageButton 
      android:id="@+id/radio" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:background="@drawable/button_radio" 
      android:clickable="true" 
      android:onClick="genericClick" /> 

     <ImageButton 
      android:id="@+id/player" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:background="@drawable/button_player" 
      android:onClick="genericClick" /> 

     <ImageButton 
      android:id="@+id/navi" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:background="@drawable/button_navigation" 
      android:onClick="genericClick" /> 
    </LinearLayout> 

    <!-- Volume controls --> 
    <LinearLayout 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     android:id="@+id/volume_controls" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_centerVertical="true" 
     android:background="@drawable/service_button_container_background" 
     android:orientation="vertical" > 

     <ImageButton 
      android:id="@+id/volume_up" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:background="@drawable/button_volume_up" 
      android:onClick="genericClick" /> 

     <ImageButton 
      android:id="@+id/volume_mute" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:background="@drawable/button_volume_mute_off" 
      android:onClick="genericClick" /> 

     <ImageButton 
      android:id="@+id/volume_down" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:background="@drawable/button_volume_down" 
      android:onClick="genericClick" /> 
    </LinearLayout> 

    <!-- Radio controls --> 
    <LinearLayout 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     android:id="@+id/radio_controls" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentTop="true" 
     android:layout_alignRight="@+id/main_view" 
     android:layout_marginRight="100dp" 
     android:background="@drawable/service_button_container_background" 
     android:visibility="invisible" > 

     <ImageButton 
      android:id="@+id/radio_preset" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:onClick="genericClick" android:background="@drawable/button_menu"/> 

     <ImageButton 
      android:id="@+id/button2" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:background="@drawable/button_rewind" 
      android:onClick="genericClick" /> 

     <ImageButton 
      android:id="@+id/button3" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:background="@drawable/button_play" 
      android:onClick="genericClick" /> 

     <ImageButton 
      android:id="@+id/button4" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:background="@drawable/button_forward" 
      android:onClick="genericClick" /> 
    </LinearLayout> 

    <!-- Radio Info --> 
    <LinearLayout 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     android:id="@+id/radio_info" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignBottom="@+id/radio_controls" 
     android:layout_alignLeft="@+id/main_view" 
     android:layout_marginLeft="100dp" 
     android:background="@drawable/service_button_container_background" 
     android:orientation="vertical" 
     android:visibility="invisible"> 

     <LinearLayout 
      xmlns:android="http://schemas.android.com/apk/res/android" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" > 

      <TextView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="FM" 
       android:textSize="18.0px" /> 

      <TextView 
       android:id="@+id/preset" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="1" 
       android:textColor="#ffd6d6d6" 
       android:textSize="18.0px" /> 

      <TextView 
       android:layout_width="10.0dip" 
       android:layout_height="wrap_content" 
       android:text=" " 
       android:textSize="18.0px" /> 

      <TextView 
       android:id="@+id/frequency" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="108.0" 
       android:textColor="#ffd6d6d6" 
       android:textSize="18.0px" /> 
     </LinearLayout> 

     <LinearLayout 
      xmlns:android="http://schemas.android.com/apk/res/android" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" > 

      <TextView 
       android:id="@+id/title" 
       android:layout_width="195.0dip" 
       android:layout_height="24.0dip" 
       android:text="Русское радио" 
       android:textColor="#ffd6d6d6" 
       android:textSize="18.0px" /> 
     </LinearLayout> 
    </LinearLayout> 
</RelativeLayout> 
+2

Không cần triển khai OnClickListener và ghi đè phương thức onClick: bạn có thể tự định nghĩa phương thức onClick. Nếu bạn xóa việc triển khai và thẻ @Override và chạy lại chương trình của bạn, nó vẫn không thành công? – Guillaume

+0

Ngoài ra, bạn nên gọi phương thức của mình với phương pháp thực sự, không phải là "onClick" chung chung – Guillaume

+0

@Guillaume, Hmmm.You đúng. Tôi cũng bối rối khi thấy rằng .... – Prem

Trả lời

11

Tháo @Override chú thích. Kiểm tra chính thức documentation cho android: onClick - trong trường hợp của họ là phương thức xử lý "selfDestruct (View)".

Cập nhật: như những người khác được đề xuất, bạn không nên sử dụng cách đặt tên chung cho phương thức xử lý sự kiện của mình - "onClick" quá chung chung. Nếu bạn có 10 nút thì sao? Hãy thử một cái gì đó như "onClickLoginButton", "onClickSaveBtn" vv Tôi nghĩ bạn không thể sử dụng "onClick" làm tên cho phương thức xử lý của bạn, vì View.OnClickListener có phương thức "onClick", vì vậy bạn cần thay đổi tên anyways.

<Button 
    android:id="@+id/volume_up" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:background="@drawable/button_volume_up" 
    android:onClick="handleVolumeUp" /> 

Và trong lớp học Hoạt động của bạn

public void handleVolumeUp(View view) { 
    // TODO Auto-generated method stub  
} 

Tháo thực hiện OnClickListener để định nghĩa về lớp học của bạn trở thành:

public class InsertViewActivity extends Activity 

UPDATE2: Nếu bạn muốn tất cả các nút để được xử lý bởi 1 người nghe nhấp chuột, hãy làm như sau:

@Override 
public void onCreate(Bundle savedInstanceState) 
{ 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    Button btn1 = (Button)findViewById(R.id.btn1); 
    btn1.setOnClickListener(btnListener); 
    Button btn2 = (Button)findViewById(R.id.btn2); 
    btn2.setOnClickListener(btnListener); 
    Button btn3 = (Button)findViewById(R.id.btn3); 
    btn3.setOnClickListener(btnListener); 
} 

private OnClickListener btnListener = new OnClickListener() 
{ 

    public void onClick(View v) 
    { 
     //do the same stuff or use switch/case and get each button ID and do different 

     //stuff depending on the ID 
    } 

}; 
+0

Anh ấy đang đặt tên phương thức là onClick trong tệp bố cục. Có thể cho cái tên đó không? Bởi vì khi bạn triển khai onClickListner, phương pháp này là bắt buộc để thực hiện. Xin vui lòng cho tôi biết nếu tôi sai ... Tôi chỉ không bao giờ được sử dụng methodName onClick trong tập tin bố trí .... Vì vậy, tôi tò mò muốn biết điều này – Prem

+0

Tôi không chắc chắn nếu đó là có thể, ông chắc chắn nên sử dụng chi tiết/tên cụ thể cho phương thức xử lý sự kiện. – hovanessyan

+0

xóa việc triển khai và phương pháp @Override và đổi tên thành oClick1, vẫn không thành công. – SpuDy

0

là hoạt động thứ hai của bạn trong manifest.xml?

Bạn cần thêm hoạt động của mình vào đó. Xem mã bên dưới Thay đổi SecondActivity trong hoạt động của bạn. Và thiết lập tên gói để được hoạt động của bạn nằm

<application 
    android:allowBackup="true" 
    android:icon="@drawable/ic_launcher" 
    android:label="@string/app_name" 
    android:theme="@style/AppTheme" > 
    <activity 
     android:name="PackageName.MainActivity" android:label="@string/app_name" > 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 

      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 

    <activity 
     android:name="PackageName.SecondActivityName" >  
    </activity> 

</application> 

2

Các vấn đề trong đề tài bố trí cho 5.X Android Các hoạt động chính không có chủ đề thẻ trong Layout.xml khác đã, đang và đó là một sai lầm cho hệ điều hành Android.

Giải pháp là đây: android 5 and onClick in xml layout

Nói tóm lại: 1) xóa thẻ sheme từ khắp nơi bố trí; 2) thêm thẻ với đúng sheme trong tệp kê khai

Tôi đã thử cho điện thoại Android 5.X - nó hoạt động.

+0

Ý của bạn là xóa chủ đề thẻ. Bạn có đề án nam hay chủ đề? Ngoài ra, những gì bạn có nghĩa là bằng cách loại bỏ nó. bạn có thể chứng minh không? –

+0

Sheme. Đang xóa = xóa. – Denis

-1

và bạn cần lưu ý một điều khác: public void onclick_method() {your logic;}. Studio không cung cấp cho bạn gợi ý. Tôi chỉ viết như thế này và lãng phí nhiều thời gian để tìm câu trả lời, cuối cùng tôi thấy rằng phương pháp đúng là như thế này: public void onclick_method (View view) {your logic;} như vậy, donot được quên tham số trong dấu ngoặc!

Blockquote

0

Một vấn đề thường gặp Tôi đã nhìn thấy là người mới bắt đầu sẽ tuyên bố phương pháp onClick như protected hoặc private. Khi bạn làm điều này, công cụ XML không thể truy cập vào phương thức tạo ra vấn đề này.

Các vấn đề liên quan