gpt4 book ai didi

java - JPE : Attempt to invoke virtual method 'android.content.res.Resources' on a null object reference

转载 作者:行者123 更新时间:2023-12-04 23:58:53 28 4
gpt4 key购买 nike

好吧好吧我已经阅读了这些不同的帖子:java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()'还有这个 What is a NullPointerException, and how do I fix it?还有这个java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()并且已经尝试了很多东西。但我的问题仍然存在。也许我错过了什么,我是新人,我正在尽力而为,请保持友善。

我在 fragment “DataSetFragment1”中调用 SMS 类的方法

有人可以帮我吗,谢谢

这是我的 SMS 类的代码:

public class Sms extends AppCompatActivity {
private String contact;
private String content;
private int type;
private int read;
private String date;

public void addSMS()
{
List<Sms> mesSms= new ArrayList<>();
InputStream is = getResources().openRawResource(R.raw.sms1);
BufferedReader reader = new BufferedReader(
new InputStreamReader(is, Charset.forName("UTF-8"))
);
}
}

这是我的 fragment 的代码:

public class Dataset1Fragment extends Fragment {
private void createOnClicBtnEnreg(){
Sms mesSms = new Sms();
mesSms.addSMS();
}
@Override
public void onViewCreated(@NonNull final View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
view.findViewById(R.id.btnEnreg).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
btnEnreg = view.findViewById(R.id.btnEnreg);

Context myContext = getContext();
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.Q) {
RoleManager roleManager = myContext.getSystemService(RoleManager.class);
// check if the app is having permission to be as default SMS app
assert roleManager != null;
boolean isRoleAvailable = roleManager.isRoleAvailable(RoleManager.ROLE_SMS);
if (isRoleAvailable){
// check whether your app is already holding the default SMS app role.
boolean isRoleHeld = roleManager.isRoleHeld(RoleManager.ROLE_SMS);
if (!isRoleHeld){
Intent roleRequestIntent = roleManager.createRequestRoleIntent(RoleManager.ROLE_SMS);
startActivityForResult(roleRequestIntent,5);
}
}
} else {
Intent intent = new Intent(Telephony.Sms.Intents.ACTION_CHANGE_DEFAULT);
intent.putExtra(Telephony.Sms.Intents.EXTRA_PACKAGE_NAME, myContext.getPackageName());
startActivityForResult(intent, 5);
}

if (hasPermissions(getContext(),Permissions)){
createOnClicBtnEnreg();
Toast.makeText(getContext(), "Insertion OK", Toast.LENGTH_SHORT).show();

}
}

这是我的日志:

E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.data.myApp, PID: 12080
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:93)
at android.view.ContextThemeWrapper.getResourcesInternal(ContextThemeWrapper.java:134)
at android.view.ContextThemeWrapper.getResources(ContextThemeWrapper.java:128)
at androidx.appcompat.app.AppCompatActivity.getResources(AppCompatActivity.java:566)
at com.example.testbackup.Sms.addSMS(Sms.java:36)
at com.example.testbackup.ui.dataset1.Dataset1Fragment.createOnClicBtnEnreg(Dataset1Fragment.java:321)
at com.example.testbackup.ui.dataset1.Dataset1Fragment.access$100(Dataset1Fragment.java:61)
at com.example.testbackup.ui.dataset1.Dataset1Fragment$2.onClick(Dataset1Fragment.java:251)

编辑:这段代码很好,遵循了 Hata 和 Ivo Beckers 的响应

public class Sms{
private String contact;
private String contenu;
private int type;
private int read;
private String date;


public void addSMS(Context myContext)
{
List<Sms> mesSms= new ArrayList<>();
InputStream is = myContext.getResources().openRawResource(R.raw.sms1);
BufferedReader reader = new BufferedReader(
new InputStreamReader(is, Charset.forName("UTF-8"))
);

最佳答案

修改addSMS如下:

public void addSMS(Context context) {
List<Sms> mesSms= new ArrayList<>();
InputStream is = context.getResources().openRawResource(R.raw.sms1);
BufferedReader reader = new BufferedReader(
new InputStreamReader(is, Charset.forName("UTF-8"))
);
}

并在您从 createOnClicBtnEnreg 使用它时传递 Context 实例:

private void createOnClicBtnEnreg(){
Sms mesSms = new Sms();
mesSms.addSMS(getContext());
}

注意:此答案仅适用于显示的错误。如果错误已清除,即使另一个错误出现并仍然存在(应作为另一个问题发布),也应关闭此问答。

关于java - JPE : Attempt to invoke virtual method 'android.content.res.Resources' on a null object reference,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69810466/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com