2013-10-22 57 views
1

我有一個從我的主菜單中的子菜單,打開一個列表視圖活動的按鈕,即使沒有檢測到代碼中檢測到錯誤,它仍然會在點擊時崩潰。我已經嘗試了從創建自定義數組到使用自動生成的通用列表的幾件事。我敢肯定它是與佈局的一個問題...似乎無法讓我的列表視圖從按鈕打開

應該創建列表與按鈕(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> 

列表視圖的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> 

感謝您的耐心。

+0

要在哪個按鈕上啓動哪個活動並讓您爲activityin清單創建條目?併發布堆棧跟蹤 – Raghunandan

+0

@ user2873962發佈您的堆棧跟蹤和logcat – Ram

+0

它看起來像你從某處複製粘貼代碼 – Raghunandan

回答

0

與按鈕(abbutton)應該創建列表的活動:

要開始點擊abbutton

更改此

列表活動我beleive
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"一個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"
。我相信你在org.myscores.www package有一個名爲scoreboard的活動。 SO更改爲<activity
android:name="org.myscores.www.scoreboard"

+0

非常感謝,事實證明我有一些遺留在測試教程中的包信息,我將其更改爲myscore,然後在清單中我聲明錯誤的'screj',因爲xml和java被命名爲相同,並且在拆分包中,我將這個班級改名爲大寫字母「Screej」,現在它已經成爲黃金了。非常感謝,謝謝。 – ToeKnee

+0

@ user2873962歡迎您的到來,併爲此感到高興。 – Raghunandan

0

您的清單活動未在清單中聲明。例如,您必須在添加MainActivity時添加它。

<activity 
      android:name="org.myscores.www.ablist" 
      android:label="@string/app_name"/>