2014-07-10 164 views
0

我已經有了一些代碼。有了這段代碼,我可以製作一張照片,它會向我展示ImageView中的圖片。現在我添加了其他Button(weiter),它就像是「下一個」Button。 但現在我無法啓動此頁面。 OnClickListener()是否有錯誤?按鈕不起作用

public class Foto extends Activity{ 

ImageView iv; 
Button weiter; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.foto); 


    // ActionBar 
    setTitle("Get Picture"); 
    android.app.ActionBar bar = getActionBar(); 
    bar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#104E8B"))); 


    weiter =(Button)findViewById(R.id.weiter); 
    weiter.setOnClickListener((OnClickListener) this); 
    startActivity(new Intent(this,Login.class)); 

    iv=(ImageView) findViewById(R.id.imageView); 
    Button btn = (Button) findViewById(R.id.Foto); 
    btn.setOnClickListener(new OnClickListener(){ 



@Override 
public void onClick(View v) { 
    Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
    startActivityForResult(intent, 0); 

} 
}); 
} 
@Override 
public void onActivityResult(int requestCode, int resultCode, Intent data) { 

    if(requestCode ==0) 
    { 
     Bitmap theImage = (Bitmap) data.getExtras().get("data"); 
     iv.setImageBitmap(theImage); 
    } 
} 

}

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" > 

    <Button 
     android:id="@+id/Foto" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentTop="true" 
     android:layout_centerHorizontal="true" 
     android:layout_marginTop="49dp" 
     android:text="@string/foto" /> 

    <ImageView 
     android:id="@+id/test" 
     android:layout_width="300dp " 
     android:layout_height="300dp" 
     android:layout_alignParentLeft="true" 
     android:layout_centerVertical="true" /> 

    <ImageView 
     android:id="@+id/imageView" 
     android:layout_width="300dp" 
     android:layout_height="300dp" 
     android:layout_centerHorizontal="true" 
     android:layout_centerVertical="true" 
     android:src="@drawable/keinfoto" /> 

    <Button 
     android:id="@+id/weiter" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/test" 
     android:layout_centerHorizontal="true" 
     android:text="@string/weiter" /> 

</RelativeLayout> 

public class QRCodeScannerActivity extends Activity implements OnClickListener{ 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.addmeterscan); 

     setTitle("QR-Code scannen"); 
     android.app.ActionBar bar = getActionBar(); 
     bar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#104E8B"))); 


     Button weiter = (Button)findViewById(R.id.btnweiter); 
     weiter.setOnClickListener(this); 

} 


    public void onClick (View v) { 




     switch(v.getId()){ 


     case R.id.scan: 
     IntentIntegrator integrator = new IntentIntegrator(this); 
     integrator.initiateScan(); 
     break; 

     case R.id.btnweiter: 
      startActivity(new Intent(this,Foto.class)); 
+0

顯示您的佈局XML ? –

+0

您正在將'Foto'投射到'OnClickListener',這是無效的,在'weiter.setOnClickListener((OnClickListener)this')行中;' – Doomsknight

回答

0

您點擊監聽器編碼不正確。首先,如果你想要寫爲每個按鈕單獨onclicklisteners那麼你可以做如下簡單,

btn.setOnClickListener(new OnClickListener(){ 
    @Override 
    public void onClick(View v) { 
    //Do something here on this button click 
    } 
}); 

而如果你有一個以上的按鈕,然後就可以實現接口OnClickListener然後將實施必要的方法在你的課堂上,然後你可以使用switch case語句或if{...}else if{...}語句來做你的邏輯。請參閱下文的

首先實現接口

public class Foto extends Activity implements OnClickListener 

那麼會發生編譯器錯誤並詢問是否要採取必要的方法只是做,因爲它說,你將有一個自動生成的方法你的課如下,

public void onClick(View v) { 
    //Auto generated method 
    //in this method you can use switch case of if else statement to do as needed 
    switch (v.getId()) 
    case R.id.Foto: 
     //do something when Foto is pressed here 
     break; 
    case R.id.weiter: 
     //do something here when weiter button is pressed 
     break; 
    default: 

     break; 
} 

現在在你的onCreate方法只是更新代碼如下。

weiter =(Button)findViewById(R.id.weiter); 
weiter.setOnClickListener(this); //if you implement the interface just use this keyword nothing else 
//startActivity(new Intent(this,Login.class)); this line should not be here, it should be in the onClick method 

iv=(ImageView) findViewById(R.id.imageView); 
Button btn = (Button) findViewById(R.id.Foto); 
btn.setOnClickListener(this)  //same here just use this keyword 

也有追趕上點擊,您可以看到我的另一個答案here對於第三種方法。

+0

現在可以使用了! :) 謝謝!! – user3379235

