Motion event error on ImageButton click

一曲冷凌霜 提交于 2019-12-12 05:51:05

问题


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

This is the 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>

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

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

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

What am I doing wrong?

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


回答1:


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.

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



回答2:


The findViewById() calls and especially the setOnClickListener() calls should have been inside the onCreate(). With setOnClickListener() inside the onClick() i doubt the onClick was ever called.

We would need more logs to find the exact issue.



来源:https://stackoverflow.com/questions/28411401/motion-event-error-on-imagebutton-click

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!