1

我創建了一個Search Layout它好工作。但它讓我經常死機:Android - RecyclerView:沒有附加適配器;跳過佈局?

RecyclerView﹕ No adapter attached; skipping layout 

這裏是我的SearchActivity.java

public class SearchActivity extends Activity implements RadioGroup.OnCheckedChangeListener{ 
    public static final String DIR_SDCARD = Environment.getExternalStorageDirectory().getAbsolutePath(); 
    public static final String DIR_DATABASE = DIR_SDCARD + "/Android/data/"; 
    public ArrayAdapter adapter; 
    public Cursor cursor_id; 
    public Cursor cursor_final; 
    public static String PACKAGE_NAME; 
    EditText editText; 
    DB db = new DB(SearchActivity.this); 
    public Cursor cursor; 
    public SQLiteDatabase sql; 
    RecyclerView recList; 
    Context context; 
    RadioGroup radioGroup; 
    int RadioID = R.id.rbNormal; 
    int b = 0; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_search); 

     context = getApplicationContext(); 
     PACKAGE_NAME = getApplicationContext().getPackageName(); 
     CreateFile(); 
     db.GetPackageName(PACKAGE_NAME); 
     try { 
      db.CreateandOpenDataBase(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
     sql = db.openDataBase(); 

     editText = (EditText) findViewById(R.id.search); 

     final Button btnSearch = (Button) findViewById(R.id.btnSearch); 
     final TextView tv = (TextView) findViewById(R.id.text); 

     recList = (RecyclerView) findViewById(R.id.cardList); 
     recList.setHasFixedSize(true); 
     LinearLayoutManager llm = new LinearLayoutManager(this); 
     llm.setOrientation(LinearLayoutManager.VERTICAL); 

     radioGroup = (RadioGroup) findViewById(R.id.rgSearch); 
     radioGroup.setOnCheckedChangeListener(this); 

     recList.setLayoutManager(llm); 

     btnSearch.setOnTouchListener(new View.OnTouchListener() { 
      @Override 
      public boolean onTouch(View v, MotionEvent motionevent) { 
       if (editText.getText().length() >= 2){ 
        tv.setVisibility(View.VISIBLE); 
       } 
       int action = motionevent.getAction(); 
       if (action == MotionEvent.ACTION_UP) { 
        if (editText.getText().length() < 2) { 
         Toast.makeText(SearchActivity.this, "Please enter two character!", 
           Toast.LENGTH_SHORT).show(); 
         return true; 
        } else { 
         InputMethodManager im = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE); 
         im.hideSoftInputFromWindow(getWindow().getDecorView().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS); 


         ContactAdapter ca = new ContactAdapter(createList(editText.getText()),context); 
         if(ca != null){ 
          recList.setAdapter(ca); 
          tv.setVisibility(View.VISIBLE); 
          tv.setText(String.valueOf(b) + " " + getResources().getString(R.string.result)); 
         } 
        } 
       } 
       return false; 
      } 
     }); 
    } 


    private List<StructSearch> createList(Editable editable) { 
    b = 0; 
     String _Text = editable.toString(); 
     String sq = null; 
     if (RadioID == R.id.rbNormal) { 
      sq = "Question like '%"+_Text+"%'"; 
     } 
     else if (RadioID == R.id.rbAnd) { 
      String[] parts = Pattern.compile(" ", Pattern.LITERAL).split(_Text); 
      for(String Result : parts) { 
       sq += " Question like '%" + Result + "%' and"; 
      } 
      sq = sq.substring(4, sq.length()-3); 
     } else if (RadioID == R.id.rbOr) { 
      String[] parts = Pattern.compile(" ", Pattern.LITERAL).split(_Text); 
      for(String Result : parts) { 
       sq += " or Question like '%" + Result + "%'"; 
      } 
     } 

     List<StructSearch> result = new ArrayList<StructSearch>(); 
     try { 
      cursor = sql.rawQuery("SELECT * FROM WebSite_QaListDB WHERE "+sq, null); 
        if (cursor != null && cursor.moveToFirst()) { 
          do { 
           StructSearch ci = new StructSearch(); 
           ci.Q_QaID = cursor.getInt(cursor.getColumnIndex("QaID")); 
           ci.Q_Question = cursor.getString(cursor.getColumnIndex("Question")); 
           ci.Q_Answer = cursor.getString(cursor.getColumnIndex("Answer")); 
           ci.Q_Title = cursor.getString(cursor.getColumnIndex("Title")); 

           cursor_id = sql.rawQuery("SELECT Item2ID FROM WebSite_ReleatedDB WHERE Item1ID =" + ci.Q_QaID, null); 

           try { 
            if (cursor_id != null && cursor_id.moveToFirst()) { 
             do { 
              ci.R_Item2ID = cursor_id.getInt(cursor_id.getColumnIndex("Item2ID")); 
              cursor_final = sql.rawQuery("SELECT Title FROM WebSite_CategoryDB WHERE CategoryID =" + ci.R_Item2ID 
                + " AND parentID != 0" ,null); 
              try { 
               if (cursor_final != null && cursor_final.moveToFirst()){ 
                do { 
                 ci.C_Title = cursor_final.getString(cursor_final.getColumnIndex("Title")); 
                }while (cursor_final.moveToNext()); 
               } 
              }catch (Exception e){ 
               Log.i("xxx", "You have an error"); 
              }finally { 
               if (cursor_final != null){ 
                cursor_final.close(); 
               } 
              } 

             } while (cursor_id.moveToNext()); 
            } 
           }catch (Exception e){ 
            Log.i("xxx", "You have an error"); 
           }finally { 
            if (cursor_id != null) { 
             cursor_id.close(); 
            } 
           } 
           result.add(ci); 
           b++; 
          } while (cursor.moveToNext()); 
         adapter.notifyDataSetChanged(); 
        } 
     } catch (Exception e) { 
      Log.i("xxx", "You have an error"); 
     } finally { 
      if (cursor != null) { 
       cursor.close(); 
      } 
     } 
     return result; 
    } 

    private boolean CreateFile(){ 
     File file = new File(DIR_DATABASE + PACKAGE_NAME + "/BankDB"); 
     boolean flag =file.mkdirs(); 
     if(flag){ 
      Log.i("A_LOG","Create File"); 
      return true; 
     }else { 
      Log.e("A_LOG","Do not Create File"); 
      return false; 
     } 
    } 

    @Override 
    public void onCheckedChanged(RadioGroup group, int checkedId) { 
     RadioID = 0; 
     RadioID = checkedId; 
    } 
} 

所有的代碼,良好的工作,我認爲我的錯誤就是從這裏開始:

   if(ca != null){ 
        recList.setAdapter(ca); 
        ... 
       } 

如果您需要查看我的Adapter.javaDB.java或佈局,我可以顯示它。

我使用的是Android Studio

圖像0​​(搜索),我layout具有可選的搜索一個RadioGroupDataBase

layoutSearch

+0

好的,回答我的投票問題。 –

回答

0

而是這個

ContactAdapter CA =新ContactAdapter的(createList(EDITTEXT .getText()),上下文);

編寫此代碼

ContactAdapter CA =新 ContactAdapter(createList(editText.getText()),getActivity());

OR

ContactAdapter CA =新 ContactAdapter(createList(editText.getText()),SearchActivity.this);

OR

ContactAdapter CA =新 ContactAdapter(createList(editText.getText()),這一點);

我不確定,但它可能對您有所幫助。 OK,

+0

謝謝,但不行。不知道getActivity和這個。 –

+0

這意味着相同的錯誤? – Kinjal

+0

不,不要運行應用程序。 –

相關問題