2013-06-25 65 views
0

我有一个自定义视图,我想当我设置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> 
+1

显示错误日志 – stinepike

回答

1

Holder没有任何Button孩子,这是你的Activity。因此,您尝试在Holder内找到Button,结果findViewById返回null,因此发生崩溃。

0

您的课程不会扩展活动。

您可以找到当前视图层次结构设置为该活动的ViewById。你的课不扩展活动,而是扩展视图。

在你的情况findViewById返回null。你将得到NullPointerException。

扩展活动

public class Holder extends Activity implements OnClickListener 

然后覆盖的onCreate。

@Override. 
    protected void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.mylayout); 
     // initialize buttons. 

    }