Reputation: 1833
My app use voice recognition, you type on a button it recognize the word and put it on TextView. It has an activity and I want to change it on fragment but I faced with a few problems:
My original code works good and looks like:
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button recognizeButton = (Button)findViewById(R.id.button1);
recognizeButton.setOnClickListener(this);
}
@Override
public void setContentView(View context) {
final DisplayMetrics dm = context.getResources().getDisplayMetrics();
final Configuration conf = context.getResources().getConfiguration();
conf.locale = new Locale("en");
context.getResources().updateConfiguration(conf, dm);
}
@Override
public void onClick(View v) {
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "You may speak!");
intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 1);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, "en-US");
startActivityForResult(intent, 1);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1 && resultCode == RESULT_OK) {
ArrayList<String> results;
results = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
TextView speechText = (TextView) findViewById(R.id.textView1);
String str="";
for(int i=0;i<results.size();i++){
str+= results.get(i);
}
if (str.equals("five")) {
speechText.setText(str);
}else{
speechText.setText("It's not: " + str);
}
}
}
Then I change it on Fragment and it's crashes:
public class MainActivity extends Fragment implements View.OnClickListener{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.button, container, false);
final DisplayMetrics dm = container.getResources().getDisplayMetrics();
final Configuration conf = container.getResources().getConfiguration();
conf.locale = new Locale("en");
container.getResources().updateConfiguration(conf, dm);
Button recognizeButton = (Button)view.findViewById(R.id.button1);
recognizeButton.setOnClickListener(this);
return view;
}
// @Override
// public void setContentView(View context) {
// final DisplayMetrics dm = context.getResources().getDisplayMetrics();
// final Configuration conf = context.getResources().getConfiguration();
// conf.locale = new Locale("en");
// context.getResources().updateConfiguration(conf, dm);
// }
@Override
public void onClick(View v) {
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "You may speak!");
intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 1);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, "en-US");
startActivityForResult(intent, 1);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1 && resultCode == Activity.RESULT_OK) {
ArrayList<String> results;
results = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
TextView speechText = (TextView)getView().findViewById(R.id.tvPage);
String str="";
for(int i=0;i<results.size();i++){
str+= results.get(i);
}
if (str.equals("five")) {
speechText.setText(str);
}else{
speechText.setText("It's not: " + str);
}
}
}
}
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
<fragment
android:name="htgroup.welcome.voiceapp.MainActivity"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/button_fragment"
tools:layout="@layout/button"/>
</RelativeLayout>
button.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="recognize"
android:id="@+id/button1"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:id="@+id/textView1"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:ems="10"
android:layout_marginTop="57dp" />
</LinearLayout>
Log: FATAL EXCEPTION: main
Process: htgroup.welcome.voiceapp, PID: 1699
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{htgroup.welcome.voiceapp/htgroup.welcome.voiceapp.MainActivity}: java.lang.ClassCastException: htgroup.welcome.voiceapp.MainActivity cannot be cast to android.app.Activity
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2327)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.ClassCastException: htgroup.welcome.voiceapp.MainActivity cannot be cast to android.app.Activity
at android.app.Instrumentation.newActivity(Instrumentation.java:1067)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2317)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
What do I wrong?
Upvotes: 2
Views: 2574
Reputation: 1833
I found an answer:
1.In onCreate I commented this rows (see below)
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.button, container, false);
// final DisplayMetrics dm = container.getResources().getDisplayMetrics();
// final Configuration conf = container.getResources().getConfiguration();
// conf.locale = new Locale("en");
// container.getResources().updateConfiguration(conf, dm);
Button recognizeButton = (Button) view.findViewById(R.id.button1);
recognizeButton.setOnClickListener(this);
return view;
}
I created new class Button
public class Button extends FragmentActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
In Manifest I changed .MainActivity to .Button
Upvotes: 0
Reputation: 132972
Probably :
<fragment
android:name="htgroup.welcome.voiceapp.MainActivity" //<<
....
/>
lines causing issue.
fragment android:name
attribute always contains name of Fragment with full package name instead of Activity name in which Fragment is add.
Change android:name
as to get it work:
<fragment
android:name="htgroup.welcome.voiceapp.ButtonFragment"
....
/>
Considering ButtonFragment
class is inside htgroup.welcome.voiceapp
package
Upvotes: 1