java - 没有虚方法 getLayoutInflater

标签 java android android-studio

我正在尝试编写一个应用程序,从在线 MySQL 数据库下载一些事件的数据并将其写入我的 Android 应用程序。现在,我想做的只是测试下载事件名称是否有效,但我不断收到此错误:

java.lang.NoSuchMethodError: No virtual method getLayoutInflater()Landroid/view/LayoutInflater; in class Lcom/suspicio/appfisio_business/FrammentoCorsi; or its super classes (declaration of 'com.suspicio.appfisio_business.FrammentoCorsi' appears in /data/app/com.suspicio.appfisio_business-1/split_lib_slice_8_apk.apk)
                  at com.suspicio.appfisio_business.FrammentoCorsi$EventoAdapter.getView(FrammentoCorsi.java:204)

指向线:

listViewItem = getLayoutInflater().inflate(R.layout.frammento_corsi, null, true);

这是我的代码(它是一个 fragment ):

FrammentoCorsi.java:

public class FrammentoCorsi extends Fragment {

public static final int CODE_GET_REQUEST = 1024;
public static final int CODE_POST_REQUEST = 1025;

public FrammentoCorsi() { //Vuoto
}

boolean isUpdating = false;
View rootView;
List<Evento> eventoList;
ListView listView;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.frammento_corsi, container, false);
        listView = (ListView) rootView.findViewById(R.id.listViewEventi);
        return rootView;
}

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {

    eventoList = new ArrayList<>();
    readEventi();

}

private void readEventi() {
    PerformNetworkRequest request = new PerformNetworkRequest(Api.URL_READ_EVENTI, null, CODE_GET_REQUEST);
    request.execute();
}

private void refreshEventoList(JSONArray eventi) throws JSONException {
    //clearing previous heroes
    eventoList.clear();

    //traversing through all the items in the json array
    //the json we got from the response
    for (int i = 0; i < eventi.length(); i++) {
        //getting each hero object
        JSONObject obj = eventi.getJSONObject(i);

        //adding the hero to the list
        eventoList.add(new Evento(
                obj.getInt("id"),
                obj.getString("titolo"),
                obj.getString("inizio"),
                obj.getString("fine"),
                obj.getInt("categoria"),
                obj.getString("link"),
                obj.getString("luogo")
        ));
    }

    //creating the adapter and setting it to the listview
    EventoAdapter adapter = new EventoAdapter(eventoList);
    listView.setAdapter(adapter);
}

//inner class to perform network request extending an AsyncTask
public class PerformNetworkRequest extends AsyncTask<Void, Void, String> {

    //the url where we need to send the request
    String url;

    //the parameters
    HashMap<String, String> params;

    //the request code to define whether it is a GET or POST
    int requestCode;

    ProgressBar barra = (ProgressBar) getView().findViewById(R.id.progressBar);

    //constructor to initialize values
    PerformNetworkRequest(String url, HashMap<String, String> params, int requestCode) {
        this.url = url;
        this.params = params;
        this.requestCode = requestCode;
    }

    //when the task started displaying a progressbar
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        barra.setVisibility(View.VISIBLE);
    }

    //this method will give the response from the request
    @Override
    protected void onPostExecute(String s) {
        super.onPostExecute(s);
        barra.setVisibility(View.INVISIBLE);
        try {
            JSONObject object = new JSONObject(s);
            if (!object.getBoolean("error")) {
                Toast.makeText(getActivity().getApplicationContext(), object.getString("message"), Toast.LENGTH_SHORT).show();
                refreshEventoList(object.getJSONArray("eventi"));
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    //the network operation will be performed in background
    @Override
    protected String doInBackground(Void... voids) {
        RequestHandler requestHandler = new RequestHandler();

        if (requestCode == CODE_POST_REQUEST)
            return requestHandler.sendPostRequest(url, params);


        if (requestCode == CODE_GET_REQUEST)
            return requestHandler.sendGetRequest(url);

        return null;
    }
}

class EventoAdapter extends ArrayAdapter<Evento> {

    List<Evento> eventoList;

        //constructor to get the list
    public EventoAdapter(List<Evento> eventoList) {
        super(getActivity(), R.layout.frammento_corsi, eventoList);
        this.eventoList = eventoList;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        View listViewItem = convertView;
        if (listViewItem == null) {
            listViewItem = getLayoutInflater().inflate(R.layout.frammento_corsi, null, true);
        }

        //getting the textview for displaying name
        TextView textViewName = listViewItem.findViewById(R.id.nomeeventocalendario);

        //the update and delete textview
        //ImageView textViewUpdate = listViewItem.findViewById(R.id.notifica);
        //ImageView textViewDelete = listViewItem.findViewById(R.id.link);

        final Evento evento = eventoList.get(position);

        textViewName.setText(evento.getTitolo());

        return listViewItem;


    }

}

}

及其资源文件frammento_corsi.xml:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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"
         xmlns:app="http://schemas.android.com/apk/res-auto"
         tools:context="com.suspicio.appfisio_business.FrammentoCorsi">

<ListView
    android:id="@+id/listViewEventi"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"/>

<TextView
    android:id="@+id/nomeeventocalendario"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"/>

<ProgressBar
    android:id="@+id/progressBar"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:visibility="gone"/>

</FrameLayout>

你能帮我一下吗?

提前致谢!

最佳答案

使用这个

 LayoutInflater layoutInflater = LayoutInflater.from(getContext());
 listViewItem = layoutInflater.inflate(R.layout.frammento_corsi, null ,true);

而不是这个

 listViewItem = getLayoutInflater().inflate(R.layout.frammento_corsi, null, true);

关于java - 没有虚方法 getLayoutInflater,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49417762/

相关文章:

java - 从 tomcat 7 升级到 tomcat8.5.41 部署失败

java - FirebaseAuth.getInstance().signOut() 不注销

android - 无法构建发布 Apk Android Studio 3.0.1 Proguard-Rules

java - 在 googleMap (android) 中更新位置

java - Stomp 的 MessageMap 格式是不是完全不能用了?

java - 将自动重试添加到 Android 下载管理器

java - 是否可以将字符串中的文本颜色更改为 Java 中的多种颜色?

android - java.awt.AWTError : Toolkit not found: apple. awt.CToolkit Android appengineUpdate

android-studio - 如何禁用 Android Studio 启动画面?

java - 如何通过蓝牙查询远程手机是否支持PBAP?