我收到错误消息:
无法解析方法' add(int, com.example.trainer.androidlistviewelement.ListViewFragmentMainActivity.PlaceholderFragment)'
CODE:
package com.example.trainer.androidlistviewelement;
import android.app.Fragment;
import android.support.v7.app.ActionBarActivity;
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.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import static com.example.trainer.androidlistviewelement.R.id.container;
public class ListViewFragmentMainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_view_fragment_main);
TextView tv1 = (TextView) findViewById(R.id.textView1);
tv1.setText(tv1.getText());
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction().add(R.id.container, new PlaceholderFragment()).commit();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_list_view_fragment_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public static class PlaceholderFragment extends Fragment {
String[] monthsArray = {"Jan", "Feb", "Mar", "Apr"};
private ListView monthsListView;
private ArrayAdapter<String> arrayAdapter;
public PlaceholderFragment() {}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_list_view_fragment_main, container, false);
monthsListView = (ListView) rootView.findViewById(R.id.months_list);
arrayAdapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, monthsArray);
monthsListView.setAdapter(arrayAdapter);
return rootView;
}
}
}
答案 0 :(得分:3)
您的问题出在此处:import android.app.Fragment;
您无法使用本机片段支持,并使用getSupportFragmentManager
更改
import android.app.Fragment;
到
import android.support.v4.app.Fragment;