2013-07-24 2 views
0

onClickListener 메서드에 문제가 있습니다. 애플리케이션 충돌이라고 할 때. 나는 해결책을 찾고 있었지만 내 코드에는 무엇이 말해 왔는지 모든 것이있는 것처럼 보인다.onClickListener로 인해 응용 프로그램이 손상 될 수 있습니다.

public class Sudoku extends Activity implements OnClickListener { 

    @Override 
    public void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_sudoku); 
     View continue_button = findViewById(R.id.continue_button); 
     continue_button.setOnClickListener(this); 
     View new_button = findViewById(R.id.new_button); 
     new_button.setOnClickListener(this); 
     View about_button = findViewById(R.id.about_button); 
     about_button.setOnClickListener(this); 
     View exit_button = findViewById(R.id.exit_button); 
     exit_button.setOnClickListener(this); 
    } 

    // ... 

    public void onClick(View v) 
    { 
     switch (v.getId()) 
     { 
     case R.id.about_button: 
     Intent i = new Intent(this, About.class); 
     startActivity(i); 
     break; 
     } 
    } 
} 

-

public class About extends Activity { 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_about); 
    } 
} 

그리고 주요 활동 :

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:background="@color/background" 
    android:orientation="horizontal" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:padding="30dip" 
    tools:context=".Sudoku" > 
    <LinearLayout 
     android:orientation="vertical" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center" > 
     <TextView 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:text="@string/main_tittle" 
      android:gravity="center" 
      android:textColor="@color/main_tittle" 
      android:textSize="25sp" 
      android:layout_marginBottom="25dip"/> 
     <Button 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:text="@string/continue_label" /> 
     <Button 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:text="@string/new_game_label" /> 
     <Button 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:text="@string/about_label" /> 
     <Button 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:text="@string/exit_label" /> 
    </LinearLayout> 
</LinearLayout> 

나는 안드로이드/자바 프로그래밍에서 매우 초보자 해요, 용서하십시오.

+2

귀하의 logcat 오류를 추가하십시오. –

+0

은 게시 된 xml'activity_sudoku1'입니다. 또한 버튼에는 id 속성이 없습니다. – Raghunandan

+1

우리는 logcat 추적이 필요합니다 ... – Aracem

답변

0

내가 어떤 아이디의이 선언되는 표시되지 않습니다이이

Button continue_button = (Button)findViewById(R.id.continue_button); 
     continue_button.setOnClickListener(this); 
     Button new_button = (Button)findViewById(R.id.new_button); 
     new_button.setOnClickListener(this); 
     Button about_button =(Button) findViewById(R.id.about_button); 
     about_button.setOnClickListener(this); 
     Button exit_button =(Button) findViewById(R.id.exit_button); 
     exit_button.setOnClickListener(this); 

같이해야한다 귀하의 버튼에 대한 xml.

 <Button 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:id="@+id/about_button" 
     android:text="@string/about_label" /> 
1
<?xml version="1.0" encoding="utf-8"?> 

<LinearLayout 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_gravity="center" 
    android:orientation="vertical" > 

    <TextView 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginBottom="25dip" 
     android:gravity="center" 
     android:id="@+id/main_tittle" 
     android:textSize="25sp" /> 

    <Button 
     android:id="@+id/continue_label" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" /> 

    <Button 
     android:id="@+id/new_game_label" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" /> 

    <Button 
     android:id="@+id/about_label" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" /> 

    <Button 
     android:id="@+id/exit_label" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" /> 
</LinearLayout> 

@Override 
     public void onCreate(Bundle savedInstanceState) 
     { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.activity_sudoku); 
      View continue_button = findViewById(R.id.continue_button); /// findviewbyid not by text String resource 
      continue_button.setOnClickListener(this); 
      View new_button = findViewById(R.id.new_button); 
      new_button.setOnClickListener(this); 
      View about_button = findViewById(R.id.about_button); 
      about_button.setOnClickListener(this); 
      View exit_button = findViewById(R.id.exit_button); 
      exit_button.setOnClickListener(this); 
     } 
0

당신은이

View continue_button = findViewById(R.id.continue_button); 
     continue_button.setOnClickListener(this); 
     View new_button = findViewById(R.id.new_button); 
     new_button.setOnClickListener(this); 
     View about_button = findViewById(R.id.about_button); 
     about_button.setOnClickListener(this); 
     View exit_button = findViewById(R.id.exit_button); 
     exit_button.setOnClickListener(this); 
을 사용하고 또한 당신이보기

를 사용하고 다음 버튼을 사용하는 경우 0

그것은 당신이 (모든 버튼 태그)이 같은 id 속성을 추가 할 때마다 버튼에 포스트 XML 파일

  <Button 
      android:id="@+id/continue_button" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:text="@string/continue_label" /> 


     <Button 
     android:id="@+id/new_button" 
     android:id="@+id/new_game_label" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" /> 

    <Button 
     android:id="@+id/about_button" 
     android:id="@+id/about_label" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" /> 

    <Button 
     android:id="@+id/exit_button" 
     android:id="@+id/exit_label" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" /> 
관련 문제