找不到片段PlaceholderFragment的id的视图

1 java android android-fragments

我创建了一个Main活动,它将我发送到一个ActiviteResultats活动.

我的目标现在只是在此活动中显示一条消息(来自主要消息的消息).

我按照Starting Another Activity教程进行了操作.

问题是,当我开始ActiviteResultats活动时,我有一个错误:

"找不到id为0x7f080000的视图...........对于片段PlaceholderFragment"

......并且应用程序关闭了.

我还不知道如何使用片段,教程说我不必在这个例子中使用它.

我哪里错了?

我的代码:

package com.example.surveyor;

import android.app.Activity;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class ActiviteResultats extends Activity {

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

    if (savedInstanceState == null) {
        getFragmentManager().beginTransaction()
                .add(R.id.container, new     PlaceholderFragment()).commit();
    }


    String message = "TODO";

    // Create the text view
    TextView textView = new TextView(this);
    textView.setTextSize(40);
    textView.setText(message);

    // Set the text view as the activity layout
    setContentView(textView);


}



@Override
public boolean onCreateOptionsMenu(Menu menu) {

    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.activite_resultats, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

/**
 * A placeholder fragment containing a simple view.
 */
public static class PlaceholderFragment extends Fragment {

    public PlaceholderFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(
                R.layout.fragment_activite_resultats, container, false);
        return rootView;
    }
}

}
Run Code Online (Sandbox Code Playgroud)

小智 5

我有同样的问题.只需删除部分:

if (savedInstanceState == null) {
getFragmentManager().beginTransaction()
                    .add(R.id.container, new PlaceholderFragment()).commit();
        }
Run Code Online (Sandbox Code Playgroud)

这解决了我.希望有所帮助!