2014-05-03 4 views
0

Мое приложение, когда я пытаюсь запустить свою программу. Это код, который я использую.Приложение FragmentTest не работает, мое приложение продолжает сбой

MainActivity.java

import android.os.Bundle; 
import android.view.View; 
import android.app.Activity; 
import android.app.Fragment; 
import android.app.FragmentManager; 
import android.app.FragmentTransaction; 


public class MainActivity extends Activity { 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    setContentView(R.layout.activity_main); 
} 

public void selectFrag(View view) { 
    Fragment fr; 

    if(view == findViewById(R.id.button2)) { 
     fr = new FragmentTwo(); 

    }else { 
     fr = new FragmentOne(); 
    } 

    FragmentManager fm = getFragmentManager(); 
    FragmentTransaction fragmentTransaction = fm.beginTransaction(); 
    fragmentTransaction.replace(R.id.fragment_place, fr); 
    fragmentTransaction.commit(); 

} 

} 

FragmentOne.java

import android.app.Fragment; 
import android.os.Build; 
import android.os.Bundle; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 

public class FragmentOne extends Fragment { 
@Override 
public View onCreateView(LayoutInflater inflater, 
    ViewGroup container, Bundle savedInstanceState) { 

    //Inflate the layout for this fragment 

    return inflater.inflate(
      R.layout.fragment_one, container, false); 
} 
} 

FragmentTwo.java

import android.app.Fragment; 
import android.os.Build; 
import android.os.Bundle; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 

public class FragmentOne extends Fragment { 
@Override 
public View onCreateView(LayoutInflater inflater, 
    ViewGroup container, Bundle savedInstanceState) { 

    //Inflate the layout for this fragment 

    return inflater.inflate(
      R.layout.fragment_one, container, false); 
} 
} 

main_activity.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="vertical" > 
<Button 
    android:id="@+id/button1" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="Fragment No.1" 
    android:onClick="selectFrag" /> 

<Button 
    android:id="@+id/button2" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:onClick="selectFrag" 
    android:text="Fragment No.2" /> 

<fragment 
    android:name="com.javacodegeeks.android.fragmentstest.FragmentOne" 
    android:id="@+id/fragment_place" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" /> 

</LinearLayout> 

fragment_one.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    android:background="#00ffff"> 

    <TextView 
     android:id="@+id/textView1" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_weight="1" 
     android:text="This is fragment No.1" 
     android:textStyle="bold" /> 

</LinearLayout> 

fragment_two.xml также же, как и fragment_one.xml, но в TextView я печать фрагмент № 2 вместо фрагмента №1.

И в моем файле AndroidManifest.xml версия min sdk, которую я использую, - это 11. Пожалуйста, ребята, уточните меня, я ошибаюсь и исправляю меня.

Я скопировал код из this link

+2

что ошибка в LogCat? –

+1

'android: name =" com.javacodegeeks.android.fragmentstest.FragmentOne "' - это имя вашего пакета? ** com.javacodegeeks.android.fragmentstest **? Если нет, замените имя пакета в этой строке. –

+0

Спасибо @Bob Malooga за то, что помогли мне. Мое имя пакета com.example.fragmentstest – TULSIRAJ

ответ

0

Поскольку ваше имя пакета package name is com.example.fragmentstest, измените эту строку:

android:name="com.javacodegeeks.android.fragmentstest.FragmentOne" 

к

android:name="com.example.fragmentstest.FragmentOne" 

в main_activity.xml

+0

Yup Я уже сделал это, и он работает нормально. Спасибо, чувак .. :) – TULSIRAJ

+0

Добро пожаловать, мужик. ;) –

Смежные вопросы