这是我的主要活动
public class MainActivity extends AppCompatActivity {
//private static Button buttonPst;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.content_main);
findViewById(R.id.button4).setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent I = new Intent();
I.setAction("android.amila.action.PST");
startActivity(I);
}
}
);
}
}
Custom_row
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:text="@string/textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/rowTitle"
android:layout_weight="1"
android:textSize="18sp"
android:typeface="normal" />
<TextView
android:text="@string/textview1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/rowSubtitle"
android:layout_weight="1"
android:textSize="18sp" />
这是xml文件
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/content_pst"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.amila.newglossary.pst"
>
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_marginRight="10dp"
android:layout_marginEnd="10dp"
android:id="@+id/pstList" />
.java文件
public class pst extends Activity {
String[] englishName = new String[]{"bacon", "Ham", "Tuna", "Candy", "Meatball", "Potato"};
String[] sinhalaName = new String[]{"wdddbhha", "wdddbhha", "wdddbhha", "wdddbhha", "wa", "wa"};
// Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.content_pst);
ListView listView = (ListView) findViewById(R.id.pstList);
adapter ada= new adapter(this,englishName,sinhalaName);
listView.setAdapter(ada);
}
}
这是适配器类
public class adapter extends ArrayAdapter<String> {
String[] englishName;
String[] sinhalaName;
Context c;
LayoutInflater inflater;
public adapter(Context context, String[] englishName,String[] sinhalaName) {
super(context, R.layout.custom_row,englishName);
this.c=context;
this.englishName=englishName;
this.sinhalaName=sinhalaName;
}
public class ViewHolder{
TextView englishTv;
TextView sinhalaTv;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
inflater = (LayoutInflater)
c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.custom_row, null);
}
final ViewHolder holder=new ViewHolder();
holder.englishTv=(TextView)convertView.findViewById(R.id.rowTitle);
holder.sinhalaTv=(TextView)convertView.findViewById(R.id.rowSubtitle);
holder.englishTv.setText(englishName[position]);
holder.sinhalaTv.setText(sinhalaName[position]);
return super.getView(position, convertView, parent);
}
}
我已经尝试了这么多,第一个窗口(主要活动)在按下PST时没有错误地运行,没有出现列表视图的窗口。此错误出现在RUN中并关闭应用程序
E / AndroidRuntime:致命异常:主要 过程:com.example.amila.newglossary,PID:6309 java.lang.RuntimeException:无法启动活动ComponentInfo {com.example.amila.newglossary / com.example.amila.newglossary.pst}: java.lang.RuntimeException:您的内容必须具有其id为的ListView 属性是'android.R.id.list' 在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646) 在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707) 在android.app.ActivityThread.-wrap12(ActivityThread.java) 在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1460) 在android.os.Handler.dispatchMessage(Handler.java:102) 在android.os.Looper.loop(Looper.java:154) 在android.app.ActivityThread.main(ActivityThread.java:6077) at java.lang.reflect.Method.invoke(Native Method) 在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:865) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755) 引起:java.lang.RuntimeException:您的内容必须具有一个ListView,其id属性为'android.R.id.list' 在android.app.ListActivity.onContentChanged(ListActivity.java:243) 在com.android.internal.policy.PhoneWindow.setContentView(PhoneWindow.java:419) 在android.app.Activity.setContentView(Activity.java:2414) 在com.example.amila.newglossary.pst.onCreate(pst.java:42) 在android.app.Activity.performCreate(Activity.java:6664) 在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118) 在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2599) 在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707) 在android.app.ActivityThread.-wrap12(ActivityThread.java) 在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1460) 在android.os.Handler.dispatchMessage(Handler.java:102) 在android.os.Looper.loop(Looper.java:154) 在android.app.ActivityThread.main(ActivityThread.java:6077) at java.lang.reflect.Method.invoke(Native Method) 在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:865) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
答案 0 :(得分:1)
在适配器中返回您自定义的视图而不是返回超级
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = new ViewHolder();
if (convertView == null) {
inflater = (LayoutInflater)
c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.custom_row, null);
holder.englishTv=(TextView)convertView.findViewById(R.id.rowTitle);
holder.sinhalaTv=(TextView)convertView.findViewById(R.id.rowSubtitle);
convertView.setTag(holder);
}else{
holder = (ViewHolder) convertView.getTag();
}
holder.englishTv.setText(englishName[position]);
holder.sinhalaTv.setText(sinhalaName[position]);
return convertView;
}