简体   繁体   English

Android:传递给方法时上下文为空

[英]Android: Context is null when passed to method

In my application class InboxTabFragment is instantiate as like below: 在我的应用程序类中,InboxTabFragment的实例化如下所示:

InboxTabFragment.java 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) {

}

Problem occurs in line: 问题发生在网上:

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

Context passed in method parameter is null and I have no idea how to deal with this. 方法参数中传递的上下文为null,我不知道如何处理。 I'll be grateful if someone would like to help me resolve this problem. 如果有人想帮助我解决此问题,我将不胜感激。

Try context = this.getActivity().getApplicationContext(); 尝试context = this.getActivity().getApplicationContext(); before using it. 在使用它之前。

You are never assigning at value to the context object. 您永远不会将值分配给context对象。 Add: 加:

context = this.getActivity().getApplicationContext();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 传递给getAssets()方法的上下文为null - Context being passed into method for getAssets() is null 尝试通过java类访问活动中的方法时,null对象引用上的android.content.Context.getPackageName()&#39; - android.content.Context.getPackageName()' on a null object reference, when trying to access a method in an activity through a java class 将值传递给Action类方法时,变量值将为null - variable value is taking as null when the value is passed to the Action class method 将null值传递给方法时自动抛出IllegalArgumentException - Automatically throwing IllegalArgumentException when null values are passed to a method 尝试在空对象引用上调用虚拟方法&#39;android.content.Context android.content.Context.getApplicationContext()&#39; - Attempt to invoke virtual method 'android.content.Context android.content.Context.getApplicationContext()' on a null object reference 传递的方法变量更改为null - method variable passed changes to null Android活动上下文为空 - Android Activity Context is Null Android上下文为空 - Android Context is become null Null 传递上下文时 - Null when passing the context 什么上下文名称应该传递给Glide方法? - What context name should be passed to Glide method?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM