简体   繁体   中英

No view found for id for fragment PlaceholderFragment

I created a Main activity, which sends me to an ActiviteResultats activity.

My goal is now just to show a message in this activity (Message who come from the main).

I followed the Starting Another Activity tutorial .

The problem is, when I start the ActiviteResultats activity, I have an error:

"no view found for id 0x7f080000 ........... for fragment PlaceholderFragment"

... and the app shuts down.

I don't know yet how to use fragments, and the tutorial says I don't have to use it for this example.

Where am I going wrong?

My code :

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;
    }
}

}

I had the same problem. Just remove the part:

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

That solved it for me. Hope that helps!

A quick look over the tutorial shows that most of the Fragment code is removed in a later step. You could remove all the code related to the fragments, along with the bit about the options menu if you want to clear out all distractions. It's likely to solve your problem and isn't vital to that tutorial

What you need to do is to set up intent in your main activity, and then get it here so it goes like follows:

under Main_Activity.java

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

 Intent i = new Intent(this,activiteResultats.class); // or getApplicationContext() <= for context
 i.putExtras("TagOfMyMessage","MyMessageblablabla");
 startActivity(i);
 }

under ActivteResultats.java

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

  Intent i = getIntent();
String message = i.getStringExtras(); // or something like it 

// 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);
}

You just don't need to use fragments if you want a clear and easy tutorial i advice you to check this one : http://www.vogella.com/tutorials/AndroidIntent/article.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM