2014-02-28 143 views
-2

我是Android開發的初學者。我正在嘗試敬酒工作,但是我找不到任何代碼錯誤。Android烤麪包不顯示

這是xml代碼。

<LinearLayout 
    android:layout_width="wrap_content" 
    android:layout_height="50dp" 
    android:layout_gravity="center" 
    android:layout_marginBottom="20dp" 
    android:layout_marginTop="20dp" 
    android:gravity="center" 
    android:orientation="horizontal" > 

    <ImageView 
     android:id="@+id/topbar" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:src="@drawable/dravaka_dark" /> 
</LinearLayout> 

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="1dp" 
    android:background="#3333" > 
</LinearLayout> 

<LinearLayout 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_gravity="left" 
    android:layout_marginTop="20dp" 
    android:gravity="left" 
    android:orientation="vertical" 
    android:paddingTop="10dp" > 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal" 
     android:padding="5dp" > 

     <ImageView 
      android:id="@+id/about_icon" 
      android:layout_width="50dp" 
      android:layout_height="50dp" 
      android:layout_margin="10dp" 
      android:layout_marginLeft="20dp" 
      android:scaleType="fitXY" 
      android:src="@drawable/about_icon" /> 

     <TextView 
      android:id="@+id/about_tab" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center_vertical" 
      android:layout_marginLeft="20dp" 
      android:clickable="true" 
      android:text="@string/about_title" 
      android:textSize="25sp" /> 
    </LinearLayout> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal" 
     android:padding="5dp" > 

     <ImageView 
      android:id="@+id/schedule_icon" 
      android:layout_width="50dp" 
      android:layout_height="50dp" 
      android:layout_margin="10dp" 
      android:scaleType="fitXY" 
      android:src="@drawable/schedule_icon" /> 

     <TextView 
      android:id="@+id/schedule_tab" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center_vertical" 
      android:layout_marginLeft="20dp" 
      android:clickable="true" 
      android:text="@string/schedule_title" 
      android:textSize="25sp" /> 
    </LinearLayout> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal" 
     android:padding="5dp" > 

     <ImageView 
      android:id="@+id/songs_icon" 
      android:layout_width="50dp" 
      android:layout_height="50dp" 
      android:layout_margin="10dp" 
      android:scaleType="fitXY" 
      android:src="@drawable/songs_icon" 
      android:textSize="25sp" /> 

     <TextView 
      android:id="@+id/songs_tab" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center_vertical" 
      android:layout_marginLeft="20dp" 
      android:clickable="true" 
      android:text="@string/songs_title" 
      android:textSize="25sp" /> 
    </LinearLayout> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal" 
     android:padding="5dp" > 

     <ImageView 
      android:id="@+id/contact_icon" 
      android:layout_width="50dp" 
      android:layout_height="50dp" 
      android:layout_margin="10dp" 
      android:scaleType="fitXY" 
      android:src="@drawable/contact_icon" /> 

     <TextView 
      android:id="@+id/contact_tab" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center_vertical" 
      android:layout_marginLeft="20dp" 
      android:clickable="true" 
      android:text="@string/contact_title" 
      android:textSize="25sp" /> 
    </LinearLayout> 
</LinearLayout> 

</LinearLayout> 

這是Java代碼

public class MainActivity extends Activity implements OnClickListener { 

    TextView aboutBtn; 
    TextView performancesBtn; 
    TextView songsBtn; 
    TextView contactBtn; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     requestWindowFeature(Window.FEATURE_NO_TITLE); 
     setContentView(R.layout.activity_main); 

     aboutBtn = (TextView) findViewById(R.id.about_tab); 
     performancesBtn = (TextView) findViewById(R.id.schedule_tab); 
     songsBtn = (TextView) findViewById(R.id.songs_tab); 
     contactBtn = (TextView) findViewById(R.id.contact_tab); 

