1

私はアンドロイドプログラミングの初心者です。Fragment を android で学びたいと思い、簡単なプログラムを書いたのですが、これを実行しようとすると、プログラムがエラーになりました。なんで?

MainActivity.java:

public class MainActivity extends Activity {

Button btn;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    btn=(Button)findViewById(R.id.btn1);
    btn.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            // TODO Auto-generated method stub
            Intent intent1=new     Intent(MainActivity.this,TestFragment.class);
            startActivity(intent1);
        }
    });
}
}

TestFragment.java:

public class TestFragment extends FragmentActivity{
@Override
public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.fragment_for_btn1);

}
 }

ViewFragment.java:

public class ViewFragment extends Fragment{
public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle   savedInstanceState){
    View v=inflater.inflate(R.layout.ui_for_fragment1, container, false);

    Button btnf1;
    btnf1=(Button)v.findViewById(R.id.btnf1);
    btnf1.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            // TODO Auto-generated method stub
            Toast.makeText(getActivity(), "This is first fragment!!",  Toast.LENGTH_LONG).show();   
        }
    });
    return v;

}
}

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button
    android:id="@+id/btn1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="CLICK ME FOR TEST FRAGMENT!"/>
</LinearLayout>

fragment_for_btn1.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Fragment
    android:id="@+id/fr1"
    android:name="com.example.fragmentexample.ViewFragment"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"/>
</LinearLayout> 

ui_for_fragment1.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
 xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="fill_parent"
 android:layout_height="fill_parent">
 <Button
    android:id="@+id/btnf1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="CLICK:)"></Button>
 </LinearLayout>

LogCat は次のとおりです。

01-30 09:54:39.255: W/dalvikvm(14593): threadid=1: thread exiting with uncaught exception (group=0x4138e300)
01-30 09:54:39.270: E/AndroidRuntime(14593): FATAL EXCEPTION: main
01-30 09:54:39.270: E/AndroidRuntime(14593): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.fragmentexample/com.example.fragmentexample.TestFragment}: android.view.InflateException: Binary XML file line #6: Error inflating class Fragment
01-30 09:54:39.270: E/AndroidRuntime(14593):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
01-30 09:54:39.270: E/AndroidRuntime(14593):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
4

1 に答える 1

0

XML ファイル内fragment_for_btn1.xml:

<Fragment
    android:id="@+id/fr1"
    android:name="om.example.fragmentexample.ViewFragment"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"/>

そのはず

<fragment
    android:id="@+id/fr1"
    class="com.example.fragmentexample.ViewFragment"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"/>
于 2013-01-30T06:19:11.550 に答える