0

I've got a bit code. With this code I can make a picture and it will show me the picture in a ImageView. Now I added an other Button (weiter) it's like a "next" Button. But now i can't start this page. Is there a mistake with the 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>

the .java which is before

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));
Meghna
  • 539
  • 4
  • 14
user3379235
  • 29
  • 1
  • 6

5 Answers5

0

You defined -

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);

}
});

NOTE:

Foto is a Class and not the id of the button so you are getting ClassCastException.

Casting Foto Class to OnClickListener is invalid. You should have valid attribute defined in your layout xml with some id that you can use as an id for button in class.

UPDATE:

Since the Class name and the id are the same. So change the id of the button to something else and do the same change in class. It should work.

sjain
  • 23,126
  • 28
  • 107
  • 185
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
    }

}
}

I have only edited your code for button.

Laxmeena
  • 780
  • 2
  • 7
  • 28
0

You click listener is not coded correctly. firstly if you want to write separate onclicklisteners for each of your button then you can do that simply as below,

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

While if you have more than one button then you can implement the interface OnClickListener which then will implement necessary method in your class and then you can use switch case statement or if{...}else if{...} statement to do your logic. please refer below,

firstly implement the interface as

public class Foto extends Activity implements OnClickListener

then the compiler error will occur and will ask if you want to implement necessary method just do as it says and you will have an auto generated method in you class as below,

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;
}

now in your onCreate method just update the code as below.

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

there is also a third method for catching on clicks you can see my another answer here for that.

Community
  • 1
  • 1
madteapot
  • 2,208
  • 2
  • 19
  • 32
0

Having an Activity implement OnClickListener isn't great.

Instead, you can try the following:

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

Also, your view assignment should be something like

ch.swisscom.smartmeter.Foto = (ch.swisscom.smartmeter.Foto) findViewById(R.id.foto);
OrhanC1
  • 1,415
  • 2
  • 14
  • 28
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);
    }
}

Dont forget to implements OnClickListener

Meghna
  • 539
  • 4
  • 14