Android 使用 RecyclerView 进行语音识别
Android Speech Recognition with RecyclerView
我正在使用这个项目 https://github.com/googlesamples/android-RecyclerView as reference to learn about RecyclerView, and implementing the answer by MLProgrammer-Cim here 来处理 onClick 事件。
在我的 CustomAdapter
activity 中,我正在尝试启动另一个 activity MainActivity
中的方法 speakNow();
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);
}
....
}
这 return 中的一个 NullPointerException
MainActivity.speakNow(MainActivity.java:478)
这是这个 startActivityForResult(a, checka);
和 Adapter$ViewHolder.onClick(CustomAdapter.java:287) 即 ma.speakNow();
我已经阅读了各种 "calling Methods from other activities returning NPE"、问题和答案,不太确定这些如何适用于我的案例,因为 MainActivity 扩展了 SampleActivityBase
,这是一个 public 抽象 class 它扩展了 FragmentActivity.
我需要在哪里/如何初始化 MainActivity ma = new MainActivity();所以它不为空?谢谢
在startActivityForResult中抛出异常,那是因为MainActivity ma = new MainActivity();
创建的Activity与系统创建的Activity不同,Activity你创建的里面什么都没有,看Activity
的源代码执行startActivityForResult
,这取决于你的某个成员variable.so你得到了空点异常。您可以在适配器中添加一个 setter,将 Activity 的引用传递给适配器
public void setActivity(Activity aty) {
this.aty = aty;
}
我正在使用这个项目 https://github.com/googlesamples/android-RecyclerView as reference to learn about RecyclerView, and implementing the answer by MLProgrammer-Cim here 来处理 onClick 事件。
在我的 CustomAdapter
activity 中,我正在尝试启动另一个 activity MainActivity
speakNow();
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);
}
....
}
这 return 中的一个 NullPointerException
MainActivity.speakNow(MainActivity.java:478)
这是这个 startActivityForResult(a, checka);
和 Adapter$ViewHolder.onClick(CustomAdapter.java:287) 即 ma.speakNow();
我已经阅读了各种 "calling Methods from other activities returning NPE"、问题和答案,不太确定这些如何适用于我的案例,因为 MainActivity 扩展了 SampleActivityBase
,这是一个 public 抽象 class 它扩展了 FragmentActivity.
我需要在哪里/如何初始化 MainActivity ma = new MainActivity();所以它不为空?谢谢
在startActivityForResult中抛出异常,那是因为MainActivity ma = new MainActivity();
创建的Activity与系统创建的Activity不同,Activity你创建的里面什么都没有,看Activity
的源代码执行startActivityForResult
,这取决于你的某个成员variable.so你得到了空点异常。您可以在适配器中添加一个 setter,将 Activity 的引用传递给适配器
public void setActivity(Activity aty) {
this.aty = aty;
}