2013-06-25 5 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); 





} 

행 번호 주석 처리 .setOnClickListener (this); 다시 들어가면 앱이 다운됩니다. 나가면 아무런 문제가 없습니다. 또한 여기에 ... 청취자가 직접 View.onClickListenr를 사용하여 제작뿐만 아니라 별도의 클래스에있을 필요 tryed 또한 주요 활동 클래스와 메인 레이아웃 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

귀하의 HolderButton 명의 자녀가 없으면 귀하의 Activity입니다. 따라서 Holder 내에 Button을 찾으려고 시도하면 이 반환되고 null이 반환되어 충돌이 발생합니다.

0

학급은 활동을 연장하지 않습니다.

활동에 설정된 현재 뷰 계층 구조의 뷰 정의 참조를 찾을 수 있습니다. 클래스가 활동을 확장하지 않고보기를 확장합니다.

findViewById가 null을 반환합니다. NullPointerException이 발생합니다.

다음에서 onCreate를 오버라이드 (override)

public class Holder extends Activity implements OnClickListener 

활동

을 확장합니다.

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

    } 
관련 문제