选择单选按钮时应用程序崩溃了吗?

时间:2015-11-18 23:43:46

标签: java android

大家。我正在制作像craiglist这样的应用程序。它拍摄物品的照片并将其放入图像视图中。我有2个单选按钮。每次我点击单选按钮,应用程序崩溃。相机部分正在工作。好吧,我有几个关于我的应用程序的问题,我无法弄清楚如何做... 1.如何防止选择单选按钮时发生崩溃。 2.如何将用户选择的无线电按钮值传递到另一个屏幕的textview。 3.如何打印在两个屏幕中捕获的图像。喜欢将相同的图像传递给另一个活动。 4.还有物品的价格。如何让它显示在另一个屏幕上。?

下面是我的代码。 随时编辑和更正。 谢谢!

这是清单文件

<?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>

这是主要的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 ,  ){

    //}
}

这是主要活动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>

这是第二项活动。的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这是第二个活动的xml

<?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>

2 个答案:

答案 0 :(得分:1)

在您的活动中添加以下代码。

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

您的代码应该可以使用!

答案 1 :(得分:0)

如果我没有错误地将值传递给另一个活动,则创建一个Intent,将其传递给附加内容,然后启动活动。那不是你在那里做的。看看this.
请将logcat输出粘贴到一个错误级别,这样我们就知道到底出了什么问题。
但也许你可以看看在这两个使用单选按钮onetwo的教程中。