     aboutBtn.setOnClickListener(this); 
     performancesBtn.setOnClickListener(this); 
     songsBtn.setOnClickListener(this); 
     contactBtn.setOnClickListener(this); 
    } 

    @Override 
    public void onClick(View v) { 

     switch (v.getId()) { 
     case R.id.about_icon: 
      Toast.makeText(MainActivity.this, "about clicked", 
        Toast.LENGTH_LONG).show(); 
      break; 
     case R.id.schedule_icon: 
      Toast.makeText(getApplicationContext(), "schedule clicked", 
        Toast.LENGTH_LONG).show(); 
      break; 

     case R.id.songs_icon: 
      Toast.makeText(getApplicationContext(), "songs clicked", 
        Toast.LENGTH_LONG).show(); 
      break; 

     case R.id.contact_icon: 
      Toast.makeText(getApplicationContext(), "contact clicked", 
        Toast.LENGTH_LONG).show(); 
      break; 

     } 

    } 
    } 

我只是一個初學者。請不要批評我問一個愚蠢的問題。我知道這可能是最簡單的解決方案。

在敬酒時,我試着'MainActivity.this'來檢查是否是這個問題。但即使如此,模擬器並沒有顯示吐司。

回答

5

您將OnClickListener設置爲TextView,但打開ImageViewID。 ImageView s將永遠不會收到點擊,並且您沒有代碼可以處理TextView點擊。

+0

非常感謝@laalto。 – Anubhav

1

此時應更換此

aboutBtn = (TextView)findViewById(R.id.about_tab); 
performancesBtn = (TextView)findViewById(R.id.schedule_tab); 
songsBtn = (TextView)findViewById(R.id.songs_tab); 
contactBtn = (TextView)findViewById(R.id.contact_tab); 

隨着

ImageView aboutBtn = (ImageView)findViewById(R.id.about_icon); 
ImageView performancesBtn = (ImageView)findViewById(R.id.schedule_icon); 
ImageView songsBtn = (ImageView)findViewById(R.id.songs_icon); 
contactBtn = (ImageView) findViewById(R.id.contact_icon); 

你傳遞錯誤Ids到您的Switch Case。如果您想將onclick()轉換成ImageView,則通過ImageView ID。在這裏你傳遞textView Ids,如果你想執行onClick() txtView則應下面改變你Switch Case

switch (v.getId()) { 
    case R.id.about_tab: 
    Toast.makeText(MainActivity.this, "about clicked", 
      Toast.LENGTH_LONG).show(); 
    break; 
    case R.id.schedule_tab: 
    Toast.makeText(getApplicationContext(), "schedule clicked", 
      Toast.LENGTH_LONG).show(); 
    break; 

    case R.id.songs_tab: 
    Toast.makeText(getApplicationContext(), "songs clicked", 
      Toast.LENGTH_LONG).show(); 
    break; 

    case R.id.contact_tab: 
    Toast.makeText(getApplicationContext(), "contact clicked", 
      Toast.LENGTH_LONG).show(); 
    break; 

} 
0

試試這個

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    requestWindowFeature(Window.FEATURE_NO_TITLE); 
    setContentView(R.layout.activity_main); 

    aboutBtn = (TextView) findViewById(R.id.about_tab); 
    performancesBtn = (TextView) findViewById(R.id.schedule_tab); 
    songsBtn = (TextView) findViewById(R.id.songs_tab); 
    contactBtn = (TextView) findViewById(R.id.contact_tab); 

    aboutBtn.setOnClickListener(this); 
    performancesBtn.setOnClickListener(this); 
    songsBtn.setOnClickListener(this); 
    contactBtn.setOnClickListener(this); 
} 

@Override 
public void onClick(View v) { 

    switch (v.getId()) { 
    case R.id.about_tab: 
     Toast.makeText(MainActivity.this, "about clicked", 
       Toast.LENGTH_LONG).show(); 
     break; 
    case R.id.schedule_tab: 
     Toast.makeText(getApplicationContext(), "schedule clicked", 
       Toast.LENGTH_LONG).show(); 
     break; 

    case R.id.songs_tab: 
     Toast.makeText(getApplicationContext(), "songs clicked", 
       Toast.LENGTH_LONG).show(); 
     break; 

    case R.id.contact_tab: 
     Toast.makeText(getApplicationContext(), "contact clicked", 
       Toast.LENGTH_LONG).show(); 
     break; 

    } 

} 
} 
+0

是的這是有效的! :) – Anubhav