我在這裏教程,http://developer.android.com/training/basics/firstapp/starting-activity.html 我正在使用Eclipse,並且已經完成了教程中指定的更新。Android - 「方法add(int,Fragment)...」
首先,我得到錯誤「容器無法解析或不是字段」。然後,我將android:id =「@ + id/container」放入我的activity_display_message.xml文件中。
現在,我收到DisplayMessageActivity.java第34行的錯誤「類型FragmentTransaction中的方法add(int,Fragment)不適用於參數(int,DisplayMessageActivity.PlaceholderFragment)」。我剛剛在線上方發表了一條評論。
我不知道該怎麼處理這個錯誤。 這是我的文件。
activity_display_message.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.myfirstapp.DisplayMessageActivity"
android:id="@+id/container" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
</RelativeLayout>
DisplayMessageActivity.java
package com.example.myfirstapp;
import android.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class DisplayMessageActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_message);
// Get the message from the intent
Intent intent = getIntent();
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
// 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);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
// Error is Here
.add(R.id.container, new PlaceholderFragment()).commit();
}
}
@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);
}
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() { }
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.activity_display_message,
container, false);
return rootView;
}
}
}
請讓我知道是否需要一個答案的任何其他信息。 在此先感謝!
是否進口'android.app.Fragment'或\t'android.support.v4.app.Fragment'? – 2014-10-05 08:52:56