-2
Hier ist eine Aktivität, die ich in ein Fragment konvertieren wollte, also könnte ich es in einem Sliding-Tab-Layout als Fragment verwenden.Wie man eine Aktivität in ein Fragment umwandelt, das im Registerkartenlayout verwendet wird
package com.example.hpuser.speechtotext;
public class MainActivity extends AppCompatActivity {
TextView resultTEXT;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
});
resultTEXT= (TextView)findViewById(R.id.TVresult);
}
public void onButtonClick(View v) {
if (v.getId() == R.id.imageButton) {
promptSpeechInput();
}
}
public void promptSpeechInput() {
Intent i = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
i.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
i.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault());
i.putExtra(RecognizerIntent.EXTRA_PROMPT, "Say something!");
try {
startActivityForResult(i, 100);
}
catch(ActivityNotFoundException a)
{
Toast.makeText(MainActivity.this, "Sorry! your device does not support
speech Language!", Toast.LENGTH_LONG).show();
}
}
public void onActivityResult(int request_code, int result_code, Intent i) {
super.onActivityResult(request_code, result_code, i);
switch(request_code)
{
case 100: if(result_code == RESULT_OK && i != null)
{
ArrayList<String> result =
i.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
resultTEXT.setText(result.get(0));
}
break;
}
}
Hier ist das eigentliche Fragment der Aktivität, die ich codieren. Die App stürzt aber ab, jede Hilfe?
package com.example.vez.design3;
public class HomeFragment extends Fragment {
TextView resultTEXT;
private LinearLayout llLayout;
private FragmentActivity faActivity;
public HomeFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
faActivity = (FragmentActivity) super.getActivity();
llLayout = (LinearLayout) inflater.inflate(R.layout.fragment_home,
container, false);
resultTEXT = (TextView) llLayout.findViewById(R.id.TVresult);
return llLayout;
}
public void promptSpeechInput() {
Intent i = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
i.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
i.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault());
i.putExtra(RecognizerIntent.EXTRA_PROMPT, "Say something!");
try {
startActivityForResult(i, 100);
}
catch(ActivityNotFoundException a)
{
Toast.makeText(super.getActivity(), "Sorry! your device does not
support speech Language!", Toast.LENGTH_LONG).show();
}
}
public void onActivityResult(int request_code, int result_code, Intent i) {
super.onActivityResult(request_code, result_code, i);
switch(request_code)
{
case 100: if(result_code == 1 && i != null)
{
ArrayList<String> result =
i.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
resultTEXT.setText(result.get(0));
}
break;
}
}
}
was in Crash-Logs ist ?? –
@VivekMishra, es sagt nur, dass die App nicht mehr reagiert, aber was ich herausfand ist, dass ich auskommentiere, wenn ich auskommentiereTEXT = (TextView) llLayout.findViewById (R.id.TVresult); es lädt gut, aber ich brauche diese Widgets zu arbeiten aber – JohnnyCage
es funktioniert auch gut, wenn ich dies in OncreateView habe, aber wie lade ich ID der Widgets mit view.findViewById()? 'code' @Override Öffentlichkeit onCreateView (LayoutInflater inflater, Viewgroup Container, Bundle savedInstanceState) {// aufpumpen das Layout für dieses Fragment return inflater.inflate (R.layout.fragment_home, Container, false); } 'Code' – JohnnyCage