可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
I am trying to change the background color of an activity using a handler, but I am getting an error "Attempt to invoke virtual method".
Here is my code
public class MainActivity extends AppCompatActivity { private EditText editTextUser, editTextPass; private RelativeLayout relativeLayoutMain; private Random random = new Random(); Intent intent; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); relativeLayoutMain = (RelativeLayout) findViewById(R.id.relativeLayoutMain); Button btnSignIn = (Button) findViewById(R.id.buttonSignIn); btnSignIn.setEnabled(false); handler.postDelayed(runner, 2000); Button buttonSignUp = (Button) findViewById(R.id.buttonSignUp); buttonSignUp.setText("Not registered? CLICK HERE"); editTextUser = (EditText) findViewById(R.id.editTextUser); editTextPass = (EditText) findViewById(R.id.editTextPassword); if (editTextUser.getText().toString() != null && editTextPass.getText().toString() != null) { btnSignIn.setEnabled(true); } } android.content.res.Resources res = getResources(); int[] clrItems = res.getIntArray(R.array.color_background); List arrayOfColor = new ArrayList(); public List getArrayOfColor() { arrayOfColor.add(clrItems); return arrayOfColor; } Runnable runner = new Runnable() { @Override public void run() { Log.e("run: ", "call"); Bitmap bitmap = Bitmap.createBitmap(612, 612, Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(bitmap); final int clr = 0xFF424242; final Paint paint = new Paint(); final Rect destRect = new Rect((612-bitmap.getWidth())/2, 24, (612)-(612-bitmap.getWidth())/2, 612-24); final RectF rectF = new RectF(destRect); final Rect srcRect = new Rect(0, 0, bitmap.getWidth(), 612); final float roundPx = 612; paint.setAntiAlias(true); canvas.drawARGB(0, 0, 0, 0); paint.setColor(clr); canvas.drawRoundRect(rectF, roundPx, roundPx, paint); paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN)); canvas.drawBitmap(bitmap, srcRect, destRect, paint); GradientDrawable gd = new GradientDrawable(GradientDrawable.Orientation.LEFT_RIGHT, new int[]{0xFF616261, 0xFF131313}); gd.setCornerRadius(0f); relativeLayoutMain.setBackground(gd); handler.postDelayed(runner, 4000); } }; public void login(View view) { intent = new Intent(this, HomeActivity.class); startActivity(intent); } public void register(View view) { intent = new Intent(this, SignUpActivity.class); startActivity(intent); } }
And here is my logcat.
Please help me where i am wrong to move the backgroundColor constantly of the activity.
Thanks in advance
回答1:
You invoke the method getResources() as part of the class initialisation (outside of any method, so it will be executed as part of the constructor)
At this point, the Activity instance does not yet exist so it may not call methods which require the existence of the instance.
Statements which will cause an exception because they exploit the fact that an Activity is a kind of Context:
android.content.res.Resources res = getResources();
int[] clrItems = res.getIntArray(R.array.color_background);
The following statement on the other hand will not cause problems because it's just plain old Java:
List arrayOfColor = new ArrayList();
Simply paste the "problem statements" into a method, e.g. onCreate()
// declare here android.content.res.Resources res; int[] clrItems; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // initialise here res = getResources(); clrItems = res.getIntArray(R.array.color_background); relativeLayoutMain = (RelativeLayout) findViewById(R.id.relativeLayoutMain); Button btnSignIn = (Button) findViewById(R.id.buttonSignIn); btnSignIn.setEnabled(false); ... }
回答2:
Why do you need res and clrItems as fields? However you can do something like this
public class MainActivity extends AppCompatActivity { private EditText editTextUser, editTextPass; private RelativeLayout relativeLayoutMain; private Random random = new Random(); Intent intent; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); relativeLayoutMain = (RelativeLayout) findViewById(R.id.relativeLayoutMain); res = getResources(); clrItems = res.getIntArray(R.array.color_background); Button btnSignIn = (Button) findViewById(R.id.buttonSignIn); btnSignIn.setEnabled(false); handler.postDelayed(runner, 2000); Button buttonSignUp = (Button) findViewById(R.id.buttonSignUp); buttonSignUp.setText("Not registered? CLICK HERE"); editTextUser = (EditText) findViewById(R.id.editTextUser); editTextPass = (EditText) findViewById(R.id.editTextPassword); if (editTextUser.getText().toString() != null && editTextPass.getText().toString() != null) { btnSignIn.setEnabled(true); } } android.content.res.Resources res; int[] clrItems; List arrayOfColor = new ArrayList(); public List getArrayOfColor() { arrayOfColor.add(clrItems); return arrayOfColor; } Runnable runner = new Runnable() { @Override public void run() { Log.e("run: ", "call"); Bitmap bitmap = Bitmap.createBitmap(612, 612, Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(bitmap); final int clr = 0xFF424242; final Paint paint = new Paint(); final Rect destRect = new Rect((612-bitmap.getWidth())/2, 24, (612)-(612-bitmap.getWidth())/2, 612-24); final RectF rectF = new RectF(destRect); final Rect srcRect = new Rect(0, 0, bitmap.getWidth(), 612); final float roundPx = 612; paint.setAntiAlias(true); canvas.drawARGB(0, 0, 0, 0); paint.setColor(clr); canvas.drawRoundRect(rectF, roundPx, roundPx, paint); paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN)); canvas.drawBitmap(bitmap, srcRect, destRect, paint); GradientDrawable gd = new GradientDrawable(GradientDrawable.Orientation.LEFT_RIGHT, new int[]{0xFF616261, 0xFF131313}); gd.setCornerRadius(0f); relativeLayoutMain.setBackground(gd); handler.postDelayed(runner, 4000); } }; public void login(View view) { intent = new Intent(this, HomeActivity.class); startActivity(intent); } public void register(View view) { intent = new Intent(this, SignUpActivity.class); startActivity(intent); } }