简体   繁体   English

刷新异步任务后尝试访问适配器时出现nullPointer异常

[英]nullPointer exception when trying to access adapter after asynctask refresh

I was following along with some videos on making a weather app and somehow ended up trying to launch my AsyncTask from a button pressed in a drop down from my actionbar. 我跟随着一些视频制作气象应用程序,最终以某种方式尝试通过从动作栏下拉菜单中按下的按钮启动AsyncTask Right now the asynctask is a separate class and the application loads the aynctask fine the first time but when i try to refresh and re launch the async task from a button in my actionbar menu the app crashes with a NullPointerException : 现在,asynctask是一个单独的类,应用程序在第一次加载aynctask时就很好,但是当我尝试刷新并从操作栏菜单中的按钮重新启动异步任务时,应用程序崩溃,并出现NullPointerException

07-18 00:45:05.615    5849-5849/johnpark.sunshine E/AndroidRuntime﹕ FATAL EXCEPTION: main
    java.lang.NullPointerException
            at johnpark.sunshine.MainActivityFragment.processFinish(MainActivityFragment.java:70)
            at johnpark.sunshine.ForeCastLoader.onPostExecute(ForeCastLoader.java:39)
            at johnpark.sunshine.ForeCastLoader.onPostExecute(ForeCastLoader.java:23)
            at android.os.AsyncTask.finish(AsyncTask.java:631)
            at android.os.AsyncTask.access$600(AsyncTask.java:177)
            at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:4745)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
            at dalvik.system.NativeStart.main(Native Method)

Its getting a nullPointer exception when trying to access the adapter again. 尝试再次访问适配器时,它得到nullPointer异常。 Essentially there is a refresh button in my drop down in my Main activity that extends ActionBar. 本质上,我的主活动下拉菜单中有一个刷新按钮,用于扩展ActionBar。 When i click that button it calls a method from the MainActivityFragment class that calls the execute method of the ForeCastLoader asynctask . 当我单击该按钮时,它将从MainActivityFragment类中调用一个方法,该类将调用ForeCastLoader asynctask的execute方法。 In the onPostExecute() method the results are passed back to the fragment through a processFinish() method. onPostExecute()方法中,结果通过processFinish()方法传递回片段。 Back in that method in ActivityFragment, when trying to use the adapter i get a null pointer exception. 回到ActivityFragment中的该方法中,当尝试使用适配器时,我得到了一个空指针异常。 Any ideas as to what im doing wrong? 关于我做错了什么的任何想法?

MainActivity 主要活动

public class MainActivity extends ActionBarActivity
                implements OnNavigationListener, android.support.v7.app.ActionBar.OnNavigationListener {

private final String TAG = this.getClass().getSimpleName();
private boolean mNaviFirstHit = true;
MainActivityFragment mfa;

@TargetApi(Build.VERSION_CODES.HONEYCOMB)
@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    String[] dropdownValues = getResources().getStringArray(R.array.nav_list);

    android.support.v7.app.ActionBar actionBar = getSupportActionBar();

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(actionBar.getThemedContext(),
            android.R.layout.simple_spinner_item, android.R.id.text1,
            dropdownValues);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

    actionBar.setDisplayShowTitleEnabled(false);
    actionBar.setNavigationMode(NAVIGATION_MODE_LIST);
    actionBar.setListNavigationCallbacks(adapter, this);
}

@Override
public boolean onNavigationItemSelected(int position, long id) {
    if (mNaviFirstHit) {
        mNaviFirstHit = false;
        return true;
    }

    if(position==1)
    {
        Intent in = new Intent(getApplicationContext(),SettingsActivity.class);
        startActivity(in);
    }
    if(position==2)
    {
        mfa = new MainActivityFragment();
        mfa.update();
    }
    return true;
}
}

MainActivityFragment MainActivityFragment

public class MainActivityFragment extends Fragment implements AsyncResponse{

private ArrayList <String> list = new ArrayList<>();
static ListView lv;
ForeCastLoader  f = new ForeCastLoader(); 
ArrayAdapter<String> adapter;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);       
    f.delegate = this;
    adapter = new ArrayAdapter<String>(getActivity().getApplicationContext(),
            R.layout.list_item_forecast,
            R.id.list_item_forecast_textview, list);

    f.execute("94043");
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState)
{
    View v = inflater.inflate(R.layout.fragment_main, container, false);
    lv = (ListView)v.findViewById(R.id.listview_forecast);
    lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Intent in = new Intent(getActivity().getApplicationContext(), DetailView.class);
            in.putExtra("tempData", adapter.getItem(position));
            startActivity(in);
        }
    });
    lv.setAdapter(adapter);
    return v;
}

public void update()
{
    f.delegate = this;
    f.execute("97865");
}

public void processFinish(String[] output)
{
    list.clear();
    for(String s:output)
    {
        list.add(s);
    }
    adapter.notifyDataSetChanged();
    lv.setAdapter(adapter);
}

I left out the aSyncTask class because i already know its returning values correctly and i dont think the problem could be anywhere there. 我省略了aSyncTask类,因为我已经正确地知道了它的返回值,而且我认为问题可能不在任何地方。 Thanks for your help, im sure its something im just overlooking. 感谢您的帮助,我确定它只是被忽略了。

Well, I can see you create a new MainActivityFragment when the NavigationItem clicked. 好吧,我可以看到您在单击NavigationItem时创建了一个新的MainActivityFragment In this way the newly created MainActivityFragment is not the one that you added to your MainActivity when you first launch the app. 这样,新创建的MainActivityFragment并不是您首次启动应用程序时添加到MainActivity的对象。 The new one's onCreate method is not called, so you can't access the adapter in it. 新的onCreate方法不会被调用,因此您无法在其中访问adapter

The right way to access a previously added Fragment in an Activity is to use the findFragmentByTag method of the FragmentManager . 访问Activity先前添加的Fragment的正确方法是使用FragmentManagerfindFragmentByTag方法。

I assume you add your MainActivityFragment with a tag like this. 我假设您使用这样的标签添加MainActivityFragment

transaction.add(id, maf, MainActivityFragmentTag);

After adding it, you can access it like this. 添加后,您可以像这样访问它。

if(position==2)
{
    mfa = getSupportFragmentManager().findFragmentByTag(MainActivityFragmentTag);
    mfa.update();
}

当您单击刷新按钮时,我假设您正在position =2 MainActivity中调用onNavigationItemSelected()方法。在if条件下,您正在创建MainActivityFragment的新实例并调用它的update()您的适配器正在在MainActivityFragment中的createView()方法中初始化,由于您尚未在此新片段实例上调用show() ,因此不会调用它。请尝试在现有片段本身上调用update()方法。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM