2012-05-18 64 views
2

我看了這個主題的其他線程,並沒有一個幫助。我正在使用適當的minSdkVersion,我的菜單xml出現安裝正確,我的Manifest調用了Them.Holo.Light,以及所有其他「通常被忽略的」問題(我知道來自其他線程,無論如何)。Android 4/ICS上下文菜單沒有顯示

我使用的Eclipse與最新的SDK,並遵循Android開發wiki的基本教程。 Eclipse沒有顯示任何錯誤。如你所見,我引用了main.xml和main_activity.xml。 main.xml顯示應用程序加載時的默認視圖,這是一個簡單的標題欄和兩張小馬的圖片。這個想法是,用戶可以使用上下文菜單在小馬圖片和Lionel Richie之一(嘿,它是一個測試應用程序)之間進行切換。

我試着在模擬器上運行它,以及我的Xoom平板電腦和手機,但沒有運氣。

代碼:

/***** Contents of testIceCreamS Manifest *****/ 

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="test.icecreams" 
    android:versionCode="1" 
    android:versionName="1.0" > 

<uses-sdk android:minSdkVersion="4" 
    android:targetSdkVersion="11" /> 

    <application 
     android:icon="@drawable/ic_launcher" 
     android:label="@string/app_name" > 
     <activity 
      android:name=".TestIceCreamSActivity" 
      android:theme="@android:style/Theme.Holo.Light" 
      android:label="@string/app_name" 
      android:uiOptions="splitActionBarWhenNarrow" > 

      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
    </application> 

</manifest> 


/***** Contents of TestIceCreamSActivity.java *****/ 

package test.icecreams; 

import android.app.Activity; 
import android.os.Bundle; 
import test.icecreams.R; 

public class TestIceCreamSActivity extends Activity { 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
    } 
} 

/***** Contents of ActionBarUsage.java *****/ 

import test.icecreams.R; 
import android.app.Activity; 
import android.os.Bundle; 
import android.view.Menu; 
import android.view.MenuInflater; 
import android.view.MenuItem; 
import android.widget.ImageView; 
import android.widget.LinearLayout; 
public class ActionBarUsage extends Activity { 

/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    MenuInflater inflater = getMenuInflater(); 
    inflater.inflate(R.menu.main_activity, menu); 
    return true; 
} 

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    LinearLayout bkgr = (LinearLayout)findViewById(R.id.LinearLayout1); 
    ImageView image = (ImageView)findViewById(R.id.imageView1); 

    switch (item.getItemId()) { 
    case R.id.buttonone: 
      image.setImageResource(R.drawable.ponies); 
      return true; 
    case R.id.buttontwo: 
      image.setImageResource(R.drawable.hello); 
      return true; 
    case R.id.buttonthree: 
      bkgr.setBackgroundResource(R.color.backgroundwhite); 
      return true; 
    case R.id.buttonfour: 
      bkgr.setBackgroundResource(R.color.background); 
      return true; 
    case R.id.buttonfive: 
      //The alert code goes here! 
      return true; 
    default : 
     return super.onOptionsItemSelected(item); 
    } 
} 
} 

/***** Contents of main_activity.xml: *****/ 

<?xml version="1.0" encoding="utf-8"?> 
<menu xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:id="@+id/buttonone" 
    android:title="@string/showimage1" 
    android:icon="@drawable/helloicon" 
    android:showAsAction="ifRoom|withText"/> 
    <item android:id="@+id/buttontwo" 
      android:title="@string/showimage2" 
      android:icon="@drawable/poniesicon" 
      android:showAsAction="ifRoom|withText"/> 
    <item android:id="@+id/buttonthree" 
      android:title="@string/showwhite" 
      android:showAsAction="ifRoom|withText"/> 
    <item android:id="@+id/buttonfour" 
      android:title="@string/showblack" 
      android:showAsAction="ifRoom|withText"/> 
    <item android:id="@+id/buttonfive" 
      android:title="@string/showalert" 
      android:showAsAction="ifRoom|withText"/> 
</menu> 


/***** Contents of main.xml: *****/ 
<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/LinearLayout1" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:background="#00000000" 
    android:orientation="vertical" > 

<TextView 
    android:id="@+id/hellotext" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:debuggable="true" 
    android:text="@string/hello" /> 

<ImageView 
    android:id="@+id/imageView1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:contentDescription="@string/cd" 
    android:src="@drawable/ponies" /> 

</LinearLayout> 

我也有strings.xml中的內容,但我想這是可能是最重要的這些的。

有什麼建議嗎?

+1

+1對於小馬和Lionel Ritchie。 –

回答

0

事實上,我知道你想創建一個在ICS上運行的應用程序,其佈局爲main.xml和一個上下文菜單,如* main_activity.xml *。您不必創建兩個活動,只有在應用程序中啓動的主要活動纔會開始。

請儘量讓所有在一個活動在同一個文件TestIceCreamSActivity.java:

package test.icecreams; 

import test.icecreams.R; 
import android.app.Activity; 
import android.os.Bundle; 
import android.view.Menu; 
import android.view.MenuInflater; 
import android.view.MenuItem; 
import android.widget.ImageView; 
import android.widget.LinearLayout; 

public class TestIceCreamSActivity extends Activity { 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     MenuInflater inflater = getMenuInflater(); 
     inflater.inflate(R.menu.main_activity, menu); 
     return true; 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     LinearLayout bkgr = (LinearLayout)findViewById(R.id.LinearLayout1); 
     ImageView image = (ImageView)findViewById(R.id.imageView1); 

     switch (item.getItemId()) { 
     case R.id.buttonone: 
       image.setImageResource(R.drawable.ponies); 
       return true; 
     case R.id.buttontwo: 
       image.setImageResource(R.drawable.hello); 
       return true; 
     case R.id.buttonthree: 
       bkgr.setBackgroundResource(R.color.backgroundwhite); 
       return true; 
     case R.id.buttonfour: 
       bkgr.setBackgroundResource(R.color.background); 
       return true; 
     case R.id.buttonfive: 
       //The alert code goes here! 
       return true; 
     default : 
      return super.onOptionsItemSelected(item); 
     } 
    } 
} 

也請改變你的目標SDK版本14級(ICS 4.0 - > 4.0.2)或15(4.0 .3)在您的清單中:

<uses-sdk android:minSdkVersion="4" 
      android:targetSdkVersion="15" />