Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/196.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Android:传递给方法时,上下文为null_Java_Android - Fatal编程技术网

Java Android:传递给方法时,上下文为null

Java Android:传递给方法时,上下文为null,java,android,Java,Android,在我的应用程序类中,InboxTabFragment的实例化如下所示: InboxTabFragment.java public class InboxFragmentTab extends ListFragment implements LoaderManager.LoaderCallbacks<Cursor> { private static final Uri SMS_CONTENT_URI = Uri.parse("content://sms"); private stati

在我的应用程序类中,InboxTabFragment的实例化如下所示:

InboxTabFragment.java

public class InboxFragmentTab extends ListFragment implements LoaderManager.LoaderCallbacks<Cursor> {
private static final Uri SMS_CONTENT_URI = Uri.parse("content://sms");
private static final Uri SMS_INBOX_CONTENT_URI = Uri.withAppendedPath(SMS_CONTENT_URI, "inbox");
private static final String COLUMNS[] = new String[] {"person", "address", "body", "date", "type"};

SimpleCursorAdapter adapter;
Context context;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.inbox_layout, container, false);
    getLoaderManager().initLoader(0, null, this);
    return rootView;
}

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

@Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
    CursorLoader loader = new CursorLoader(this.getActivity(), SMS_INBOX_CONTENT_URI, COLUMNS,
            null, null, null);
    return loader;
}

@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor c) {
    ArrayList<Sms> result = new ArrayList<Sms>();

    if(c!=null) {
        for(boolean hasData = c.moveToFirst(); hasData; hasData=c.moveToNext()){
            boolean isSent = c.getInt(c.getColumnIndex("type")) == 2;
            String address = c.getString(c.getColumnIndex("address"));

            String sender = ContactManager.getContactName(context, address);
            String receiver = "Me";

            Sms sms = new Sms(address, c.getString(c.getColumnIndex("body")), new Date(Long.parseLong(c.getString(c.getColumnIndex("date")))), isSent ? sender : receiver);
            sms.setSender(isSent ? receiver : sender);
            result.add(sms);
        }
    }
    SmsAdapter adapter = new SmsAdapter(getActivity(), R.layout.row, result);
    setListAdapter(adapter);
}

@Override
public void onLoaderReset(Loader<Cursor> loader) {

}
方法参数中传递的上下文为null,我不知道如何处理这个问题。
如果有人愿意帮我解决这个问题,我将不胜感激。

试试
context=this.getActivity().getApplicationContext()尝试
context=this.getActivity().getApplicationContext()您从未将at值赋值给
上下文对象。加:


context=this.getActivity().getApplicationContext()

您从未将at值赋值给
上下文
对象。加:


context=this.getActivity().getApplicationContext()

尝试传递
getActivity()
而不是
context
使用
getApplicationContext()
或你的活动类的
this
尝试传递
getActivity()
而不是
context
使用
getApplicationContext()
或你的活动类的
this
String sender = ContactManager.getContactName(context, address);