2015-07-11 129 views
0

我使用此項目https://github.com/googlesamples/android-RecyclerView作爲參考,以瞭解RecyclerView,並通過MLProgrammer-Cim here來實現答案以處理onClick事件。使用RecyclerView進行Android語音識別

在我CustomAdapter活動我試圖啓動一個方法speakNow();這是另一個活動MainActivity

public class CustomAdapter extends RecyclerView.Adapter<CustomAdapter.ViewHolder> { 
private static final String TAG = "CustomAdapter"; 
... 

public static class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener { 
    ... 
    public MyViewHolderClicks mListener; 
    private Context context; 
    MainActivity ma = new MainActivity(); 

//  FragmentTransaction ft =  ((FragmentActivity)context).getSupportFragmentManager().beginTransaction(); 
//  SampleActivityBase fragment = (SampleActivityBase)ft.findFragmentByTag("SampleActivityBase"); 


    public ViewHolder(View myv, MyViewHolderClicks listener) { 

     super(myv); 
     mListener = listener; 
     .... 
     imageIcon.setOnClickListener(this); 
     myv.setOnClickListener(this); 
     this.context =context; 


    } 
    @Override 
    public void onClick(View v) { 
     int position = getLayoutPosition(); 
     if (v instanceof Button){ 
      mListener.onTomato((Button) v, getLayoutPosition()); 
      switch (v.getId()) { 

       case R.id.button_for_cardview: 
        ma.speakNow(); 
..... 
} 
} 

MainActivity

public class MainActivity extends SampleActivityBase { 
.... 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    .... 
    //Intent intent = new Intent(getApplicationContext(),  CustomAdapter.class); 
    //startActivityForResult(new Intent(this, CustomAdapter.class), checka); 
    if (savedInstanceState == null) { 
     FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); 
     RecyclerViewFragment fragment = new RecyclerViewFragment(); 
     transaction.replace(R.id.sample_content_fragment, fragment); 
     transaction.commit(); 
    } 
} 

@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 

    if (requestCode == checka && resultCode == RESULT_OK){ 
     ArrayList<String> results = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS); 
     lv.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, results)); 

     ..... 
     } 

    } 
    super.onActivityResult(requestCode, resultCode, data); 


} 

protected void speakNow() { 
    Intent a = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH); 
    a.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM); 
    a.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 6); 
    a.putExtra(RecognizerIntent.EXTRA_PROMPT, GlobalVariables.getInstance().getRightAnswer1()); 
    startActivityForResult(a, checka); 
} 
.... 

}

這返回一個NullPointerException在 MainActivity.speakNow(MainActivity.java:478)這是startActivityForResult(a, checka); 和適配器$ ViewHolder.onClick(CustomAdapter.java:287),這是ma.speakNow();

我讀過的各種「呼籲其他活動方法返回的NPE」,提問和回答,也不太清楚如何將這些應用到我的情況下,因爲MainActivity擴展了SampleActivityBase這是一個擴展FragmentActivity的公共抽象類。

我在哪裏/如何初始化MainActivity ma = new MainActivity();所以它不是零?由於

回答

0

異常在startActivityForResult拋出,那是因爲你通過MainActivity ma = new MainActivity();創建活動是從活動不同的系統中創建,創建的活動有什麼也沒有,讀的Activity源代碼來執行startActivityForResult,它取決於一些成員變量。所以你得到了空點異常。您可以在您的適配器中添加一個setter,將Activity的參考傳遞給適配器

public void setActivity(Activity aty) { 
    this.aty = aty; 
} 
+0

感謝您的回覆。我確定這是一個非常完整的答案,但由於我的限制,我不明白如何將活動的參考傳遞給適配器,以及如何將它設置爲觸發speakNow()方法..您可以詳細說明使用我發佈的代碼上面..謝謝 – Mcorv

+0

好吧,找出你的意思..再次感謝! – Mcorv