-3

我是android工作环境的新手,正在学习一些关于片段的教程,当我构建和运行项目时似乎没有错误,但是一旦在模拟器上安装了应用程序,我就会在对话框中收到这条消息,说应用程序已停止在职的

我花了一整天的时间寻找解决方案,现在任何帮助将不胜感激,下面是源代码:MainActivity.java:

package com.example.fragmentsanotherexample;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.Menu;

public class MainActivity extends FragmentActivity {

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

    FragmentManager fm=getSupportFragmentManager();
    android.support.v4.app.Fragment fragment=fm.findFragmentById(R.id.fragment_content);

    if(fragment==null){

        FragmentTransaction ft=fm.beginTransaction();
        ft.add(R.id.fragment_content, new BasicFragment());
        ft.commit();

    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

 }

基本片段.java:

package com.example.fragmentsanotherexample;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;



public class BasicFragment extends Fragment{

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){

    View view=inflater.inflate(R.layout.basic_fragment,container,false);

    Button button=(Button) view.findViewById(R.id.fragment_button);

    button.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
        Activity activity=getActivity();

            if(activity != null){

                Toast.makeText(activity, R.string.hello_world, Toast.LENGTH_LONG).show();
            }

        }
    });
    return view;
}


}

这是 basic_fragment.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"
android:orientation="vertical" >

<Button android:id="@+id/fragment_button"
    android:text="@string/btn_fragment"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" />



</LinearLayout>

这是activiy_main.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<FragmentLayout
    android:id="@+id/fragment_content"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" />




</LinearLayout>

这是清单文件:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.fragmentsanotherexample"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="17" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.example.fragmentsanotherexample.MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

</manifest>

就 logcat 而言,它是空的,任何形式的帮助都将不胜感激

4

1 回答 1

1
   <FragmentLayout
    android:id="@+id/fragment_content"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" />

AFAIKFragmentLayout不存在。你想要一个FrameLayout吗?

于 2013-07-04T08:48:59.477 回答