0

everyone. i am making an app like craiglist. it takes the photo of the item and puts it into image view. and i have 2 radio buttons. and everytime i click on radio buttons the app crashes. the camera part is working btw. ok i have couple of questions about my app that i cant figure out how to do.. 1. how do i prevent it from crashes which occurs while selecting radio button. 2. how to i pass the user selected radio buttoon value into another screen's textview. 3. how do i print the image that i captured in both screens. like have the same image pass into another activity. 4. also the price for the item. how do i make it to display into another screen.?

below are my code. feel free to edit and making corrections. thank you!

this is the manifest file

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.shiva_000.myproject" >

    <uses-feature android:name="android.hardware.camera2"></uses-feature>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme" >
        <activity android:name=".MainActivity" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

</manifest>

and this is the main activity.java

package com.example.shiva_000.myproject;

import android.app.Activity;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.media.Image;
import android.net.Uri;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import java.io.File;
import java.net.URI;

public class MainActivity extends Activity {
    public static final String Tag_Price ="tag";
    public static final String TAG_Radio ="Button";

    EditText et;

    Button button;
    ImageView imageView;
   // Radio button
    Button button2;
    RadioGroup radio_g;
    RadioButton rb1, rb2;



    static final int cam_request = 1;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        button =(Button) findViewById(R.id.button1);
        imageView =(ImageView) findViewById(R.id.image_view);
        button2 = (Button) findViewById(R.id.submit);
        et =(EditText) findViewById(R.id.editText);
        radio_g=(RadioGroup) findViewById(R.id.rg1);
        rb1 = (RadioButton) findViewById(R.id.newoption);
        rb2 = (RadioButton) findViewById(R.id.usedoption);

        // on click for submit
        button2.setOnClickListener(new  View.OnClickListener(){
            @Override
            public void onClick(View v) {
                Intent i = new Intent(MainActivity.this,secondScreen.class);
                startActivity(i);
                i.putExtra(Tag_Price, et.getText().toString());
                i.putExtra(TAG_Radio,radio_g.isSelected());


            }
        });


        // on click for capture image
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent camera_Intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                File file = getFile();
                camera_Intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));
                startActivityForResult(camera_Intent, cam_request);

            }

        });


//launchSecondActivity(Double.parseDouble(et.getText().toString()),null);

    }
    private void launchSecondActivity(double price ,Image image){



    }

    private File getFile()
    {
        File folder = new File("sdcard/camera_app");

        if (!folder.exists())
        {
            folder.mkdir();
        }

        File image_file = new File(folder,"cam_image.jpg");
        return image_file;


       // return null;

    }


   // public  void

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        String path = "sdcard/camera_app/cam_image.jpg";
        imageView.setImageDrawable(Drawable.createFromPath(path));
    }

    //private void launchSecondActivity(condion ,  ){

    //}
}

this is the mainactivity xml

<?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: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=".MainActivity"
    android:orientation="vertical"
    android:weightSum="1">


    <TextView
        android:layout_width="310dp"
        android:layout_height="wrap_content"
        android:text="Welcome to SellJunky"
        android:id="@+id/textView"
        android:layout_gravity="center_horizontal"
        android:layout_weight="0.03"
        android:textColor="#a91b1b"
        android:textIsSelectable="false"
        android:textSize="30dp" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Capture Image"
        android:id="@+id/button1"
        android:layout_weight="0.03"
        android:layout_gravity="center_horizontal" />
<ImageView
    android:layout_width="350dp"
    android:layout_height="200dp"
    android:id="@+id/image_view"
    android:layout_gravity="center_horizontal"/>

    <TextView
        android:layout_width="111dp"
        android:layout_height="wrap_content"
        android:text="Condition"
        android:id="@+id/textView2"
        android:layout_weight="0.03"
        android:textSize="20dp" />


    <RadioGroup
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="0.03"
        android:id="@+id/rg1">

        <RadioButton
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/newoption"
            android:text="@string/New"
            android:textSize="20dp"
            android:onClick="Condition"
            />
        <RadioButton
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/usedoption"
            android:text="@string/Used"
            android:textSize="20dp"
            android:onClick="Condition"
            />


    </RadioGroup>

    <TextView
        android:layout_width="88dp"
        android:layout_height="wrap_content"
        android:text="Price"
        android:id="@+id/textView3"
        android:layout_weight="0.03"
        android:textSize="20dp" />

    <EditText
        android:layout_width="184dp"
        android:layout_height="wrap_content"
        android:inputType="numberDecimal"
        android:ems="10"
        android:id="@+id/editText" />

    <Button
        android:layout_width="117dp"
        android:layout_height="wrap_content"
        android:text="Submit"
        android:id="@+id/submit"

        android:layout_gravity="center_horizontal" />

