2014-03-24 23 views
1

我創建了一個主要活動,該活動將我發送給ActiviteResultats活動。未找到針對片段ID的視圖PlaceholderFragment

我的目標是現在只是在這個活動中顯示一條消息(來自主體的消息)。我跟着Starting Another Activity tutorial

的問題是,當我開始ActiviteResultats活動,我有一個錯誤:

"no view found for id 0x7f080000 ........... for fragment 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; 
    } 
} 

} 

回答

0

就讓我們來看看在本教程顯示,大部分碎片代碼在後面的步驟中去除。如果您想清除所有干擾,您可以刪除與片段相關的所有代碼以及有關選項菜單的位。它可能解決您的問題,是不是該教程

0

你需要做的是建立意圖在主要活動,並在這裏獲得重要如此這般像如下:

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

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

你只是不需要使用片段,如果你想清楚和簡單的教程我建議你檢查這一個:​​

6

我有同樣的問題。只要刪除部分:

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

這解決了我。希望有所幫助!

+0

也適合我! – Gero

+0

是的,但佔位符片段的onCreateView未被調用,因此未顯示 – surfer190

相關問題