尝试在Resources中调用虚方法res = getResources();

时间:2016-08-31 12:38:36

标签: android exception background

我正在尝试使用处理程序更改活动的背景颜色,但我收到错误“尝试调用虚拟方法”。

这是我的代码

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<int[]> arrayOfColor = new ArrayList<int[]>();

public List<int[]> 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);
}
}

这是我的logcat。

 08-31 16:29:47.122 13152-13152/com.example.salimshivani.student E/AndroidRuntime: FATAL EXCEPTION: main
 Process: com.example.salimshivani.student, PID: 13152
 java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.salimshivani.student/com.example.salimshivani.student.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference
 at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3132)
 at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3415)
 at android.app.ActivityThread.access$1100(ActivityThread.java:229)
 at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1821)
 at android.os.Handler.dispatchMessage(Handler.java:102)
 at android.os.Looper.loop(Looper.java:148)
 at android.app.ActivityThread.main(ActivityThread.java:7325)
 at java.lang.reflect.Method.invoke(Native Method)
 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)

 Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference
 at android.content.ContextWrapper.getResources(ContextWrapper.java:92)
 at android.view.ContextThemeWrapper.getResources(ContextThemeWrapper.java:81)
 at com.example.salimshivani.student.MainActivity.<init>(MainActivity.java:241)
 at java.lang.Class.newInstance(Native Method)
 at android.app.Instrumentation.newActivity(Instrumentation.java:1096)
 at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3122)
 at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3415) 
 at android.app.ActivityThread.access$1100(ActivityThread.java:229) 
 at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1821) 
 at android.os.Handler.dispatchMessage(Handler.java:102) 
 at android.os.Looper.loop(Looper.java:148) 
 at android.app.ActivityThread.main(ActivityThread.java:7325) 
 at java.lang.reflect.Method.invoke(Native Method) 
 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)

请帮助我在错误的地方不断移动backgroundColor活动。

提前致谢

2 个答案:

答案 0 :(得分:20)

您调用方法getResources()作为类初始化的一部分(在任何方法之外,因此它将作为构造函数的一部分执行)

此时,Activity实例尚不存在,因此它可能无法调用需要存在实例的方法。

会导致Exception的语句,因为他们利用ActivityContext的一种事实:

  

android.content.res.Resources res = getResources();
  int [] clrItems = res.getIntArray(R.array.color_background);

另一方面,以下陈述不会引起问题,因为它只是普通的旧Java:

  

列表arrayOfColor = new ArrayList();

只需粘贴&#34;问题陈述&#34;进入一种方法,例如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);

    ...
}

答案 1 :(得分:1)

为什么您需要 res clrItems 作为字段? 但是你可以做这样的事情

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<int[]> arrayOfColor = new ArrayList<int[]>();

public List<int[]> 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);
}
}