2013-07-24 224 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> 

我在安卓/ java编程非常初学者,请原谅。

+2

添加您的logcat的错误。 –

+0

是发布的xml'activity_sudoku1'。还有按钮没有id属性。 – Raghunandan

+1

我们需要logcat跟踪... – Aracem

回答

0

我没有看到被宣布任何的id为你的按钮在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

您使用此

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

它应该是这样的

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

你必须在每一个按钮添加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" />