按钮让我闭嘴?

时间:2011-09-23 04:54:32

标签: java android

我的imageview事情一直在关闭我。

XML代码:这是我放置按钮的代码

    <Button
    android:id="@+id/sound" 
    android:src="@drawable/test"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" />   

Java代码:我有按钮的代码,它说image1.setOnClickListener(this);是强制关闭它在logcat中。

    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    main = new LinearLayout(this);  
    main.setBackgroundColor(Color.BLACK);  
    main.setLayoutParams(new LinearLayout.LayoutParams(320,480));  

    viewA = new TextView(this);  
    viewA.setBackgroundColor(Color.WHITE);  
    viewA.setTextColor(Color.BLACK);  
    viewA.setTextSize(15);
    viewA.setLayoutParams(new LinearLayout.LayoutParams(320,180));  

    main.addView(viewA);
    setContentView(main);

    Button image1 = (Button) findViewById(R.id.sound);

    image1.setOnClickListener(this);


}


public void onClick(View v) {
    switch(v.getId()){


    }
}

我的整个代码:如果您需要查看它,那么您可以告诉我们最新情况

package dev.mrunknow.slidedirection;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.MotionEvent;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import android.view.View;

public class SlideDirection extends Activity implements View.OnClickListener{
    /** Called when the activity is first created. */

    private LinearLayout main;      
    private TextView viewA;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        main = new LinearLayout(this);  
        main.setBackgroundColor(Color.BLACK);  
        main.setLayoutParams(new LinearLayout.LayoutParams(320,480));  

        viewA = new TextView(this);  
        viewA.setBackgroundColor(Color.WHITE);  
        viewA.setTextColor(Color.BLACK);  
        viewA.setTextSize(15);
        viewA.setLayoutParams(new LinearLayout.LayoutParams(320,180));  

        main.addView(viewA);
        setContentView(main);

        Button image1 = (Button) findViewById(R.id.sound);

        image1.setOnClickListener(SlideDirection.this);


    }


    public void onClick(View v) {
        switch(v.getId()){


        }
    }

    float x_start = 0, y_start = 0, x_end = 0, y_end = 0;

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        viewA.setText("");
        viewA.setLayoutParams(new LinearLayout.LayoutParams(320,80));
        viewA.setTextSize(40);

        int action = event.getAction();

        if (action == MotionEvent.ACTION_DOWN)
        {
            x_start = event.getX();
            y_start = event.getY();

        }

        if(action == MotionEvent.ACTION_UP)
        {
            x_end = event.getX();
            y_end = event.getY();


            if((x_start - x_end) > 75 && (y_start - y_end) < -75)
            {
                viewA.setText("LEFT");
                Toast.makeText(this, "Left Works!", Toast.LENGTH_SHORT).show();
            }
            if((x_start - x_end) < -75 && (y_start - y_end) < -75)
            {
                viewA.setText("RIGHT");
                Toast.makeText(this, "Right Works!", Toast.LENGTH_SHORT).show();
            }
        }
        return true;
    }



    }

4 个答案:

答案 0 :(得分:0)

为什么你通过在实施OnClickListener的同时延长来承担风险..为什么不像这样建立一个内部类。

class click implements OnCLickListener{

@Override
        public void onClick(DialogInterface dialog, int which) {
            // TODO Auto-generated method stubdo your stuff

        }


}

而不是 image1.setOnClickListener(SlideDirection.this); 添加image1.setOnClickListener(new click());

答案 1 :(得分:0)

 public void onClick(View v) {
    if(v==image1)
    {
    //your logic
    }
 }

答案 2 :(得分:0)

请在下面的onCreate()方法中进行更改,并且工作正常。

@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);

    main = new LinearLayout(this);  
    main.setBackgroundColor(Color.BLACK);  
    main.setLayoutParams(new LinearLayout.LayoutParams(320,480));  

    viewA = new TextView(this);  
    viewA.setBackgroundColor(Color.WHITE);  
    viewA.setTextColor(Color.BLACK);  
    viewA.setTextSize(15);
    viewA.setLayoutParams(new LinearLayout.LayoutParams(320,180));  

   // add button programmatically not from xml the change that i have suggest you 
    image1 = new Button(this);
    image1.setBackgroundDrawable(getResources().getDrawable(R.drawable.icon));
    image1.setLayoutParams(new LinearLayout.LayoutParams(320,180));


    main.addView(viewA);
    main.addView(image1);
    setContentView(main);

   // Button image1 = (Button)findViewById(R.id.sound);
    image1.setOnClickListener(this);


}

main = new LinearLayout(this); main.setBackgroundColor(Color.BLACK); main.setLayoutParams(new LinearLayout.LayoutParams(320,480)); viewA = new TextView(this); viewA.setBackgroundColor(Color.WHITE); viewA.setTextColor(Color.BLACK); viewA.setTextSize(15); viewA.setLayoutParams(new LinearLayout.LayoutParams(320,180)); // add button programmatically not from xml the change that i have suggest you image1 = new Button(this); image1.setBackgroundDrawable(getResources().getDrawable(R.drawable.icon)); image1.setLayoutParams(new LinearLayout.LayoutParams(320,180)); main.addView(viewA); main.addView(image1); setContentView(main); // Button image1 = (Button)findViewById(R.id.sound); image1.setOnClickListener(this); }

答案 3 :(得分:0)

当您动态创建活动视图时,并且您没有在您的activit视图中添加任何Button,因此它将始终返回null,因此会引发异常并且您的应用程序将崩溃...