2015-10-08 44 views
0

第一篇文章。我正在關注Android編程方面的一些相當新的課程,但不是一個完整的新手。我有一個編程的背景很久以前..Android OnClick每個新項目都會失敗

由於一些奇怪的原因,較舊的項目有OnClick運行良好,但我今天創建的任何項目,無論是Genymotion還是AVD OnClick都不會在以下準系統示例中觸發創建。 https://www.dropbox.com/s/js1qh263vldte0z/deleteme.zip?dl=0

這裏是我正在工作的原始項目,我甚至不能點擊按鈕(就好像前面有透明的東西)。

如果有人可以解釋爲什麼當我打開基於22的一些舊項目時,一切正常,但現在運行在23(以16爲後退)OnClick不起作用,這對你來說會很好。謝謝!

的佈局:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
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:orientation="vertical" 
android:paddingLeft="@dimen/activity_horizontal_margin" 
android:paddingRight="@dimen/activity_horizontal_margin" 
android:paddingTop="@dimen/activity_vertical_margin" 
android:paddingBottom="@dimen/activity_vertical_margin" 
tools:context="ca.shaarable.planetxerox.admin_add_products_hardware"> 

<LinearLayout 
    android:orientation="vertical" 
    android:id="@+id/admin_add_printer_first" 
    android:layout_width="match_parent" 
    android:layout_height="0dp" 
    android:layout_weight="0.2"> 

    <TextView 
     android:id="@+id/admin_add_printer_tv_name" 
     android:text="@string/admin_add_printer_tv_name" 
     android:textSize="12sp" 
     android:layout_marginStart="10dp" 
     android:layout_marginLeft="10dp" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content"/> 


    <EditText 
     android:id="@+id/admin_add_printer_et_name" 
     android:text="@string/admin_add_printer_et_name" 
     android:layout_marginStart="10dp" 
     android:layout_marginLeft="10dp" 
     android:layout_marginRight="10dp" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content"/> 


</LinearLayout> 

<LinearLayout 
    android:orientation="vertical" 
    android:id="@+id/admin_add_printer_second" 
    android:layout_width="match_parent" 
    android:layout_height="0dp" 
    android:layout_weight="0.65"> 

    <TextView 
     android:id="@+id/admin_add_printer_tv_thumbnail" 
     android:text="@string/admin_add_printer_tv_thumbnail" 
     android:textStyle="italic" 
     android:layout_marginBottom="5dp" 
     android:gravity="center_horizontal" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content"/> 
    <ImageView 
     android:id="@+id/admin_add_printer_image" 
     android:src="@drawable/igen5" 
     android:layout_gravity="center_horizontal" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"/> 

</LinearLayout> 

<LinearLayout 
    android:orientation="vertical" 
    android:id="@+id/admin_add_printer_third" 
    android:layout_width="match_parent" 
    android:layout_height="0dp" 
    android:layout_weight="0.15"> 

    <LinearLayout 
     android:id="@+id/admin_add_printer_sublayout" 
     android:orientation="horizontal" 
     android:layout_marginTop="20dp" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content"> 

     <LinearLayout 
      android:layout_weight="1" 
      android:layout_marginRight="10dp" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content"> 

      <Button 
       android:id="@+id/admin_add_printer_CANCEL" 
       android:text="@string/admin_add_printer_CANCEL" 
       android:background="#F99F1C" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content"/> 

     </LinearLayout> 

     <LinearLayout 
      android:layout_weight="1" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content"> 

      <Button 
       android:id="@+id/admin_add_printer_ADD" 
       android:text="@string/admin_add_printer_ADD" 
       android:background="#2ABDBA" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content"/> 


     </LinearLayout> 

    </LinearLayout> 

</LinearLayout> 

下面是該類的代碼:

package ca.shaarableapps.presssupport; 

import android.os.Bundle; 
import android.support.v7.app.AppCompatActivity; 
import android.util.Log; 
import android.view.View; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.ImageView; 

public class AdminAddPrinter extends AppCompatActivity implements View.OnClickListener 
{ 
private EditText printerName; 
private ImageView printerThumbnail; 
private Button cancelBtn, addBtn; 
private static final String TAG = "MyActivity"; 
@Override 
protected void onCreate(Bundle savedInstanceState) 
{ 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_admin_add_printer); 

    printerName = (EditText)findViewById(R.id.admin_add_printer_et_name); 
    printerThumbnail = (ImageView)findViewById(R.id.admin_add_printer_image); 
    cancelBtn = (Button)findViewById(R.id.admin_add_printer_CANCEL); 
    addBtn = (Button)findViewById(R.id.admin_add_printer_ADD); 

} 

@Override 
public void onClick(View v) 
{ 
    Log.v(TAG, "Nooooooooooooo"); 
    switch (v.getId()) 
    { 

     case R.id.admin_add_printer_ADD: 
      addBtn.setText("Good Job"); 
      break; 

    } 
} 

}

This is the layout as seen by the user...

+1

你缺少每個'Button'的'setOnClickListener(this)'函數 –

回答

-1

的問題是在這裏,你的代碼。

   <Button 
       android:id="@+id/admin_add_printer_ADD" 
       android:text="@string/admin_add_printer_ADD" 
       android:background="#2ABDBA" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:onClick="onClick" // add this line 
       /> 
+0

我希望我能記住所有的東西......謝謝基舒,就是這樣!非常感激。 – Shaare

+0

請upvote它也 – KishuDroid

+1

@KishuDroid當然,這種解決方案將工作,但它不是好主意。它會讓你的代碼在將來更難閱讀和修改 –

1

嘗試在onCreate(...)方法中添加.setOnClickListener(this);

protected void onCreate(Bundle savedInstanceState){ 
    ... 
    addBtn.setOnClickListener(this); 
} 

希望這有助於

+0

好的,感謝您的幫助。這工作。 :)但是我仍然不知道爲什麼我不能使用推薦的方式(實現View.OnClickListener)。 – Shaare

+0

我不知道你說什麼'爲什麼我不能使用推薦的方式(實現View.OnClickListener)''。你能解釋更多 –

+0

嗨潘,謝謝你的幫助。我沒有告訴xml佈局onClick =「onClick」。 – Shaare