gpt4 book ai didi

Android 错误消息 : Cannot Resolve Method Add

转载 作者:可可西里 更新时间:2023-10-31 22:04:19 26 4
gpt4 key购买 nike

我收到错误消息:

Cannot resolve method 'add(int, com.example.trainer.androidlistviewelement.ListViewFragmentMainActivity.PlaceholderFragment)'

代码:

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

最佳答案

你的问题在这里:import android.app.Fragment;

不能使用原生 fragment 支持,使用getSupportFragmentManager

改变

import android.app.Fragment;

import android.support.v4.app.Fragment;

关于Android 错误消息 : Cannot Resolve Method Add,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29843278/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com