我在尝试将片段添加到主要活动时收到错误。 显示错误是无法解析方法add(int,com.mypackage.SampleFragmentActivity)
主要活动:
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothServerSocket;
import android.bluetooth.BluetoothSocket;
public class MultiPlayerActivity extends Activity {
*
*
*
public void manageServerConnection(BluetoothSocket bluetoothSocket){
setContentView(R.layout.fragment_sample);
FragmentManager fragmentManager = getFragmentManager();
SampleFragmentActivity samplefragment = new SampleFragmentActivity();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.add(R.id.fragmentHolder, samplefragment);
fragmentTransaction.commit();
}
*
*
}
SampleFragmentActivity:
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
/**
* A placeholder fragment containing a simple view.
*/
public class SampleFragmentActivityFragment extends Fragment {
public SampleFragmentActivityFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.activity_sample_fragment, container, false);
EditText ed = (EditText) view.findViewById(R.id.editText);
Button sendButton = (Button) view.findViewById(R.id.button);
sendButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
return view;
}
}
fargmentSample.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">
<LinearLayout
android:id="@+id/fragmentHolder"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"></LinearLayout>
</RelativeLayout>
activity_sample_fargment.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:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="com.mypackagename.SampleFragmentActivityFragment">
<TextView android:text="Enter Text" android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textView12" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/editText"
android:layout_below="@+id/textView12"
android:layout_alignParentStart="true"
android:layout_alignEnd="@+id/textView12" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Send"
android:id="@+id/button"
android:layout_below="@+id/editText"
android:layout_alignParentStart="true" />
</RelativeLayout>
请帮我解决错误。我曾尝试将导入更改为android.support.v4.app.Fragment;
,但仍然出现错误
答案 0 :(得分:1)
试试这个
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothServerSocket;
import android.bluetooth.BluetoothSocket;
public class MultiPlayerActivity extends Activity {
*
*
*
public void manageServerConnection(BluetoothSocket bluetoothSocket){
setContentView(R.layout.fragment_sample);
Fragment fragment = new SampleFragmentActivityFragment();
FragmentManager frgManager = getFragmentManager();
frgManager.beginTransaction().replace(R.id.fragmentHolder, fragment).commit();
}
*
*
}
答案 1 :(得分:0)
首先将您的活动更改为FragmentActivity并尝试此代码..
SampleFragmentActivity fragment=new SampleFragmentActivity();
getSupportFragmentManager().beginTransaction()
.replace(R.id.fragmentHolder, fragment).commit();