+0

如果它對你有幫助,請做upvote .. :) – Setu

0

你定義它是的.java -

Button btn = (Button) findViewById(R.id.Foto); //This is not OK 
    btn.setOnClickListener(new OnClickListener(){ 



@Override 
public void onClick(View v) { 
    Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
    startActivityForResult(intent, 0); 

} 
}); 

注:

Foto是一個類,而不是按鈕的ID,所以你得到ClassCastException。

Casting Foto Class to OnClickListener is invalid。您應該在佈局xml中定義一個有效的屬性,並使用一些id,您可以將它用作類中按鈕的ID。

UPDATE:

由於類名和ID是相同的。因此,將按鈕的ID更改爲其他內容,並在類中進行相同的更改。它應該工作。

+0

他確實在xml中有'Foto' id的按鈕 – Setu

0
public class Foto extends Activity implements OnClickListener{ 

ImageView iv; 
Button weiter; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.foto); 

    // ActionBar 
    setTitle("Get Picture"); 
    android.app.ActionBar bar = getActionBar(); 
    bar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#104E8B"))); 


    weiter =(Button)findViewById(R.id.weiter); 
    weiter.setOnClickListener(this); 
    startActivity(new Intent(this,Login.class)); 

    iv=(ImageView) findViewById(R.id.imageView); 
    Button btn = (Button) findViewById(R.id.Foto); 
    btn.setOnClickListener(this); 

} 
@Override 
public void onActivityResult(int requestCode, int resultCode, Intent data) { 

    if(requestCode ==0) 
    { 
     Bitmap theImage = (Bitmap) data.getExtras().get("data"); 
     iv.setImageBitmap(theImage); 
    } 

@Override 
public void onClick(View v) { 
    if(v==btn){ 
    Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
    startActivityForResult(intent, 0); 
    } 
    if(v==weiter){ 
     // do some code for weiter button 
    } 

} 
} 

我只編輯了你的按鈕代碼。

0

擁有Activity工具OnClickListener並不好。

相反,你可以嘗試以下方法:

OnClickListener mPhotoClick = new OnClickListener { 
    @Override 
    public void onClick(View v) { 
     Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
     startActivityForResult(intent, 0); 
    } 
}; 

另外,您認爲分配應該是這樣

ch.swisscom.smartmeter.Foto = (ch.swisscom.smartmeter.Foto) findViewById(R.id.foto); 
0
Button weiter ; 
Button btn ; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.foto); 
    // ActionBar 
    setTitle("Get Picture"); 
    android.app.ActionBar bar = getActionBar(); 
    bar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#104E8B"))); 
    weiter =(Button)findViewById(R.id.weiter); 
    weiter.setOnClickListener(this); 

    iv=(ImageView) findViewById(R.id.imageView); 

    btn = (Button) findViewById(R.id.Foto); 
    btn.setOnClickListener(this); 
} 
@Override 
public void onClick(View v) { 
if(v.getId()==R.id.Foto) 
    Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
    startActivityForResult(intent, 0); 
} 
if(v.getId()==R.id.weiter){ 
    startActivity(new Intent(this,Login.class));//i just shifted your code here 
} 

} 
@Override 
public void onActivityResult(int requestCode, int resultCode, Intent data) { 

    if(requestCode ==0) 
    { 
     Bitmap theImage = (Bitmap) data.getExtras().get("data"); 
     iv.setImageBitmap(theImage); 
    } 
} 

不要忘記器具OnClickListener