开发者

Button keeps force closing on me?

My imageview thing keeps closing on me.

XML code: this is my code that places the button

    <Butto开发者_开发知识库n
    android:id="@+id/sound" 
    android:src="@drawable/test"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" />   

Java code: where I have the the code for the button and it says image1.setOnClickListener(this); is that is force closing it in the 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()){


    }
}

My entire code: if you need to see it so you can tell whats going on

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



    }


Why you taking risk by extending at the same time implementing OnClickListener.. why not to make an inner class like this.

class click implements OnCLickListener{

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

        }


}

and instead of image1.setOnClickListener(SlideDirection.this); add image1.setOnClickListener(new click());


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


please make change in onCreate() method like below and it work fine.

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


}


As you are creating your activity view dynamically, and you have added no Button into your activit's view, so it will always return null and as a result, exception will be raised and your application would crash...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