尝试在空对象引用上调用虚拟方法'android.content.Context.getResources()'

27

我尝试使用一个片段来打开数据库,然而当我点击按钮开始搜索时,程序意外终止并显示以下错误:

java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference
     at android.widget.Toast.<init>(Toast.java:102)
     at android.widget.Toast.makeText(Toast.java:259)
     at com.example.nkwpy.myapplication.MainFragment.query(MainFragment.java:176)
     at com.example.nkwpy.myapplication.MainFragment.access$000(MainFragment.java:46)
     at com.example.nkwpy.myapplication.MainFragment$queryListener.onClick(MainFragment.java:161)
     at android.view.View.performClick(View.java:5207)
     at android.view.View$PerformClick.run(View.java:21177)
     at android.os.Handler.handleCallback(Handler.java:739)
     at android.os.Handler.dispatchMessage(Handler.java:95)
     at android.os.Looper.loop(Looper.java:148)
     at android.app.ActivityThread.main(ActivityThread.java:5458)
     at java.lang.reflect.Method.invoke(Native Method)
     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:738)
     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:628)

MainFragment:

package com.example.nkwpy.myapplication;
import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.view.View.OnClickListener;
import android.widget.Toast;



/**
 * A simple {@link Fragment} subclass.
 * Activities that contain this fragment must implement the
 * {@link MainFragment.OnFragmentInteractionListener} interface
 * to handle interaction events.
 * Use the {@link MainFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class MainFragment extends Fragment {
    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    MainActivity parent=(MainActivity) getActivity();
    SQLiteDatabase test;
    DBManager dbHelper;


    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;

    private EditText N;
    private EditText Z;
    private EditText R_A;
    private Button queryBtn;

    private OnFragmentInteractionListener mListener;

    public MainFragment() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment MainFragment.
     */
    // TODO: Rename and change types and number of parameters
    public static MainFragment newInstance(String param1, String param2) {
        MainFragment fragment = new MainFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }


    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view =inflater.inflate(R.layout.fragment_main, container, false);
        N=(EditText)view.findViewById(R.id.neuton);
        Z=(EditText)view.findViewById(R.id.proton);
        queryBtn = (Button)view.findViewById(R.id.query);
        queryBtn.setOnClickListener(new queryListener());
        R_A=(EditText)view.findViewById(R.id.result);


        dbHelper = new DBManager(getActivity());
        dbHelper.openDatabase();
        dbHelper.closeDatabase();
        return view;
    }

    // TODO: Rename method, update argument and hook method into UI event
    public void onButtonPressed(Uri uri) {
        if (mListener != null) {
            mListener.onFragmentInteraction(uri);
        }
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        if (context instanceof OnFragmentInteractionListener) {
            mListener = (OnFragmentInteractionListener) context;
        } else {
            throw new RuntimeException(context.toString()
                    + " must implement OnFragmentInteractionListener");
        }
    }

    @Override
    public void onDetach() {
        super.onDetach();
        mListener = null;
    }

    /**
     * This interface must be implemented by activities that contain this
     * fragment to allow an interaction in this fragment to be communicated
     * to the activity and potentially other fragments contained in that
     * activity.
     * <p/>
     * See the Android Training lesson <a href=
     * "http://developer.android.com/training/basics/fragments/communicating.html"
     * >Communicating with Other Fragments</a> for more information.
     */
    public interface OnFragmentInteractionListener {
        // TODO: Update argument type and name
        void onFragmentInteraction(Uri uri);
    }

    class queryListener implements OnClickListener{

        @Override
        public void onClick(View v) {
            //
            query();
            test.close();
        }
    }
    private void query() {

        try {
            String string1 = N.getText().toString();
            String string2 = Z.getText().toString();
            String sql = "select * from sly4 where N=" + string1 + " and Z=" + string2;
            Cursor cursor =test.rawQuery(sql, null);
            cursor.moveToFirst();
            String r = cursor.getString(cursor.getColumnIndex("value"));
            R_A.setText(r);
        } catch (Exception e) {
            Toast.makeText(parent, "Please check the number you entered", Toast.LENGTH_LONG).show();
        }
    }
}

类 DBManager:

import java.io.File;
 import java.io.FileNotFoundException;
 import java.io.FileOutputStream;
 import java.io.IOException;
 import java.io.InputStream;
 import android.content.Context;
 import android.database.sqlite.SQLiteDatabase;
 import android.os.Environment;
 import android.util.Log;

public class DBManager {
    private final int BUFFER_SIZE = 400000;
    public static final String DB_NAME = "main.db"; 
    public static final String PACKAGE_NAME = "com.example.nkwpy.myapplication";
    public static final String DB_PATH = "/data"
            + Environment.getDataDirectory().getAbsolutePath() + "/"
            + PACKAGE_NAME;

    private SQLiteDatabase database;
    private Context context;

    DBManager(Context context) {
        this.context = context;
    }

    public void openDatabase() {
        this.database = this.openDatabase(DB_PATH + "/" + DB_NAME);
    }

