0

import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.Toast;
import java.io.IOException;

public class welcome_theme_screen extends AppCompatActivity {
    String UITheme;
    int Theme;
    RadioGroup ThemeChooser = (RadioGroup) findViewById(R.id.themeChooser);
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_welcome_theme);
        getSupportActionBar().hide();
        //Title Font
        TextView textView = (TextView) findViewById(R.id.WelcomeTitle);
        Typeface typeFace = Typeface.createFromAsset(getAssets(), "Title.ttf");
        textView.setTypeface(typeFace);
        //Button Font
        Button button = (Button) findViewById(R.id.button);
        button.setTypeface(typeFace);
        //Write Default Theme
        try {
            new CfgMgr().Write(this, "UITheme", "Space");
        } catch (IOException e) {
            e.printStackTrace();
        }
        CheckBox EnableSched = (CheckBox) findViewById(R.id.EnableSched);
        ThemeChooser.setOnCheckedChangeListener(OnItemSelected);
    }

    RadioGroup.OnCheckedChangeListener OnItemSelected = new OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            Theme = ThemeChooser.getCheckedRadioButtonId();
            Toast.makeText(getBaseContext(), Theme, Toast.LENGTH_LONG).show();
            switch (Theme) {
                default:
                    break;
                case 0:
                    UITheme = "Space";
                    break;
                case 1:
                    UITheme = "Light";
                    break;
                case 2:
                    UITheme = "Dark";
                    break;
                case 3:
                    UITheme = "AMOLED";
                    break;
                case 4:
                    UITheme = "Pacifying";
                    break;
                case 5:
                    UITheme = "Desert";
                    break;
                case 6:
                    UITheme = "Beach";
                    break;
            }
            /*
            try {
                new CfgMgr().Write(welcome_theme_screen.this, "UITheme", UITheme);
            } catch (IOException e) {
                e.printStackTrace();
            }

             */
        }
    };



    public void NextClicked(View view) {
        Intent intent = new Intent(this, Welcome_Tabs_Screen.class);
        startActivity(intent);
    }

    public void EnableSchedClicked(View view) {
        CheckBox EnableSched = (CheckBox) findViewById(R.id.EnableSched);

        if (EnableSched.isChecked() == true) {
            EnableSched.setChecked(false);
            EnableSched2Clicked(EnableSched);
        } else {
            EnableSched.setChecked(true);
            EnableSched2Clicked(EnableSched);
        }
    }

        public void EnableSched2Clicked(View view) {
            CheckBox EnableSched = (CheckBox) findViewById(R.id.EnableTabs);
            String EnableSchedVarStr;
            if (EnableSched.isChecked() == true) {
                EnableSchedVarStr = "true";
            } else {
                EnableSchedVarStr = "false";
            }
    }
}

Ошибка

java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{mate.files/mate.files.welcome_theme_screen}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.pm.ApplicationInfo android.content.Context.getApplicationInfo()' on a null object reference
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3355)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3614)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:86)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2199)
        at android.os.Handler.dispatchMessage(Handler.java:112)
        at android.os.Looper.loop(Looper.java:216)
        at android.app.ActivityThread.main(ActivityThread.java:7625)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:524)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:987)
     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.pm.ApplicationInfo android.content.Context.getApplicationInfo()' on a null object reference
        at android.content.ContextWrapper.getApplicationInfo(ContextWrapper.java:159)
        at android.view.ContextThemeWrapper.getTheme(ContextThemeWrapper.java:157)
        at android.content.Context.obtainStyledAttributes(Context.java:679)
        at androidx.appcompat.app.AppCompatDelegateImpl.createSubDecor(AppCompatDelegateImpl.java:848)
        at androidx.appcompat.app.AppCompatDelegateImpl.ensureSubDecor(AppCompatDelegateImpl.java:815)
        at androidx.appcompat.app.AppCompatDelegateImpl.findViewById(AppCompatDelegateImpl.java:640)
        at androidx.appcompat.app.AppCompatActivity.findViewById(AppCompatActivity.java:259)
        at mate.files.welcome_theme_screen.<init>(welcome_theme_screen.java:19)
        at java.lang.Class.newInstance(Native Method)
        at android.app.AppComponentFactory.instantiateActivity(AppComponentFactory.java:69)
        at androidx.core.app.CoreComponentFactory.instantiateActivity(CoreComponentFactory.java:45)
        at android.app.Instrumentation.newActivity(Instrumentation.java:1224)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3340)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3614) 
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:86) 
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108) 
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2199) 
        at android.os.Handler.dispatchMessage(Handler.java:112) 
        at android.os.Looper.loop(Looper.java:216) 
        at android.app.ActivityThread.main(ActivityThread.java:7625) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:524) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:987) 

1 ответ 1

1

Вроде NullPointerException - самая тривиальнейшая ошибка. Нельзя вызывать findViewById в конструкторе активити, переносите это в onCreate.

Ваш ответ

Нажимая на кнопку «Отправить ответ», вы соглашаетесь с нашими пользовательским соглашением, политикой конфиденциальности и политикой о куки

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.