2012-06-06 178 views
-4

當我試圖運行它的代碼說,應用程序已停止不幸。請幫助我與我的編碼。以下是我想要解決方案的代碼。應用程序強行關閉運行。

package gaurav.android; 

import java.util.*; 

import android.app.Activity; 
import android.graphics.Color; 
import android.os.Bundle; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 

public class EventActivity extends Activity { 
    /** Called when the activity is first created. */ 
    View colorregion; 
    int[] colorchoices = {Color.BLACK , Color.BLUE, Color.CYAN}; 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     colorregion = findViewById(R.id.color_region); 
     Button clbutton = (Button)findViewById(R.id.color_button); 
     clbutton.setOnClickListener((OnClickListener) new ColorRandomizer()); 
    } 
    private void regioncolor(int color) 
    { 
     colorregion.setBackgroundColor(color); 
    } 
    private class ColorRandomizer implements onClickListener 
    { 
     public void onClick(View v) 
     { 
      Random r= new Random(); 
      int index= r.nextInt(colorchoices.length); 
      regioncolor(colorchoices[index]); 
     } 
    } 
} 
+5

的main.xml和日誌貓請........ –

+0

第一問 – Trikaldarshi

+0

之前,你應該調試代碼粘貼到您的logcat顯示你得到了什麼樣的錯誤 –

回答

1

更改代碼

private class ColorRandomizer implements onClickListener 
    { 
     public void onClick(View v) 
     { 
      Random r= new Random(); 
      int index= r.nextInt(colorchoices.length); 
      regioncolor(colorchoices[index]); 
     } 
    } 

這個

private class ColorRandomizer implements OnClickListener { 
     public void onClick(View v) { 
      Random r = new Random(); 
      int index = r.nextInt(colorchoices.length); 
      regioncolor(colorchoices[index]); 
     } 
    } 

它不是onClickListenerOnClickListener

+2

+1,我認爲這不應該即使編譯然後......... –

+1

是的,我不知道他如何運行 – Trikaldarshi

+0

不客氣,如果它的工作接受和upvote – Trikaldarshi

0
package com.test; 

    import java.util.Random; 

    import android.app.Activity; 
    import android.graphics.Color; 
    import android.os.Bundle; 
    import android.view.View; 
    import android.view.View.OnClickListener; 
    import android.widget.Button; 

    public class EventActivity extends Activity 
    { 
    /** Called when the activity is first created. */ 
    View colorregion; 
    int[] colorchoices = {Color.BLACK , Color.BLUE, Color.CYAN}; 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     colorregion = findViewById(R.id.color_region); 
     Button clbutton = (Button)findViewById(R.id.color_button); 
     clbutton.setOnClickListener(new ColorRandomizer()); 
    } 
    private void regioncolor(int color) 
    { 
     colorregion.setBackgroundColor(color); 
    } 
    private class ColorRandomizer implements OnClickListener 
    { 
     public void onClick(View v) 
     { 
      Random r= new Random(); 
      int index= r.nextInt(colorchoices.length); 
      regioncolor(colorchoices[index]); 
     } 
    } 

} 
相關問題