my alertdialogs提供空指针异常为什么?

u7up0aaq  于 2021-08-20  发布在  Java
关注(0)|答案(0)|浏览(178)

我希望textview根据用户在警报对话框中选择的语言(西班牙语或英语)更改文本。这是代码,

package com.example.alertdialogsdemo;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    TextView textView = findViewById(R.id.textView);
    SharedPreferences sharedPreferences = this.getSharedPreferences("com.example.alertdialogsdemo", Context.MODE_PRIVATE);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        new AlertDialog.Builder(this)
                .setIcon(android.R.drawable.ic_btn_speak_now)
                .setTitle("Language")
                .setMessage("Choose the language you wanna continue with.")
                .setPositiveButton("ENGLISH", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        //Set English
                        setLanguage("English");
                    }
                })
                .setNegativeButton("SPANISH", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        //Set Spanish
                        setLanguage("Spanish");
                    }
                })
                .show();
    }

    public void setLanguage(String language) {
        sharedPreferences.edit().putString("language", language).apply();
        textView.setText(language);
    }
}

但这是我收到的错误,我认为这是nullpointerexception,但我如何处理它?请帮助!

com.example.alertdialogsdemo E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.alertdialogsdemo, PID: 6508
    java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.alertdialogsdemo/com.example.alertdialogsdemo.MainActivity}: 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:3365)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:223)
        at android.app.ActivityThread.main(ActivityThread.java:7656)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题