OnClickListener没有响应

时间:2014-07-12 21:33:38

标签: java android onclicklistener

我无法让我的点击听众工作。正确检索ImageButton。

监听器(扩展我设置视图的自定义活动):

@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方法没有出现问题。 我尝试过使用监听器的不同方法但从未得到任何响应。我也尝试了一个适合我的代码,但没有回应。

如果你能帮助我,那会很棒;)

2 个答案:

答案 0 :(得分:3)

使用DrawerLayout时,应该只有一个主要内容View,抽屉View - 在这种情况下,您的ListView - 列在其后面。以任何其他方式使用DrawerLayout将导致不正确,不可预测的行为,通常会阻止与其他布局元素的正常交互。

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

答案 1 :(得分:1)

这里我举一个Image按钮的例子。如果它也给出错误,那么清理eclipse项目并重试。

<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();
        }
    });
}
}