    private SQLiteDatabase openDatabase(String dbfile) {
        try {
            if (!(new File(dbfile).exists())) {
                InputStream is = this.context.getResources().openRawResource(R.raw.main);
                FileOutputStream fos = new FileOutputStream(dbfile);
                byte[] buffer = new byte[BUFFER_SIZE];
                int count = 0;
                while ((count = is.read(buffer)) > 0) {
                    fos.write(buffer, 0, count);
                }
                fos.close();
                is.close();
            }
            SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(dbfile,
                    null);
            return db;
        } catch (FileNotFoundException e) {
            Log.e("Database", "File not found");
            e.printStackTrace();
        } catch (IOException e) {
            Log.e("Database", "IO exception");
            e.printStackTrace();
        }
        return null;
    }
    public void closeDatabase() {
        this.database.close();
    }
}

顺便说一下,我在MainActivity里使用了关于DBManager的代码,并且它成功了。当我将代码复制到像上面那样的片段中时,它失败了。我该怎么办?


请提供您在哪个类和哪一行遇到了这个错误,以便我们能够提出建议。 - Jay Rathod
你的代码片段只应负责与UI相关的操作 - 在此处放置数据库访问是不好的实践。相反,尝试回调到活动中为您执行该工作。在这里阅读更多信息:http://simpledeveloper.com/how-to-communicate-between-fragments-and-activities/ - Mike Baxter
6个回答

33

onAttach() 之前和 onDetach() 之后都不能执行 MainActivity parent=(MainActivity) getActivity();

因为这将在片段实例化时执行。方法 getActivity 将始终返回 null。另外,尽可能不要在片段中保存对 Activity 的引用。如果不正确地对引用进行清空,可能会导致内存泄漏。

尽可能使用 getActivity()getContext()

将您的 Toast 更改如下:

Toast.makeText(getContext(), "Please check the number you entered", 
Toast.LENGTH_LONG).show(); 
Toast.makeText(getActivity(), "Please check the number you entered", 
Toast.LENGTH_LONG).show();

1
使用 getActivity() 还是 getContext() 哪个更好? - j.elmer
@GowthamanM 你什么时候调用上述方法?是在onDetach()之后还是在onAttach()之前调用的? - Kalpesh Patel
@KalpeshPatel 我在 catch (OutOfMemoryError e){ Log.e("outofmerrorexeceptions"," "+e.toString()); Toast.makeText(getContext(),"Photo "+e.toString(),Toast.LENGTH_SHORT).show(); } 中调用。 - Gowthaman M
@KalpeshPatel 在 Oncreateview() 之外的片段内部。 - Gowthaman M
onCreateView() 之外是什么意思?你会在 onCreateView() 之后调用它吗? 你能通过在 OutOfMemory 异常的 catch 调用中调用 isAdded() 来检查片段状态吗? 如果 isAdded() 返回 false,那么你无法做太多事情。此时你的片段未附加到 Activity 中,并且会在 getActivity()getContext() 调用中获得 null - Kalpesh Patel

2

我也遇到了同样的错误!它在MainActivity中运行良好,但在我的Fragment类中却不行。

我通过以下两个步骤解决了这个问题:

1) 在Fragment类中,不要像在Activity类中那样全局声明任何内容。

例如:

public class HomeFragment extends Fragment {

// DON'T WRITE THIS HERE LIKE YOU DO IN ACTIVITY
String myArray[] = getResources().getStringArray();

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
 ...
}

相反,将任何内容写在

onCreateView()

方法内。

2) 首先使用

getActivity()

方法,然后再使用

getResource() 方法。

  public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

//WRITE LIKE THIS
 String myArray[] = getActivity().getResources().getStringArray(R.array.itemsList);

...
}

1

我也遇到了这个问题,花了一整天来调试。 mediaPlayer = MediaPlayer.create(this, R.raw.sound); 简单地用活动引用(unityActivity)替换“this”关键词(如果是Unity插件),我就解决了它。


0

我在我的Android活动中遇到了这个问题。错误是我的Toast没有获取上下文,或者我更喜欢称之为活动的引用。

以前的代码:

Toast.makeText(context, "Session Expire..!", Toast.LENGTH_SHORT).show();

修正后的代码:

Toast.makeText(activityname.this, "Session Expire..!", Toast.LENGTH_SHORT).show();

0

像这样声明一个方法:

private boolean checkContext(Context context) {
    if(context != null)
        return true;
    else
        return false;
}

现在,如果活动执行此方法 onCreate() 或 onStart(),可以这样:
if(checkContext(getApplicationContext() or NameActivity.this))
{
   /// do stuff that hang from context
}

如果是在Fragment中,可以在onCreateView()或onStart()方法中执行如下代码:
if(checkContext(getContext()))
{
   //dostuf that hang from context   
}

0

在这种情况下,你所需要做的仅仅是:

创建上下文; 将其分配给片段上下文,然后在使用创建的上下文之前检查非空性。

就像这样...

    public class MyHome extends Fragment {
    
    **Context context;**
    
    public MyHome() {
        

    }


    public static MyHome newInstance() {
        return new MyHome();
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        **this.context = getContext();**
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_my_home, container, false);

if(context !=null){
   Toast.makeText(context,"Hello homepage",Toast.LENGHT_LONG).show();
}

}
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接