android.view.LayoutInflater中的java.lang.NullPointerException

时间:2015-04-27 13:37:33

标签: android android-layout nullpointerexception layout-inflater

我有一个AsyncTask用于从数据库接收数据,这是我的完整代码:

  

line26

 public class Tabasync2 extends AsyncTask<String, String, String> {
    static Posdent tempposdent;
    ...
    ArrayList<Posdent> aResults = new ArrayList<Posdent>();

public static Posdent send() {
    return tempposdent;
}

@Override
protected void onPreExecute() {
  ...}

@Override
protected String doInBackground(String... sText) {
   ...
   ...
        Posdent P = getList(result);
   ...
    return result;
}
protected void onPostExecute(String v) {
    super.onPostExecute(result);
   Tabs t=new Tabs();
  

行:99

    t.calling(aResults);
}

public Posdent getList(String name) {
    tempposdent = new Posdent();
    String name1 = null;
    try {
        JSONArray Jarray = new JSONArray(result);

        int k;
        for (k = 0; k < Jarray.length(); k++) {
            try {
                JSONObject Jasonobject = null;
                Jasonobject = Jarray.getJSONObject(k);
                String msg = Jasonobject.getString("message");

                name1 = Jasonobject.getString("s_name");
                Tabs t1=new Tabs();
                t1.testing(name1);
                tempposdent = new Posdent();

                tempposdent.setStudent_name(name1);
                tempposdent.setMessage(msg);
                for(int j=0;j<aResults.size();j++)
                {
                    if(aResults.get(j).hashCode()!=(tempposdent.hashCode())){
                        aResults.add(tempposdent);
                    }
                }
            } catch (JSONException js) {
                js.printStackTrace();
            }
        }
    } catch (Exception e) {
        Log.e("log_tag", "Error parsing data" + e.toString());

    }
    Tabs t=new Tabs();

    t.testing(name1);
    return tempposdent;
}
}

这是我新的logcat跟踪:

  java.lang.NullPointerException
        at android.view.LayoutInflater.from(LayoutInflater.java:211)
        at com.student.anurag.student_connect.Tabs$SearchResultAdapter.<init>(Tabs.java:144)
        at com.student.anurag.student_connect.Tabs.calling(Tabs.java:115)
        at com.student.anurag.student_connect.Tabasync2.onPostExecute(Tabasync2.java:99)
        at com.student.anurag.student_connect.Tabasync2.onPostExecute(Tabasync2.java:26)
        at android.os.AsyncTask.finish(AsyncTask.java:632)
        at android.os.AsyncTask.access$600(AsyncTask.java:177)
        at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:645)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:136)
        at android.app.ActivityThread.main(ActivityThread.java:5086)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
        at dalvik.system.NativeStart.main(Native Method)

此外,我想在主要活动中包含这一点,当我按下按钮时会发生两件事情:

  • 首先AsyncTask发布数据
  • 其他AsyncTask检索数据; 像这样:

    switch(v.getId()) {
    
           case R.id.postb:
    
           new task1().execute();
    
           new Tabasync2().execute(ep.getText().toString());
    
           break;
    
           }
    

这是tabs class snippet:

  

第115行

public void calling(ArrayList<Posdent> aResults) {
    searchResults.setAdapter(new SearchResultAdapter(context, aResults));
}

 class SearchResultAdapter extends BaseAdapter {

    int count;
    Typeface type;
    Context context;
    private LayoutInflater layoutInflater;
    private ArrayList<Posdent> sdetails = new ArrayList<Posdent>();

    public SearchResultAdapter(Context context, ArrayList<Posdent> aResults) {
  

第144行

        layoutInflater = LayoutInflater.from(this.context);
        this.sdetails = aResults;
        this.count = aResults.size();
        this.context = context;
        type = Typeface.createFromAsset(context.getAssets(), "fonts/book.TTF");
        Log.d("Inside SearchResultAdapter", "" + count);
    }

1 个答案:

答案 0 :(得分:0)

this.context初始化后分配

layoutInflater。改变

  layoutInflater = LayoutInflater.from(this.context);

  layoutInflater = LayoutInflater.from(context);