我有一個自定義視圖,我想當我設置onClickListener我總是最終崩潰。onClickListener導致崩潰時設置
public class Holder extends View implements OnClickListener {
Bitmap test;
int row=1; int column=1;
int x=0;int y=0;
private Paint paint = new Paint();
int deltaX=5; int deltaY = 3;
Canvas c;
String [][] puzzleS;
Button [] numbers;
int width, height;
String input;
/*public Holder(Context context)
{
super(context);
test=BitmapFactory.decodeResource(super.getResources(),R.drawable.smallball);
SudokuGenerator gen = new SudokuGenerator();
String puzzle= "........."+
"........."+
"........."+
"........."+
"........."+
"........."+
"........."+
"........."+
".........";
gen.init(puzzle);
gen.generate(3);
gen.outputPuzzle();
puzzleS = gen.getStrPuzzle();
}*/
public Holder(Context context, AttributeSet attrs)
{
super(context, attrs);
test=BitmapFactory.decodeResource(super.getResources(),R.drawable.smallball);
SudokuGenerator gen = new SudokuGenerator();
String puzzle= "........."+
"........."+
"........."+
"........."+
"........."+
"........."+
"........."+
"........."+
".........";
gen.init(puzzle);
String input="";
gen.generate(3);
gen.outputPuzzle();
puzzleS = gen.getStrPuzzle();
numbers= new Button[9];
numbers[0]=(Button) findViewById(R.id.button1); //numbers[0].setOnClickListener(this);
numbers[1]=(Button) findViewById(R.id.button2);
numbers[2]=(Button) findViewById(R.id.button3);
numbers[3]=(Button) findViewById(R.id.button4);
numbers[4]=(Button) findViewById(R.id.button5);
numbers[5]=(Button) findViewById(R.id.button6);
numbers[6]=(Button) findViewById(R.id.button7);
numbers[7]=(Button) findViewById(R.id.button8);
numbers[8]=(Button) findViewById(R.id.button9);
}
註釋行號[0] .setOnClickListener(this);回到原來的應用程序崩潰。離開它我沒有問題。我也tryed有聽衆在一個單獨的類,以及使它直接使用View.onClickListenr ......這裏也是主要的活動課和主要佈局XML
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.LinearLayout;
public class MainActivity extends Activity {
//Holder sweet;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<com.bg.twist.Holder
android:id="@+id/view"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingTop="285dip"
android:weightSum="9">
<Button
android:id="@+id/button1"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="35dip"
android:text="1"
android:layout_weight="1" />
<Button
android:id="@+id/button2"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="35dip"
android:text="2"
android:layout_weight="1" />
<Button
android:id="@+id/button3"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="35dip"
android:text="3"
android:layout_weight="1" />
<Button
android:id="@+id/button4"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="35dip"
android:text="4"
android:layout_weight="1" />
<Button
android:id="@+id/button5"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="35dip"
android:text="5"
android:layout_weight="1" />
<Button
android:id="@+id/button6"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="35dip"
android:text="6"
android:layout_weight="1" />
<Button
android:id="@+id/button7"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="35dip"
android:text="7"
android:layout_weight="1" />
<Button
android:id="@+id/button8"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="35dip"
android:text="8"
android:layout_weight="1" />
<Button
android:id="@+id/button9"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="35dip"
android:text="9"
android:layout_weight="1" />
</LinearLayout>
</FrameLayout>
顯示錯誤日誌 – stinepike