</LinearLayout>

and this is the second activity . java

package com.example.shiva_000.myproject;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.TextView;

/**
 * Created by shiva_000 on 11/18/2015.
 */
public class secondScreen extends Activity {
    ImageView imageView;
    TextView textView1;
    TextView textView2;
    Button button;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.second);

        imageView =(ImageView) findViewById(R.id.image_view);
        textView1=(TextView) findViewById(R.id.textView4);
        textView2 =(TextView) findViewById(R.id.textView5);
        button = (Button)findViewById(R.id.button2);

        //button.setOnClickListener();
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        //String path = getIntent().getStringExtra("")






    }
    public void Condition(View view)
    {
        boolean checked = ((RadioButton)view).isChecked();
        switch (view.getId())
        {
            case R.id.newoption:
                textView1.setText("New");
            case R.id.usedoption:
                    textView1.setText("Used");

        }




    }



    private void initializesecondactivity ()
    {
        double price = getIntent().getExtras().getDouble(MainActivity.Tag_Price);

    }


}

AND LAST this is the xml for second activity

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


    <ImageView
        android:layout_width="437dp"
        android:layout_height="200dp"
        android:id="@+id/image_view"
        android:layout_gravity="center_horizontal"
        android:layout_weight="0.16" />

    <TextView
        android:layout_width="91dp"
        android:layout_height="40dp"
        android:text="Condition : "
        android:id="@+id/textView4"
        android:textSize="20dp" />

    <TextView
        android:layout_width="107dp"
        android:layout_height="wrap_content"
        android:text="Price : "
        android:id="@+id/textView5"
        android:layout_weight="0.16"
        android:textSize="20dp" />

    <Button
        android:layout_width="138dp"
        android:layout_height="wrap_content"
        android:text="Finished"
        android:id="@+id/button2"
        android:layout_gravity="center_horizontal"
        android:layout_weight="0.16"
        android:textSize="20dp" />

</LinearLayout>
shiva upadhyay
  • 331
  • 2
  • 17

2 Answers2

1

Add below code in your activity.

public void Condition(View view) {

    boolean checked = ((RadioButton) view).isChecked();
    switch(view.getId()) {
        case R.id.newoption:
            if (checked)

            break;
        case R.id.usedoption:
            if (checked)

            break;
    }
}

and your code should work!

Nemo
  • 1,059
  • 3
  • 14
  • 31
  • how do i display that selected radio button in another activity? – shiva upadhyay Nov 19 '15 at 00:16
  • Can you elaborate your use case? – Nemo Nov 19 '15 at 00:18
  • so i want that selected radio button to be displayed in the 2nd activity's textview that has an id of textview4. and also the price, how do i display that into textview5? – shiva upadhyay Nov 19 '15 at 00:19
  • Use Bundle to bundle your data and pass it to your another activity. On the receiver side read the Bundle using getExtras(). Bundle bundle = new Bundle(); bundle.putString("key", "data"); Intent a = new Intent(Sender.this, Receiver.class); a.putExtra(bundle); startActivity(a) Bundle getData = getIntent().getExtras(); String getData = getData.getString(“key”); editText.setText(getData); – Nemo Nov 19 '15 at 00:24
  • ok so did that inside the on click for the submit button. and now the app crashes when i click submit button. please help – shiva upadhyay Nov 19 '15 at 00:37
  • Please share the log when even it is possible. – Nemo Nov 19 '15 at 00:42
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/95499/discussion-between-shiva-upadhyay-and-nemo). – shiva upadhyay Nov 19 '15 at 00:44
0

If Im not mistaken to pass the values to another activity you create an Intent ,pass it the extras and then start the activity. That is NOT what you have done there.Take a look at this.
Please paste the logcat output at an error level so we know exactly what is wrong.
But maybe u can take a look at these two tutorials on using radio buttons one and two.

Community
  • 1
  • 1
Manny265
  • 1,709
  • 4
  • 23
  • 42