在ImageButton的点击动作事件错误 [英] Motion event error on ImageButton click

查看:94
本文介绍了在ImageButton的点击动作事件错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

public class AddActivity extends Activity implements OnClickListener{
String[] info =  new String[11];

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

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub
    TextView keyString = (TextView)findViewById(R.id.keyString);
    TextView site1 = (TextView)findViewById(R.id.site1);
    TextView site2 = (TextView)findViewById(R.id.site2);
    TextView site3 = (TextView)findViewById(R.id.site3);
    ImageButton submit = (ImageButton)findViewById(R.id.submit);
    ImageButton add1 = (ImageButton)findViewById(R.id.add1);
    ImageButton add2 = (ImageButton)findViewById(R.id.add2);
    ImageButton add3 = (ImageButton)findViewById(R.id.add3);

    submit.setOnClickListener((OnClickListener) this);
    add1.setOnClickListener((OnClickListener) this);
    add2.setOnClickListener((OnClickListener) this);
    add3.setOnClickListener((OnClickListener) this);

    int id = v.getId();
    switch(id){
    case R.id.submit:{
        submitEntry(info);
        break;
    }
    case R.id.add1:{
        add2.setVisibility(View.VISIBLE);
        site2.setVisibility(View.VISIBLE);
        break;
    }
    }
}
}

这是code。

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

<TextView
    android:id="@+id/textView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/key_string"
    android:textAppearance="?android:attr/textAppearanceLarge" />

<TextView
    android:id="@+id/textView3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceSmall"
    android:visibility="invisible" />

<EditText
    android:id="@+id/keyString"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge" />

<TextView
    android:id="@+id/textView4"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:visibility="invisible" />

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/site_string"
    android:textAppearance="?android:attr/textAppearanceLarge" />

<TextView
    android:id="@+id/textView6"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceSmall"
    android:visibility="invisible" />

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal" >

            <ImageButton
                android:id="@+id/add1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/ic_action_new" />

            <EditText
                android:id="@+id/site1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="@string/url_hint"
                android:textAppearance="?android:attr/textAppearanceLarge" />
        </LinearLayout>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:visibility="invisible" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal" >

            <ImageButton
                android:id="@+id/add2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/ic_action_new"
                android:visibility="invisible" />

            <EditText
                android:id="@+id/site2"
                android:layout_width="match_parent"
                android:layout_height="fill_parent"
                android:text="@string/url_hint"
                android:textAppearance="?android:attr/textAppearanceLarge"
                android:visibility="invisible" />
        </LinearLayout>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:visibility="invisible" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal" >

            <ImageButton
                android:id="@+id/add3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/ic_action_new"
                android:visibility="invisible" />

            <EditText
                android:id="@+id/site3"
                android:layout_width="match_parent"
                android:layout_height="fill_parent"
                android:text="@string/url_hint"
                android:visibility="invisible"
                android:textAppearance="?android:attr/textAppearanceLarge" />
        </LinearLayout>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:visibility="invisible" />
       <Button
            android:id="@+id/submit"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:text="@string/submit_buttom" />
    </LinearLayout>
</ScrollView>
</LinearLayout>

这是XML。该ADD1,ADD2,ADD3并提交ImageButtons都在滚动型。

And this is the XML. The add1, add2, add3 and submit ImageButtons are all in a ScrollView.

当我preSS的ADD1的ImageButton,我要ADD2和Site2 ImageButtons成为可见的,但相反,它引发以下错误。

When I press the add1 ImageButton, I want the add2 and site2 ImageButtons to become visible but instead, it throws the following error.

运动事件具有无效的指针数0;值必须在1到16之间。

Motion event has invalid pointer count 0; value must be between 1 and 16.

我在做什么错了?

PS:所有的findViewById()调用是在onclick()方法,因为NullPointerExeption被抛出,如果我叫他们在OnCreate()

PS: All the findViewById() calls are in the onClick() method because a NullPointerExeption is thrown if I call them in the onCreate().

推荐答案

那些 findViewById 调用的onClick 别ŧ意义。不知道为什么你得到一个空指针异常调用它们在的onCreate 的onClick 是从来没有在这种情况下,因为没有什么所谓的在创建活动中被指定的按钮来看看你的的onClick 方法;按钮将默认为无指定监听器。它也不会看起来像一个好主意,使用活动作为onClickListener为好。

Those findViewByIdcalls in onClickdon't make sense. Not sure why you are getting a null pointer exception calling them in onCreate.onClick is never called in this instance because nothing in the creation of the Activity is assigning the buttons to look at your onClick method; the buttons will default to having no listener assigned. It also doesn't look like a good idea to use the Activity as the onClickListener as well.

您code应该是这个样子:

Your code should look something like this:

public class AddActivity extends Activity {
// https://source.android.com/source/code-style.html
// info -> mInfo; non-public, non-static field!
String[] mInfo =  new String[11];

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

    TextView keyString = (TextView)findViewById(R.id.keyString);
    TextView site1 = (TextView)findViewById(R.id.site1);
    TextView site2 = (TextView)findViewById(R.id.site2);
    TextView site3 = (TextView)findViewById(R.id.site3);
    Button submit = (Button)findViewById(R.id.submit);
    ImageButton add1 = (ImageButton)findViewById(R.id.add1);
    ImageButton add2 = (ImageButton)findViewById(R.id.add2);
    ImageButton add3 = (ImageButton)findViewById(R.id.add3);

    add1.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
        add2.setVisibility(View.VISIBLE);
        site2.setVisibility(View.VISIBLE);
        }
    });

    submit.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
        submitEntry(mInfo);
        }
    });
}

这篇关于在ImageButton的点击动作事件错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