2014-07-12 144 views
1

我無法讓我的點擊偵聽器正常工作。 ImageButton被正確檢索。OnClickListener沒有響應

偵聽器(擴展其被設置視圖我的自定義活動):

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    Log.d("UserActivity", "Loading imagebutton..."); 
    ImageButton iButton = (ImageButton) findViewById(R.id.user_button_ratings); 
    Log.d("UserActivity", "Button " + iButton); 
    iButton.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      Log.e("Button", "Image was clicked"); 
     } 
    }); 
} 

BaseActivity

@Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(getLayoutId()); 

     drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout); 

     drawerToggle = new ActionBarDrawerToggle((Activity) this, drawerLayout, 
       R.drawable.ic_drawer, 0, 0) { 
      public void onDrawerClosed(View view) { 
       getActionBar().setTitle(R.string.app_name); 
      } 

      public void onDrawerOpened(View drawerView) { 
       getActionBar().setTitle(R.string.menu); 
      } 
     }; 
     drawerLayout.setDrawerListener(drawerToggle); 

     getActionBar().setDisplayHomeAsUpEnabled(true); 
     getActionBar().setHomeButtonEnabled(true); 

     layers = getResources().getStringArray(R.array.layers_array); 
     drawerList = (ListView) findViewById(R.id.left_drawer); 

     drawerList.setAdapter(new ArrayAdapter<String>(this, 
       R.layout.adapter_navigation, R.id.navigation_text, layers)); 

     final BaseActivity activity = this; 
     drawerList.setOnItemClickListener(new OnItemClickListener() { 
      @Override 
      public void onItemClick(AdapterView<?> arg0, View arg1, int pos, 
        long arg3) { 
       // Do something 
      } 
     }); 

     drawerList.setBackgroundColor(Color.LTGRAY); 
    } 

佈局:

<ImageButton 
    android:id="@+id/user_button_ratings" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_gravity="center_horizontal" 
    android:background="@color/transparent" 
    android:scaleType="fitCenter" 
    android:src="@drawable/mmb_rating_big" /> 

清單:

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.panic.xenira.mmb" 
    android:versionCode="2" 
    android:versionName="0.1.1" > 

    <uses-sdk 
     android:minSdkVersion="14" 
     android:targetSdkVersion="19" /> 

    <uses-permission android:name="android.permission.INTERNET" /> 

    <application 
     android:allowBackup="true" 
     android:icon="@drawable/ic_launcher" 
     android:label="@string/app_name" 
     android:theme="@style/AppTheme" > 

     <activity 
      android:name=".MainActivity" 
      android:label="@string/app_name" 
      android:theme="@style/NoActionBar" > 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
     <activity 
      android:name=".SearchActivity" 
      android:label="@string/title_activity_search" > 
     </activity> 
     <activity 
      android:name=".BaseActivity" 
      android:label="@string/title_activity_base" > 
      <intent-filter> 
       <action android:name="android.intent.action.SEARCH" /> 
      </intent-filter> 

      <meta-data 
       android:name="android.app.searchable" 
       android:resource="@xml/searchable" /> 
     </activity> 
     <activity 
      android:name=".UserActivity" 
      android:label="@string/title_activity_user" > 
     </activity> 
     <activity 
      android:name=".UserViewerActivity" 
      android:label="@string/title_activity_user_viewer" > 
     </activity> 
    </application> 

</manifest> 

LogCat中沒有錯誤,並且onClick方法未被鎖定。 我試過不同的方法來使用監聽器,但從來沒有得到任何迴應。我也嘗試了一個代碼,但對我而言沒有反應。

將是巨大的,如果你culd幫助我;)

+0

你可以發佈Android Manifest的內容嗎?而且我也看不到'setContentView' – Cristian

+0

@Cristian添加了清單。設置的內容視圖位於BaseActivity中。 onClickListener應該在擴展BaseActivity的UserActivity中設置。 – Xenira

+0

@Mike添加了BaseActivity – Xenira

回答

2

當使用DrawerLayout,應該只有一個主要內容View,與抽屜View - 在這種情況下,您的ListView - 在它之後列出。以任何其他方式使用DrawerLayout將導致錯誤的,不可預知的行爲,經常會阻止與其他佈局元素的正常交互。

可以在this developer page上找到包含示例和文檔鏈接的教程。

1

這裏我給圖像按鈕的例子。如果它也給錯誤,然後清潔日蝕項目,然後再試一次。

<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" 
android:paddingBottom="@dimen/activity_vertical_margin" 
android:paddingLeft="@dimen/activity_horizontal_margin" 
android:paddingRight="@dimen/activity_horizontal_margin" 
android:paddingTop="@dimen/activity_vertical_margin" 
tools:context=".MainActivity" > 

<ImageButton 
    android:id="@+id/imageButton1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignRight="@+id/textView1" 
    android:layout_below="@+id/textView1" 
    android:layout_marginRight="35dp" 
    android:layout_marginTop="32dp" 
    android:contentDescription="image" 
    android:src="@drawable/ic_launcher" /> 

import android.app.Activity; 
import android.os.Bundle; 
import android.view.Menu; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.ImageButton; 
import android.widget.Toast; 

public class MainActivity extends Activity { 

ImageButton imgButton; 

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

    addListenerOnButton(); 
} 

private void addListenerOnButton() { 

    imgButton = (ImageButton) findViewById(R.id.imageButton1); 

    imgButton.setOnClickListener(new OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      Toast.makeText(MainActivity.this, "Clicked", Toast.LENGTH_SHORT) 
        .show(); 
     } 
    }); 
} 
}