[英]Can't seem to get my list view to open from a button
我的主菜单有一个子菜单,带有一个可打开列表视图活动的按钮,即使在代码中未检测到错误,单击后仍会崩溃。 从创建自定义数组到使用自动生成的通用列表,我已经尝试了一些方法。 我敢肯定这是布局问题...
带有应创建列表的按钮(abbutton)的活动:
package org.myscores.www;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.widget.ImageButton;
import android.view.View.OnClickListener;
public class sundayopen extends Activity implements OnClickListener {
@Override
protected void onCreate(Bundle myscores) {
super.onCreate(myscores);
setContentView(R.layout.sundayopen);
ImageButton hb1 = (ImageButton) findViewById(R.id.homebutton1);
ImageButton sbc = (ImageButton) findViewById(R.id.satlidopen);
ImageButton ab = (ImageButton) findViewById(R.id.abbutton);
sbc.setOnClickListener(this);
hb1.setOnClickListener(this);
ab.setOnClickListener(this);
}
public void onClick(View v){
switch (v.getId()) {
case R.id.homebutton1:
startActivity(new Intent(this, MainActivity.class));
break;
case R.id.satlidopen:
startActivity(new Intent(this,stat.class));
break;
case R.id.abbutton:
startActivity(new Intent(this, ablist.class));
}
}
}
xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
android:background="@drawable/scoreback" >
<ImageView
android:id="@+id/scorebox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="63dp"
android:contentDescription="@string/scorebox"
android:src="@drawable/scorebox" />
<ImageButton
android:id="@+id/satlid"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/pillboxm"
android:layout_alignRight="@+id/pillboxm"
android:background="@android:color/transparent"
android:contentDescription="@string/satlid"
android:src="@drawable/slid" />
<ImageButton
android:id="@+id/frilid"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/thulid"
android:layout_toLeftOf="@+id/satlid"
android:background="@android:color/transparent"
android:contentDescription="@string/frilid"
android:src="@drawable/flid" />
<ImageButton
android:id="@+id/thulid"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/pillboxm"
android:layout_toLeftOf="@+id/frilid"
android:background="@android:color/transparent"
android:contentDescription="@string/thulid"
android:src="@drawable/tlid" />
<ImageButton
android:id="@+id/homebutton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginTop="11dp"
android:background="@android:color/transparent"
android:contentDescription="@string/homebutton"
android:src="@drawable/homebut1" />
<ImageButton
android:id="@+id/wedlid"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/thulid"
android:layout_toLeftOf="@+id/thulid"
android:background="@android:color/transparent"
android:contentDescription="@string/wedlid"
android:src="@drawable/wlid" />
<ImageButton
android:id="@+id/tuelid"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/pillboxm"
android:layout_toLeftOf="@+id/wedlid"
android:background="@android:color/transparent"
android:contentDescription="@string/tuelid"
android:src="@drawable/tlid" />
<ImageButton
android:id="@+id/monlid"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/pillboxm"
android:layout_toLeftOf="@+id/tuelid"
android:background="@android:color/transparent"
android:contentDescription="@string/monlid"
android:src="@drawable/mlid" />
<ImageButton
android:id="@+id/adbutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignTop="@+id/abbutton"
android:background="@android:color/transparent"
android:contentDescription="@string/adbutton"
android:src="@drawable/adbutton" />
<ImageButton
android:id="@+id/abbutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/cobutton"
android:layout_alignParentRight="true"
android:background="@android:color/transparent"
android:contentDescription="@string/abbutton"
android:src="@drawable/antibiotic_buton" />
<ImageButton
android:id="@+id/bppressure"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/monlid"
android:layout_alignTop="@+id/adbutton"
android:background="@android:color/transparent"
android:contentDescription="@string/bpbutton"
android:src="@drawable/bpbutton" />
<ImageButton
android:id="@+id/satlidopen"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/monlid"
android:layout_toLeftOf="@+id/monlid"
android:background="@android:color/transparent"
android:contentDescription="@string/satlidopen"
android:src="@drawable/satlidopen" />
<ImageButton
android:id="@+id/cobutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/satlidopen"
android:layout_marginBottom="133dp"
android:layout_toRightOf="@+id/wedlid"
android:background="@android:color/transparent"
android:contentDescription="@string/cobutton"
android:src="@drawable/cobutton" />
</RelativeLayout>
listview xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ListView
android:id="@android:id/scre"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<TextView android:id="@android:id/empty"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Empty set"
/>
</LinearLayout>
ListView Java:
package org.myscores.www;
import android.app.ListActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
public class screj extends ListActivity {
String[] listItems = {"week", "month",
"yead", "day"};
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.pointlist);
setListAdapter(new ArrayAdapter(this,
android.R.layout.simple_list_item_1, listItems));
}
}
我的清单:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="org.myscores.www"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="15" android:targetSdkVersion="15"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" android:enabled="true">
<activity
android:name="org.myscores.www.Splashscreen"
android:label="@string/app_name"
android:screenOrientation="portrait"
android:theme="@android:style/Theme.Black.NoTitleBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="org.myscores.www.Splashscreen2"
android:label="@string/app_name"
android:screenOrientation="portrait"
android:theme="@android:style/Theme.Black.NoTitleBar">
<intent-filter>
</intent-filter>
</activity>
<activity
android:name="org.myscores.www.MainActivity"
android:label="@string/app_name"/>
<activity
android:name="org.myscores.www.difference"
android:label="@string/app_name"/>
<activity
android:name="org.iimed.www.scoreboard"
android:label="@string/app_name"/>
<activity
android:name="org.myscores.www.sundayopen"
android:label="@string/app_name"/>
<activity
android:name="org.myscores.www.pointlist"
android:label="@string/app_name"/>
</application>
</manifest>
谢谢你的耐心。
清单中未声明您的ablist活动。 例如,您必须在添加MainActivity时添加它。
<activity
android:name="org.myscores.www.ablist"
android:label="@string/app_name"/>
带有应创建列表的按钮(abbutton)的活动:
我相信你想点击abbutton
开始列表活动
改变这个
case R.id.abbutton:
startActivity(new Intent(this, ablist.class));
至
case R.id.abbutton:
startActivity(new Intent(this, screj.class));
堂妹
public class screj extends ListActivity {
// activity name is scresj not ablist
并且确保清单中有这个
<activity
android:name="org.myscores.www.screj"
android:label="@string/app_name">
</activity>
也用于统计活动
<activity
android:name="org.myscores.www.stat"
android:label="@string/app_name">
</activity>
编辑:
ListActivity
具有默认布局,该布局由位于屏幕中央的单个全屏列表组成。 但是,如果需要,可以通过使用onCreate()中的setContentView()设置自己的视图布局来自定义屏幕布局。 为此,您自己的视图必须包含ID为"@android:id/list"
(或列表,如果在代码中,则为"@android:id/list"
的ListView对象
所以你需要在xml中有以下内容
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ListView android:id="@android:id/list" // id must be @android:id/list
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<TextView android:id="@android:id/empty"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Empty set"
/>
</LinearLayout>
始终让类名以大写字母Screj
不确定,但是您有<activity
android:name="org.iimed.www.scoreboard"<activity
android:name="org.iimed.www.scoreboard"<activity
。 我相信您在
android:name="org.iimed.www.scoreboard"org.myscores.www package
有一个按名称计分scoreboard
的活动。 SO更改为<activity
android:name="org.myscores.www.scoreboard"<activity
android:name="org.myscores.www.scoreboard"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.