2011-09-23 30 views
0

我的imageview事物不斷關閉我。Button保持對我的關閉?

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



    } 
+0

你發佈logcat時發生了什麼錯誤。 –

回答

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

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

if(v == image1)在image1文本下給我一個錯誤 –

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


} 

0

由於要動態創建活動視圖,並且您已經添加任何按鈕到您的activit的觀點,所以它總是返回null,因此,將引發異常,並且應用程序會崩潰...