2015-02-14 2 views
0

Я новичок в андроиде, и это мой первый раз, когда я использую фрагменты. Когда этот фрагмент вызывается и пользователь ищет какой-либо вопрос в редакторе «search_question» и нажимает на «search_question_button», искомый вопрос должен быть передан следующему классу «PaperlessRegistrationApplication», однако приложение останавливается, когда приложение « ». getOpeContentSearchQuestion (userQuestion, this); " . Я буду очень признателен, если кто-нибудь сможет помочь в этом. Спасибо.NullPointerException в Android Studio

package com.redone.pplr.opecontent.ui; 

     import android.app.Activity; 
     import android.content.Context; 
     import android.os.Bundle; 
     import android.support.v4.app.ListFragment; 
     import android.text.TextUtils; 
     import android.util.Log; 
     import android.view.LayoutInflater; 
     import android.view.View; 
     import android.view.ViewGroup; 
     import android.widget.ArrayAdapter; 
     import android.widget.Button; 
     import android.widget.EditText; 
     import android.widget.ListView; 
     import android.widget.TextView; 

     import com.redone.pplr.R; 
     import com.redone.pplr.app.PaperlessRegistrationApplication; 
     import com.redone.pplr.opecontent.model.OpeContentModel; 

     import java.util.List; 


     /** 
     * Created by user on 2/12/2015. 
     */ 
     public class OpeContentFragment extends ListFragment implements OpeContentListener { 
      private PaperlessRegistrationApplication app; 
      private EditText search_question; 
      private TextView error; 
      private Button search_question_button; 
      private int visibility = View.VISIBLE; 
      private int gone = View.GONE; 
      private String userQuestion; 
      private ListView opeContentList; 
      protected List<OpeContentModel> arrayOfList = null; 
      protected RowAdapter objAdapter;//instance of the rowadapter class 


      public OpeContentFragment() { 
      } 

      @Override 
      public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
       View view = inflater.inflate(R.layout.ope_content_fragment, container, false); 
       search_question = (EditText) view.findViewById(R.id.search_question); 
       error = (TextView) view.findViewById(R.id.error); 
       search_question_button = (Button) view.findViewById(R.id.search_question_button); 
       opeContentList = (ListView) view.findViewById(android.R.id.list); 



       //when the search question button is clicked 
       search_question_button.setOnClickListener(new View.OnClickListener() { 
        @Override 
        public void onClick(View v) { 
         userQuestion = search_question.getText().toString(); 
         if (!TextUtils.isEmpty(userQuestion)) { 
          error.setVisibility(gone); 
          opeContentSearchQuestion(userQuestion); 

         } else { 
          error.setText("No Question was Searched"); 
          error.setVisibility(visibility); 
         } 
        } 
       }); 
       return view; 
      } 

      public void opeContentSearchQuestion(String userQuestion) { 
       Log.e("SearchQuestion", userQuestion); 
       app.getOpeContentSearchQuestion(userQuestion, this); 
      } 

      @Override 
      public void onSearchMostViewedQuestions(List<OpeContentModel> result) { 
       objAdapter = new RowAdapter(getActivity(), R.layout.ope_content_list_item, 
         arrayOfList); 
       opeContentList.setAdapter(objAdapter); 
      } 

      //custom adapter class implementation which reuses existing views as user scrolls 
      class RowAdapter extends ArrayAdapter<OpeContentModel> { 
       Context context; 
       protected List<OpeContentModel> items; 
       protected Activity activity; 
       protected int row;//list_item_products 
       protected OpeContentModel objBean; 

       public RowAdapter(Activity act, int resource, 
            List<OpeContentModel> arrayoflist) { 
        super(act, resource, arrayoflist); 
        this.activity = act; 
        this.row = resource; 
        this.items = arrayoflist; 
       } 

       //getview method is called for each item of ListView 
       @Override 
       public View getView(final int position, View convertView, 
            ViewGroup parent) { 
        //a++; 
        View view = convertView;// single child element of the listview 
        ViewHolder holder = null;// a holder to temp layouts 
        // if the convertView is null, create a new view 
        if (view == null) { 
         // create a new view 
         //inflate the layout for each item of listview 
         LayoutInflater inflater = (LayoutInflater) activity 
           .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
         view = inflater.inflate(row, null); 
         holder = new ViewHolder(); 
         view.setTag(holder); 
        } else { 
         // resue the row to display newly generated data/recycle 
         holder = (ViewHolder) view.getTag(); 
        } 

        if ((items == null) || ((position + 1) > items.size())) 
         return view; 
        objBean = items.get(position); 

        holder.question = (TextView) view.findViewById(R.id.ope_content_question); 
        holder.answer = (TextView) view.findViewById(R.id.ope_content_answer); 

        if(holder.question != null && objBean.getQuestion() != null){ 
         holder.question.setText(objBean.getQuestion()); 
        } 

        if(holder.answer != null && objBean.getAnswer() != null){ 
         holder.answer.setText(objBean.getAnswer()); 
        } 
        return view; 
       } 


       // create a temporary textview/imageview/varaible for store id by id 
       public class ViewHolder { 
        public TextView question; 
        public TextView answer; 
       } 
      } 
     } 

