【Android】错误Cyclic inheritance in
2018-08-03 本文已影响10人
itbird01
编译工程时,遇到“Cyclic inheritance involving”,翻译过来就是, 编译错误.png
实际代码:
package itbird.com.myapplication33;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;
import java.util.ArrayList;
/**
* MainActivity
* @author itbird
*/
public class MainActivity extends AppCompatActivity implements MainActivity.ListViewButtonClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView listView = (ListView) findViewById(R.id.listview);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(MainActivity.this, "listview item click position = " + position, Toast.LENGTH_SHORT).show();
}
});
listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(MainActivity.this, "listview item long click position = " + position, Toast.LENGTH_SHORT).show();
return true;
}
});
ArrayList<String> data = new ArrayList<String>();
data.add("test1");
data.add("test2");
data.add("test3");
ListViewAdapter adapter = new ListViewAdapter(this, data);
adapter.setOnButtonClickListener(this);
listView.setAdapter(adapter);
}
public interface ListViewButtonClickListener {
void onClick(int position);
}
@Override
public void onClick(int position) {
Toast.makeText(MainActivity.this, "listview item button long click position = " + position, Toast.LENGTH_SHORT).show();
}
}
错误原因:
循坏继承在java中是不允许的,不仅仅局限于接口,类也不能循环继承。因为编译的时候,编译器不知道该先编译哪个。比如你的这个例子,编译class MainActivity的时候需要先编译MainActivity.ListViewButtonClickListener,但是MainActivity.ListViewButtonClickListener又是class MainActivity的内部接口
解决方法:
只要把接口ListViewButtonClickListener移动到另外的文件中就不会有这样的问题了
package itbird.com.myapplication33;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;
import java.util.ArrayList;
/**
* MainActivity
* @author itbird
*/
public class MainActivity extends AppCompatActivity implements ListViewAdapter.ListViewButtonClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView listView = (ListView) findViewById(R.id.listview);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(MainActivity.this, "listview item click position = " + position, Toast.LENGTH_SHORT).show();
}
});
listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(MainActivity.this, "listview item long click position = " + position, Toast.LENGTH_SHORT).show();
return true;
}
});
ArrayList<String> data = new ArrayList<String>();
data.add("test1");
data.add("test2");
data.add("test3");
ListViewAdapter adapter = new ListViewAdapter(this, data);
adapter.setOnButtonClickListener(this);
listView.setAdapter(adapter);
}
@Override
public void onClick(int position) {
Toast.makeText(MainActivity.this, "listview item button long click position = " + position, Toast.LENGTH_SHORT).show();
}
}
package itbird.com.myapplication33;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;
import java.util.ArrayList;
/**
* ListViewAdapter
* Created by itbird on 2018/8/3.
*/
public class ListViewAdapter extends BaseAdapter {
private Context mContext;
private ArrayList<String> mList;
private LayoutInflater mInflater;
private ListViewButtonClickListener mListener;
public ListViewAdapter(Context context, ArrayList<String> stringList) {
mContext = context;
mList = stringList;
mInflater = LayoutInflater.from(mContext);
}
public void setOnButtonClickListener(ListViewButtonClickListener listener) {
mListener = listener;
}
@Override
public int getCount() {
if (mList == null) {
return 0;
}
return mList.size();
}
@Override
public Object getItem(int position) {
if (mList == null) {
return null;
}
return mList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if (convertView == null) {
// 下拉项布局
convertView = mInflater.inflate(R.layout.list_item_btn, null);
holder = new ViewHolder();
holder.title = (TextView) convertView.findViewById(R.id.title);
holder.btn = (Button) convertView.findViewById(R.id.btn);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.title.setText(mList.get(position));
// 通常将position设置为tag,方便之后判断点击的button是哪一个
holder.btn.setTag(position);
holder.btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mListener != null) {
mListener.onClick(position);
}
}
});
return convertView;
}
public interface ListViewButtonClickListener {
void onClick(int position);
}
class ViewHolder {
TextView title;
Button btn;
}
}