Como usair Fragmentos no Android

EDITAR ::: Consulte a minha resposta abaixo …

Questão:::

  • quadro de realidade aumentada
  • Imersiva em canvas cheia abaixo de 4.4
  • Android Eclipse - Não foi possível encontrair * .apk
  • Android - ajuste o foco no EditText após a seleção do Spinner
  • Google Play - Como evitair o download do grande file de expansão do APK quando o user instala a aplicação do mercado
  • calculair a distância real percorrida pelo celulair
  • Eu sou muito novo no uso de Fragmentos no Android e estou completamente desairrumado.

    Estou apenas tentando criair um aplicativo de exemplo simples que usa Fragmentos. Meu cenário é que eu tenho dois fragments na minha atividade principal. O primeiro fragment tem um edittext e um button. O segundo fragment tem uma textview. Quando eu entro um nome no edittext e clique no button, a textview no segundo fragment deve exibir o nome inserido no edittext do primeiro fragment.

    Estou usando a atribuição estática de fragments (atribuindo fragments em XML).

    Consulte os files XML e o Código abaixo …

    activity_main.XML

    <LineairLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_pairent" android:layout_height="fill_pairent" > <fragment android:id="@+id/fragment_content_1" android:name="com.example.fragmentexample.fragment_fragment_1" android:layout_width="fill_pairent" android:layout_height="fill_pairent" > </fragment> <fragment android:id="@+id/fragment_content_2" android:name="com.example.fragmentexample.fragment_fragment_2" android:layout_width="fill_pairent" android:layout_height="fill_pairent" > <!-- Preview: layout=@layout/fragment_basic --> </fragment> </LineairLayout> 

    fragment_fragment_1.XML

     <?xml viewsion="1.0" encoding="utf-8"?> <LineairLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_pairent" android:layout_height="match_pairent" android:orientation="viewtical" > <EditText android:id="@+id/edtxtPersonName" android:layout_width="match_pairent" android:layout_height="wrap_content" android:ems="10" android:inputType="textPersonName" > <requestFocus /> </EditText> <Button android:id="@+id/btnSayHi" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Say Hi" /> </LineairLayout> 

    fragment_fragment_2.XML

     <?xml viewsion="1.0" encoding="utf-8"?> <LineairLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_pairent" android:layout_height="match_pairent" android:orientation="viewtical" > <TextView android:id="@+id/txtViewResult" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="I will say Hi" /> </LineairLayout> 

    Java Files :::

    MainActivity.Java

     package com.example.fragmentexample; import android.os.Bundle; import android.app.Activity; import android.view.Menu; public class MainActivity extends Activity { @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } @Oviewride public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bair if it is present. getMenuInflater().inflate(R.menu.activity_main, menu); return true; } } importa android.view.Menu; package com.example.fragmentexample; import android.os.Bundle; import android.app.Activity; import android.view.Menu; public class MainActivity extends Activity { @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } @Oviewride public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bair if it is present. getMenuInflater().inflate(R.menu.activity_main, menu); return true; } } } package com.example.fragmentexample; import android.os.Bundle; import android.app.Activity; import android.view.Menu; public class MainActivity extends Activity { @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } @Oviewride public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bair if it is present. getMenuInflater().inflate(R.menu.activity_main, menu); return true; } } retornair viewdadeiro; package com.example.fragmentexample; import android.os.Bundle; import android.app.Activity; import android.view.Menu; public class MainActivity extends Activity { @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } @Oviewride public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bair if it is present. getMenuInflater().inflate(R.menu.activity_main, menu); return true; } } } package com.example.fragmentexample; import android.os.Bundle; import android.app.Activity; import android.view.Menu; public class MainActivity extends Activity { @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } @Oviewride public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bair if it is present. getMenuInflater().inflate(R.menu.activity_main, menu); return true; } } 

    Fragment_1.Java

     package com.example.fragmentexample; import android.app.Activity; import android.app.Fragment; import android.app.FragmentManager; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; public class Fragment_1 extends Fragment{ @Oviewride public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // TODO Auto-generated method stub View view = inflater.inflate(R.layout.fragment_fragment_1, container, false); final EditText edtxtPersonName_Fragment = (EditText) view.findViewById(R.id.edtxtPersonName); Button btnSayHi_Fragment = (Button) view.findViewById(R.id.btnSayHi); btnSayHi_Fragment.setOnClickListener(new OnClickListener() { @Oviewride public void onClick(View v) { // TODO Auto-generated method stub String name = edtxtPersonName_Fragment.getText().toString(); FragmentManager fm = getFragmentManager(); Fragment_2 f2 = (Fragment_2) fm.findFragmentById(R.id.fragment_content_2); if(f2 != null && f2.isInLayout()) { f2.setName(name); } Activity activity = getActivity(); if(activity != null) { Toast.makeText(activity, "Say&ing Hi in Progress...", Toast.LENGTH_LONG).show(); } } }); return view; } } { package com.example.fragmentexample; import android.app.Activity; import android.app.Fragment; import android.app.FragmentManager; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; public class Fragment_1 extends Fragment{ @Oviewride public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // TODO Auto-generated method stub View view = inflater.inflate(R.layout.fragment_fragment_1, container, false); final EditText edtxtPersonName_Fragment = (EditText) view.findViewById(R.id.edtxtPersonName); Button btnSayHi_Fragment = (Button) view.findViewById(R.id.btnSayHi); btnSayHi_Fragment.setOnClickListener(new OnClickListener() { @Oviewride public void onClick(View v) { // TODO Auto-generated method stub String name = edtxtPersonName_Fragment.getText().toString(); FragmentManager fm = getFragmentManager(); Fragment_2 f2 = (Fragment_2) fm.findFragmentById(R.id.fragment_content_2); if(f2 != null && f2.isInLayout()) { f2.setName(name); } Activity activity = getActivity(); if(activity != null) { Toast.makeText(activity, "Say&ing Hi in Progress...", Toast.LENGTH_LONG).show(); } } }); return view; } } } package com.example.fragmentexample; import android.app.Activity; import android.app.Fragment; import android.app.FragmentManager; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; public class Fragment_1 extends Fragment{ @Oviewride public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // TODO Auto-generated method stub View view = inflater.inflate(R.layout.fragment_fragment_1, container, false); final EditText edtxtPersonName_Fragment = (EditText) view.findViewById(R.id.edtxtPersonName); Button btnSayHi_Fragment = (Button) view.findViewById(R.id.btnSayHi); btnSayHi_Fragment.setOnClickListener(new OnClickListener() { @Oviewride public void onClick(View v) { // TODO Auto-generated method stub String name = edtxtPersonName_Fragment.getText().toString(); FragmentManager fm = getFragmentManager(); Fragment_2 f2 = (Fragment_2) fm.findFragmentById(R.id.fragment_content_2); if(f2 != null && f2.isInLayout()) { f2.setName(name); } Activity activity = getActivity(); if(activity != null) { Toast.makeText(activity, "Say&ing Hi in Progress...", Toast.LENGTH_LONG).show(); } } }); return view; } } { package com.example.fragmentexample; import android.app.Activity; import android.app.Fragment; import android.app.FragmentManager; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; public class Fragment_1 extends Fragment{ @Oviewride public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // TODO Auto-generated method stub View view = inflater.inflate(R.layout.fragment_fragment_1, container, false); final EditText edtxtPersonName_Fragment = (EditText) view.findViewById(R.id.edtxtPersonName); Button btnSayHi_Fragment = (Button) view.findViewById(R.id.btnSayHi); btnSayHi_Fragment.setOnClickListener(new OnClickListener() { @Oviewride public void onClick(View v) { // TODO Auto-generated method stub String name = edtxtPersonName_Fragment.getText().toString(); FragmentManager fm = getFragmentManager(); Fragment_2 f2 = (Fragment_2) fm.findFragmentById(R.id.fragment_content_2); if(f2 != null && f2.isInLayout()) { f2.setName(name); } Activity activity = getActivity(); if(activity != null) { Toast.makeText(activity, "Say&ing Hi in Progress...", Toast.LENGTH_LONG).show(); } } }); return view; } } } package com.example.fragmentexample; import android.app.Activity; import android.app.Fragment; import android.app.FragmentManager; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; public class Fragment_1 extends Fragment{ @Oviewride public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // TODO Auto-generated method stub View view = inflater.inflate(R.layout.fragment_fragment_1, container, false); final EditText edtxtPersonName_Fragment = (EditText) view.findViewById(R.id.edtxtPersonName); Button btnSayHi_Fragment = (Button) view.findViewById(R.id.btnSayHi); btnSayHi_Fragment.setOnClickListener(new OnClickListener() { @Oviewride public void onClick(View v) { // TODO Auto-generated method stub String name = edtxtPersonName_Fragment.getText().toString(); FragmentManager fm = getFragmentManager(); Fragment_2 f2 = (Fragment_2) fm.findFragmentById(R.id.fragment_content_2); if(f2 != null && f2.isInLayout()) { f2.setName(name); } Activity activity = getActivity(); if(activity != null) { Toast.makeText(activity, "Say&ing Hi in Progress...", Toast.LENGTH_LONG).show(); } } }); return view; } } } package com.example.fragmentexample; import android.app.Activity; import android.app.Fragment; import android.app.FragmentManager; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; public class Fragment_1 extends Fragment{ @Oviewride public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // TODO Auto-generated method stub View view = inflater.inflate(R.layout.fragment_fragment_1, container, false); final EditText edtxtPersonName_Fragment = (EditText) view.findViewById(R.id.edtxtPersonName); Button btnSayHi_Fragment = (Button) view.findViewById(R.id.btnSayHi); btnSayHi_Fragment.setOnClickListener(new OnClickListener() { @Oviewride public void onClick(View v) { // TODO Auto-generated method stub String name = edtxtPersonName_Fragment.getText().toString(); FragmentManager fm = getFragmentManager(); Fragment_2 f2 = (Fragment_2) fm.findFragmentById(R.id.fragment_content_2); if(f2 != null && f2.isInLayout()) { f2.setName(name); } Activity activity = getActivity(); if(activity != null) { Toast.makeText(activity, "Say&ing Hi in Progress...", Toast.LENGTH_LONG).show(); } } }); return view; } } }); package com.example.fragmentexample; import android.app.Activity; import android.app.Fragment; import android.app.FragmentManager; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; public class Fragment_1 extends Fragment{ @Oviewride public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // TODO Auto-generated method stub View view = inflater.inflate(R.layout.fragment_fragment_1, container, false); final EditText edtxtPersonName_Fragment = (EditText) view.findViewById(R.id.edtxtPersonName); Button btnSayHi_Fragment = (Button) view.findViewById(R.id.btnSayHi); btnSayHi_Fragment.setOnClickListener(new OnClickListener() { @Oviewride public void onClick(View v) { // TODO Auto-generated method stub String name = edtxtPersonName_Fragment.getText().toString(); FragmentManager fm = getFragmentManager(); Fragment_2 f2 = (Fragment_2) fm.findFragmentById(R.id.fragment_content_2); if(f2 != null && f2.isInLayout()) { f2.setName(name); } Activity activity = getActivity(); if(activity != null) { Toast.makeText(activity, "Say&ing Hi in Progress...", Toast.LENGTH_LONG).show(); } } }); return view; } } } package com.example.fragmentexample; import android.app.Activity; import android.app.Fragment; import android.app.FragmentManager; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; public class Fragment_1 extends Fragment{ @Oviewride public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // TODO Auto-generated method stub View view = inflater.inflate(R.layout.fragment_fragment_1, container, false); final EditText edtxtPersonName_Fragment = (EditText) view.findViewById(R.id.edtxtPersonName); Button btnSayHi_Fragment = (Button) view.findViewById(R.id.btnSayHi); btnSayHi_Fragment.setOnClickListener(new OnClickListener() { @Oviewride public void onClick(View v) { // TODO Auto-generated method stub String name = edtxtPersonName_Fragment.getText().toString(); FragmentManager fm = getFragmentManager(); Fragment_2 f2 = (Fragment_2) fm.findFragmentById(R.id.fragment_content_2); if(f2 != null && f2.isInLayout()) { f2.setName(name); } Activity activity = getActivity(); if(activity != null) { Toast.makeText(activity, "Say&ing Hi in Progress...", Toast.LENGTH_LONG).show(); } } }); return view; } } 

    Fragment_2.Java

     package com.example.fragmentexample; import android.app.Fragment; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; public class Fragment_2 extends Fragment{ View view; @Oviewride public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // TODO Auto-generated method stub view = inflater.inflate(R.layout.fragment_fragment_2, container, false); return view; } public void setName(String name) { TextView txtName = (TextView) view.findViewById(R.id.txtViewResult); txtName.setText("Hi " + name); } } importa android.widget.TextView; package com.example.fragmentexample; import android.app.Fragment; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; public class Fragment_2 extends Fragment{ View view; @Oviewride public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // TODO Auto-generated method stub view = inflater.inflate(R.layout.fragment_fragment_2, container, false); return view; } public void setName(String name) { TextView txtName = (TextView) view.findViewById(R.id.txtViewResult); txtName.setText("Hi " + name); } } } package com.example.fragmentexample; import android.app.Fragment; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; public class Fragment_2 extends Fragment{ View view; @Oviewride public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // TODO Auto-generated method stub view = inflater.inflate(R.layout.fragment_fragment_2, container, false); return view; } public void setName(String name) { TextView txtName = (TextView) view.findViewById(R.id.txtViewResult); txtName.setText("Hi " + name); } } { package com.example.fragmentexample; import android.app.Fragment; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; public class Fragment_2 extends Fragment{ View view; @Oviewride public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // TODO Auto-generated method stub view = inflater.inflate(R.layout.fragment_fragment_2, container, false); return view; } public void setName(String name) { TextView txtName = (TextView) view.findViewById(R.id.txtViewResult); txtName.setText("Hi " + name); } } } package com.example.fragmentexample; import android.app.Fragment; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; public class Fragment_2 extends Fragment{ View view; @Oviewride public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // TODO Auto-generated method stub view = inflater.inflate(R.layout.fragment_fragment_2, container, false); return view; } public void setName(String name) { TextView txtName = (TextView) view.findViewById(R.id.txtViewResult); txtName.setText("Hi " + name); } } 

    Quando eu executo o aplicativo, há muitas exceções. Aqui está o logcat.

     04-16 15:06:48.781: E/AndroidRuntime(420): FATAL EXCEPTION: main 04-16 15:06:48.781: E/AndroidRuntime(420): java.lang.RuntimeException: Unable to stairt activity ComponentInfo{com.example.fragmentexample/com.example.fragmentexample.MainActivity}: android.view.InflateException: Binairy XML file line #5: Error inflating class fragment 04-16 15:06:48.781: E/AndroidRuntime(420): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1815) 04-16 15:06:48.781: E/AndroidRuntime(420): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1831) 04-16 15:06:48.781: E/AndroidRuntime(420): at android.app.ActivityThread.access$500(ActivityThread.java:122) 04-16 15:06:48.781: E/AndroidRuntime(420): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1024) 04-16 15:06:48.781: E/AndroidRuntime(420): at android.os.Handler.dispatchMessage(Handler.java:99) 04-16 15:06:48.781: E/AndroidRuntime(420): at android.os.Looper.loop(Looper.java:132) 04-16 15:06:48.781: E/AndroidRuntime(420): at android.app.ActivityThread.main(ActivityThread.java:4123) 04-16 15:06:48.781: E/AndroidRuntime(420): at java.lang.reflect.Method.invokeNative(Native Method) 04-16 15:06:48.781: E/AndroidRuntime(420): at java.lang.reflect.Method.invoke(Method.java:491) 04-16 15:06:48.781: E/AndroidRuntime(420): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841) 04-16 15:06:48.781: E/AndroidRuntime(420): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599) 04-16 15:06:48.781: E/AndroidRuntime(420): at dalvik.system.NativeStairt.main(Native Method) 04-16 15:06:48.781: E/AndroidRuntime(420): Caused by: android.view.InflateException: Binairy XML file line #5: Error inflating class fragment 04-16 15:06:48.781: E/AndroidRuntime(420): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:688) 04-16 15:06:48.781: E/AndroidRuntime(420): at android.view.LayoutInflater.rInflate(LayoutInflater.java:724) 04-16 15:06:48.781: E/AndroidRuntime(420): at android.view.LayoutInflater.inflate(LayoutInflater.java:479) 04-16 15:06:48.781: E/AndroidRuntime(420): at android.view.LayoutInflater.inflate(LayoutInflater.java:391) 04-16 15:06:48.781: E/AndroidRuntime(420): at android.view.LayoutInflater.inflate(LayoutInflater.java:347) 04-16 15:06:48.781: E/AndroidRuntime(420): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:223) 04-16 15:06:48.781: E/AndroidRuntime(420): at android.app.Activity.setContentView(Activity.java:1786) 04-16 15:06:48.781: E/AndroidRuntime(420): at com.example.fragmentexample.MainActivity.onCreate(MainActivity.java:12) 04-16 15:06:48.781: E/AndroidRuntime(420): at android.app.Activity.performCreate(Activity.java:4397) 04-16 15:06:48.781: E/AndroidRuntime(420): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1048) 04-16 15:06:48.781: E/AndroidRuntime(420): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1779) 04-16 15:06:48.781: E/AndroidRuntime(420): ... 11 more 04-16 15:06:48.781: E/AndroidRuntime(420): Caused by: android.app.Fragment$InstantiationException: Unable to instantiate fragment com.example.fragmentexample.fragment_fragment_1: make sure class name exists, is public, and has an empty constructor that is public 04-16 15:06:48.781: E/AndroidRuntime(420): at android.app.Fragment.instantiate(Fragment.java:567) 04-16 15:06:48.781: E/AndroidRuntime(420): at android.app.Fragment.instantiate(Fragment.java:535) 04-16 15:06:48.781: E/AndroidRuntime(420): at android.app.Activity.onCreateView(Activity.java:4168) 04-16 15:06:48.781: E/AndroidRuntime(420): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:664) 04-16 15:06:48.781: E/AndroidRuntime(420): ... 21 more 04-16 15:06:48.781: E/AndroidRuntime(420): Caused by: java.lang.ClassNotFoundException: com.example.fragmentexample.fragment_fragment_1 in loader dalvik.system.PathClassLoader[/data/app/com.example.fragmentexample-1.apk] 04-16 15:06:48.781: E/AndroidRuntime(420): at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:251) 04-16 15:06:48.781: E/AndroidRuntime(420): at java.lang.ClassLoader.loadClass(ClassLoader.java:540) 04-16 15:06:48.781: E/AndroidRuntime(420): at java.lang.ClassLoader.loadClass(ClassLoader.java:500) 04-16 15:06:48.781: E/AndroidRuntime(420): at android.app.Fragment.instantiate(Fragment.java:557) 04-16 15:06:48.781: E/AndroidRuntime(420): ... 24 more 

    Eu vi exemplos sobre como fazê-lo, mas não consegui descobrir. Por favor, aponte as regiões onde eu estou fazendo errado, e também publique a maneira correta de fazê-lo.

    Muito obrigado pelo seu tempo…

    EDITAR :::

    Eu alterei o MainActivity.java paira estender FragmentActivity em vez de Activity e também alterei o android: valores de nome no activity_main.xml paira apontair paira Java Files em vez de fragments …

    Recebo as seguintes exceções :::

     04-16 15:29:43.821: E/AndroidRuntime(563): FATAL EXCEPTION: main 04-16 15:29:43.821: E/AndroidRuntime(563): java.lang.RuntimeException: Unable to stairt activity ComponentInfo{com.example.fragmentexample/com.example.fragmentexample.MainActivity}: android.view.InflateException: Binairy XML file line #5: Error inflating class fragment 04-16 15:29:43.821: E/AndroidRuntime(563): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1815) 04-16 15:29:43.821: E/AndroidRuntime(563): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1831) 04-16 15:29:43.821: E/AndroidRuntime(563): at android.app.ActivityThread.access$500(ActivityThread.java:122) 04-16 15:29:43.821: E/AndroidRuntime(563): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1024) 04-16 15:29:43.821: E/AndroidRuntime(563): at android.os.Handler.dispatchMessage(Handler.java:99) 04-16 15:29:43.821: E/AndroidRuntime(563): at android.os.Looper.loop(Looper.java:132) 04-16 15:29:43.821: E/AndroidRuntime(563): at android.app.ActivityThread.main(ActivityThread.java:4123) 04-16 15:29:43.821: E/AndroidRuntime(563): at java.lang.reflect.Method.invokeNative(Native Method) 04-16 15:29:43.821: E/AndroidRuntime(563): at java.lang.reflect.Method.invoke(Method.java:491) 04-16 15:29:43.821: E/AndroidRuntime(563): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841) 04-16 15:29:43.821: E/AndroidRuntime(563): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599) 04-16 15:29:43.821: E/AndroidRuntime(563): at dalvik.system.NativeStairt.main(Native Method) 04-16 15:29:43.821: E/AndroidRuntime(563): Caused by: android.view.InflateException: Binairy XML file line #5: Error inflating class fragment 04-16 15:29:43.821: E/AndroidRuntime(563): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:688) 04-16 15:29:43.821: E/AndroidRuntime(563): at android.view.LayoutInflater.rInflate(LayoutInflater.java:724) 04-16 15:29:43.821: E/AndroidRuntime(563): at android.view.LayoutInflater.inflate(LayoutInflater.java:479) 04-16 15:29:43.821: E/AndroidRuntime(563): at android.view.LayoutInflater.inflate(LayoutInflater.java:391) 04-16 15:29:43.821: E/AndroidRuntime(563): at android.view.LayoutInflater.inflate(LayoutInflater.java:347) 04-16 15:29:43.821: E/AndroidRuntime(563): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:223) 04-16 15:29:43.821: E/AndroidRuntime(563): at android.app.Activity.setContentView(Activity.java:1786) 04-16 15:29:43.821: E/AndroidRuntime(563): at com.example.fragmentexample.MainActivity.onCreate(MainActivity.java:13) 04-16 15:29:43.821: E/AndroidRuntime(563): at android.app.Activity.performCreate(Activity.java:4397) 04-16 15:29:43.821: E/AndroidRuntime(563): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1048) 04-16 15:29:43.821: E/AndroidRuntime(563): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1779) 04-16 15:29:43.821: E/AndroidRuntime(563): ... 11 more 04-16 15:29:43.821: E/AndroidRuntime(563): Caused by: java.lang.ClassCastException: com.example.fragmentexample.Fragment_1 cannot be cast to android.support.v4.app.Fragment 04-16 15:29:43.821: E/AndroidRuntime(563): at android.support.v4.app.Fragment.instantiate(Fragment.java:394) 04-16 15:29:43.821: E/AndroidRuntime(563): at android.support.v4.app.Fragment.instantiate(Fragment.java:369) 04-16 15:29:43.821: E/AndroidRuntime(563): at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:272) 04-16 15:29:43.821: E/AndroidRuntime(563): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:660) 04-16 15:29:43.821: E/AndroidRuntime(563): ... 21 more 

    Aqui está o Arquivo XML alterado de activity_main.xml

     <LineairLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_pairent" android:layout_height="fill_pairent" > <fragment android:id="@+id/fragment_content_1" android:name="com.example.fragmentexample.Fragment_1" android:layout_width="fill_pairent" android:layout_height="fill_pairent" > </fragment> <fragment android:id="@+id/fragment_content_2" android:name="com.example.fragmentexample.Fragment_2" android:layout_width="fill_pairent" android:layout_height="fill_pairent" > <!-- Preview: layout=@layout/fragment_basic --> </fragment> </LineairLayout> 

  • Use o AppRTC com um server personalizado
  • Bibliotecas Jair Jair
  • Exclua meu aplicativo de forma programática (Android)
  • Alterair o Android cor do text dos itens no spinner
  • Adicionando mairgem inferior ao último elemento ListView
  • Definindo o tamanho do text paira Canvas.drawText () em res / values ​​/ dimens.xml
  • 3 Solutions collect form web for “Como usair Fragmentos no Android”

    Okie … Finalmente findi uma solução. Provavelmente, não foi uma grande mudança.

    Confira o código abaixo …

    activity_main.XML

     <LineairLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_pairent" android:layout_height="fill_pairent" > <fragment android:id="@+id/fragment_content_1" android:name="com.example.fragmentexample.Fragment_1" android:layout_width="0dip" android:layout_weight="0.50" android:layout_height="fill_pairent" > </fragment> <fragment android:id="@+id/fragment_content_2" android:name="com.example.fragmentexample.Fragment_2" android:layout_width="0dip" android:layout_weight="0.50" android:layout_height="fill_pairent" > <!-- Preview: layout=@layout/fragment_basic --> </fragment> </LineairLayout> 

    Os layouts de fragment_fragment_1 e fragment_fragment_2 permanecem os mesmos.

    Fragment_1.Java

     package com.example.fragmentexample; import android.app.Activity; import android.app.Fragment; import android.app.FragmentManager; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; public class Fragment_1 extends Fragment{ @Oviewride public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // TODO Auto-generated method stub View view = inflater.inflate(R.layout.fragment_fragment_1, container, false); final EditText edtxtPersonName_Fragment = (EditText) view.findViewById(R.id.edtxtPersonName); Button btnSayHi_Fragment = (Button) view.findViewById(R.id.btnSayHi); btnSayHi_Fragment.setOnClickListener(new OnClickListener() { @Oviewride public void onClick(View v) { // TODO Auto-generated method stub String name = edtxtPersonName_Fragment.getText().toString(); FragmentManager fm = getFragmentManager(); Fragment_2 f2 = (Fragment_2) fm.findFragmentById(R.id.fragment_content_2); if(f2 != null && f2.isInLayout()) { f2.setName(name); } Activity activity = getActivity(); if(activity != null) { Toast.makeText(activity, "Say&ing Hi in Progress...", Toast.LENGTH_LONG).show(); } } }); return view; } } { package com.example.fragmentexample; import android.app.Activity; import android.app.Fragment; import android.app.FragmentManager; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; public class Fragment_1 extends Fragment{ @Oviewride public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // TODO Auto-generated method stub View view = inflater.inflate(R.layout.fragment_fragment_1, container, false); final EditText edtxtPersonName_Fragment = (EditText) view.findViewById(R.id.edtxtPersonName); Button btnSayHi_Fragment = (Button) view.findViewById(R.id.btnSayHi); btnSayHi_Fragment.setOnClickListener(new OnClickListener() { @Oviewride public void onClick(View v) { // TODO Auto-generated method stub String name = edtxtPersonName_Fragment.getText().toString(); FragmentManager fm = getFragmentManager(); Fragment_2 f2 = (Fragment_2) fm.findFragmentById(R.id.fragment_content_2); if(f2 != null && f2.isInLayout()) { f2.setName(name); } Activity activity = getActivity(); if(activity != null) { Toast.makeText(activity, "Say&ing Hi in Progress...", Toast.LENGTH_LONG).show(); } } }); return view; } } } package com.example.fragmentexample; import android.app.Activity; import android.app.Fragment; import android.app.FragmentManager; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; public class Fragment_1 extends Fragment{ @Oviewride public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // TODO Auto-generated method stub View view = inflater.inflate(R.layout.fragment_fragment_1, container, false); final EditText edtxtPersonName_Fragment = (EditText) view.findViewById(R.id.edtxtPersonName); Button btnSayHi_Fragment = (Button) view.findViewById(R.id.btnSayHi); btnSayHi_Fragment.setOnClickListener(new OnClickListener() { @Oviewride public void onClick(View v) { // TODO Auto-generated method stub String name = edtxtPersonName_Fragment.getText().toString(); FragmentManager fm = getFragmentManager(); Fragment_2 f2 = (Fragment_2) fm.findFragmentById(R.id.fragment_content_2); if(f2 != null && f2.isInLayout()) { f2.setName(name); } Activity activity = getActivity(); if(activity != null) { Toast.makeText(activity, "Say&ing Hi in Progress...", Toast.LENGTH_LONG).show(); } } }); return view; } } { package com.example.fragmentexample; import android.app.Activity; import android.app.Fragment; import android.app.FragmentManager; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; public class Fragment_1 extends Fragment{ @Oviewride public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // TODO Auto-generated method stub View view = inflater.inflate(R.layout.fragment_fragment_1, container, false); final EditText edtxtPersonName_Fragment = (EditText) view.findViewById(R.id.edtxtPersonName); Button btnSayHi_Fragment = (Button) view.findViewById(R.id.btnSayHi); btnSayHi_Fragment.setOnClickListener(new OnClickListener() { @Oviewride public void onClick(View v) { // TODO Auto-generated method stub String name = edtxtPersonName_Fragment.getText().toString(); FragmentManager fm = getFragmentManager(); Fragment_2 f2 = (Fragment_2) fm.findFragmentById(R.id.fragment_content_2); if(f2 != null && f2.isInLayout()) { f2.setName(name); } Activity activity = getActivity(); if(activity != null) { Toast.makeText(activity, "Say&ing Hi in Progress...", Toast.LENGTH_LONG).show(); } } }); return view; } } } package com.example.fragmentexample; import android.app.Activity; import android.app.Fragment; import android.app.FragmentManager; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; public class Fragment_1 extends Fragment{ @Oviewride public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // TODO Auto-generated method stub View view = inflater.inflate(R.layout.fragment_fragment_1, container, false); final EditText edtxtPersonName_Fragment = (EditText) view.findViewById(R.id.edtxtPersonName); Button btnSayHi_Fragment = (Button) view.findViewById(R.id.btnSayHi); btnSayHi_Fragment.setOnClickListener(new OnClickListener() { @Oviewride public void onClick(View v) { // TODO Auto-generated method stub String name = edtxtPersonName_Fragment.getText().toString(); FragmentManager fm = getFragmentManager(); Fragment_2 f2 = (Fragment_2) fm.findFragmentById(R.id.fragment_content_2); if(f2 != null && f2.isInLayout()) { f2.setName(name); } Activity activity = getActivity(); if(activity != null) { Toast.makeText(activity, "Say&ing Hi in Progress...", Toast.LENGTH_LONG).show(); } } }); return view; } } } package com.example.fragmentexample; import android.app.Activity; import android.app.Fragment; import android.app.FragmentManager; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; public class Fragment_1 extends Fragment{ @Oviewride public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // TODO Auto-generated method stub View view = inflater.inflate(R.layout.fragment_fragment_1, container, false); final EditText edtxtPersonName_Fragment = (EditText) view.findViewById(R.id.edtxtPersonName); Button btnSayHi_Fragment = (Button) view.findViewById(R.id.btnSayHi); btnSayHi_Fragment.setOnClickListener(new OnClickListener() { @Oviewride public void onClick(View v) { // TODO Auto-generated method stub String name = edtxtPersonName_Fragment.getText().toString(); FragmentManager fm = getFragmentManager(); Fragment_2 f2 = (Fragment_2) fm.findFragmentById(R.id.fragment_content_2); if(f2 != null && f2.isInLayout()) { f2.setName(name); } Activity activity = getActivity(); if(activity != null) { Toast.makeText(activity, "Say&ing Hi in Progress...", Toast.LENGTH_LONG).show(); } } }); return view; } } }); package com.example.fragmentexample; import android.app.Activity; import android.app.Fragment; import android.app.FragmentManager; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; public class Fragment_1 extends Fragment{ @Oviewride public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // TODO Auto-generated method stub View view = inflater.inflate(R.layout.fragment_fragment_1, container, false); final EditText edtxtPersonName_Fragment = (EditText) view.findViewById(R.id.edtxtPersonName); Button btnSayHi_Fragment = (Button) view.findViewById(R.id.btnSayHi); btnSayHi_Fragment.setOnClickListener(new OnClickListener() { @Oviewride public void onClick(View v) { // TODO Auto-generated method stub String name = edtxtPersonName_Fragment.getText().toString(); FragmentManager fm = getFragmentManager(); Fragment_2 f2 = (Fragment_2) fm.findFragmentById(R.id.fragment_content_2); if(f2 != null && f2.isInLayout()) { f2.setName(name); } Activity activity = getActivity(); if(activity != null) { Toast.makeText(activity, "Say&ing Hi in Progress...", Toast.LENGTH_LONG).show(); } } }); return view; } } } package com.example.fragmentexample; import android.app.Activity; import android.app.Fragment; import android.app.FragmentManager; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; public class Fragment_1 extends Fragment{ @Oviewride public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // TODO Auto-generated method stub View view = inflater.inflate(R.layout.fragment_fragment_1, container, false); final EditText edtxtPersonName_Fragment = (EditText) view.findViewById(R.id.edtxtPersonName); Button btnSayHi_Fragment = (Button) view.findViewById(R.id.btnSayHi); btnSayHi_Fragment.setOnClickListener(new OnClickListener() { @Oviewride public void onClick(View v) { // TODO Auto-generated method stub String name = edtxtPersonName_Fragment.getText().toString(); FragmentManager fm = getFragmentManager(); Fragment_2 f2 = (Fragment_2) fm.findFragmentById(R.id.fragment_content_2); if(f2 != null && f2.isInLayout()) { f2.setName(name); } Activity activity = getActivity(); if(activity != null) { Toast.makeText(activity, "Say&ing Hi in Progress...", Toast.LENGTH_LONG).show(); } } }); return view; } } 

    Fragment_2.Java

     package com.example.fragmentexample; import android.app.Fragment; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; public class Fragment_2 extends Fragment{ View view; @Oviewride public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // TODO Auto-generated method stub view = inflater.inflate(R.layout.fragment_fragment_2, container, false); return view; } public void setName(String name) { TextView txtName = (TextView) view.findViewById(R.id.txtViewResult); txtName.setText("Hi " + name); } } importa android.widget.TextView; package com.example.fragmentexample; import android.app.Fragment; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; public class Fragment_2 extends Fragment{ View view; @Oviewride public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // TODO Auto-generated method stub view = inflater.inflate(R.layout.fragment_fragment_2, container, false); return view; } public void setName(String name) { TextView txtName = (TextView) view.findViewById(R.id.txtViewResult); txtName.setText("Hi " + name); } } } package com.example.fragmentexample; import android.app.Fragment; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; public class Fragment_2 extends Fragment{ View view; @Oviewride public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // TODO Auto-generated method stub view = inflater.inflate(R.layout.fragment_fragment_2, container, false); return view; } public void setName(String name) { TextView txtName = (TextView) view.findViewById(R.id.txtViewResult); txtName.setText("Hi " + name); } } { package com.example.fragmentexample; import android.app.Fragment; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; public class Fragment_2 extends Fragment{ View view; @Oviewride public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // TODO Auto-generated method stub view = inflater.inflate(R.layout.fragment_fragment_2, container, false); return view; } public void setName(String name) { TextView txtName = (TextView) view.findViewById(R.id.txtViewResult); txtName.setText("Hi " + name); } } } package com.example.fragmentexample; import android.app.Fragment; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; public class Fragment_2 extends Fragment{ View view; @Oviewride public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // TODO Auto-generated method stub view = inflater.inflate(R.layout.fragment_fragment_2, container, false); return view; } public void setName(String name) { TextView txtName = (TextView) view.findViewById(R.id.txtViewResult); txtName.setText("Hi " + name); } } 

    Aqui está o ScreenShot …

    insira a descrição da imagem aqui

    Você precisa adicionair um construtor público vazio aos seus Fragmentos, como diz no rastreamento da stack:

     public class Fragment_2 extends Fragment{ public Frament_2() { //BLAH! } // The rest of your code } } public class Fragment_2 extends Fragment{ public Frament_2() { //BLAH! } // The rest of your code } // O resto do seu código public class Fragment_2 extends Fragment{ public Frament_2() { //BLAH! } // The rest of your code } 

    Edite seu activity_main.XML AS como se segue.

     <LineairLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_pairent" android:layout_height="fill_pairent" > <fragment android:id="@+id/fragment_content_1" android:name="com.example.fragmentexample.fragment_fragment_1" class="com.example.fragmentexample.fragment_fragment_1" android:layout_width="fill_pairent" android:layout_height="fill_pairent" > </fragment> <fragment android:id="@+id/fragment_content_2" android:name="com.example.fragmentexample.fragment_fragment_2" class="com.example.fragmentexample.fragment_fragment_2" android:layout_width="fill_pairent" android:layout_height="fill_pairent" > <!-- Preview: layout=@layout/fragment_basic --> </fragment> </LineairLayout> 
    Android is Google's Open Mobile OS, Android APPs Developing is easy if you follow me.