Я получаю следующее сообщение об ошибке

02-14 11:11:50.719 1238-1238/com.redone.pplr D/dalvikvm﹕ GC_FOR_ALLOC freed 170K, 8% free 6771K/7344K, paused 5ms, total 5ms 
    02-14 11:11:50.727 1238-1238/com.redone.pplr W/IInputConnectionWrapper﹕ getTextAfterCursor on inactive InputConnection 
    02-14 11:11:50.819 1238-1238/com.redone.pplr D/pplr_SalesKitCntentLst﹕ getContent|response|{"contentList":[],"message":null,"messageCode":null,"status":"success"} 
    02-14 11:11:50.831 1238-1241/com.redone.pplr D/dalvikvm﹕ GC_CONCURRENT freed 324K, 8% free 6856K/7384K, paused 1ms+1ms, total 6ms 
    02-14 11:11:50.839 1238-1238/com.redone.pplr W/IInputConnectionWrapper﹕ finishComposingText on inactive InputConnection 
    02-14 11:11:50.943 1238-1238/com.redone.pplr W/IInputConnectionWrapper﹕ finishComposingText on inactive InputConnection 
    02-14 11:11:55.531 1238-1238/com.redone.pplr I/GCM﹕ Error :SERVICE_NOT_AVAILABLE 
    02-14 11:11:57.883 1238-1238/com.redone.pplr E/SearchQuestion﹕ i 
    02-14 11:11:57.883 1238-1238/com.redone.pplr D/AndroidRuntime﹕ Shutting down VM 
    02-14 11:11:57.887 1238-1238/com.redone.pplr W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0xa613b908) 
    02-14 11:11:57.891 1238-1238/com.redone.pplr E/AndroidRuntime﹕ FATAL EXCEPTION: main 
     java.lang.NullPointerException 
       at com.redone.pplr.opecontent.ui.OpeContentFragment.opeContentSearchQuestion(OpeContentFragment.java:74) 
       at com.redone.pplr.opecontent.ui.OpeContentFragment$1.onClick(OpeContentFragment.java:61) 
       at android.view.View.performClick(View.java:4204) 
       at android.view.View$PerformClick.run(View.java:17355) 
       at android.os.Handler.handleCallback(Handler.java:725) 
       at android.os.Handler.dispatchMessage(Handler.java:92) 
       at android.os.Looper.loop(Looper.java:137) 
       at android.app.ActivityThread.main(ActivityThread.java:5041) 
       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:793) 
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 
       at dalvik.system.NativeStart.main(Native Method) 
+2

Возможный дубликат [Что такое Исключение нулевого указателя и как его исправить?] (Http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how -do-I-Fix-It) – Jens

ответ

1

app никогда не инициализирует. вы должны инициализировать этот объект.

Смежные вопросы