java - 按钮无 react

标签 java android android-layout android-activity parse-platform

我在我的 Activity 布局中包含了一个名为“btnConfirm”的按钮,并在下面的代码中引用了它(mConfirm),但是,当单击该按钮时,不会触发任何操作。我什至查看了 logcat,单击按钮后没有显示新消息。下面的代码显示了按钮应该触发的操作 - 传输用户输入的信息进行解析,并将用户重定向到另一个 Activity 页面。

如果您需要任何说明,请告诉我。 提前致谢。

public class ProfileCreation extends Activity {

    private static final int RESULT_LOAD_IMAGE = 1;
    FrameLayout layout;
    Button save;
    protected EditText mName;
    protected EditText mAge;
    protected EditText mHeadline;
    protected Button mConfirm;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_profile_creation);

        Parse.initialize(this, "ID", "ID");

        mName = (EditText)findViewById(R.id.etxtname);
        mAge = (EditText)findViewById(R.id.etxtage);
        mHeadline = (EditText)findViewById(R.id.etxtheadline);

        mConfirm = (Button)findViewById(R.id.btnConfirm);
        mConfirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                String name = mName.getText().toString();
                String age = mAge.getText().toString();
                String headline = mHeadline.getText().toString();


                age = age.trim();
                name = name.trim();
                headline = headline.trim();

                if (age.isEmpty() || name.isEmpty() || headline.isEmpty()) {
                    AlertDialog.Builder builder = new AlertDialog.Builder(ProfileCreation.this);
                    builder.setMessage(R.string.signup_error_message)
                        .setTitle(R.string.signup_error_title)
                        .setPositiveButton(android.R.string.ok, null);
                    AlertDialog dialog = builder.create();
                    dialog.show();
                }
                else {
                    // create the new user!
                    setProgressBarIndeterminateVisibility(true);

                    ParseUser currentUser = ParseUser.getCurrentUser(); 
                    currentUser.put("name", name); 
                    currentUser.put("age", age); 
                    currentUser.put("headline", headline); 
                    currentUser.saveInBackground(new SaveCallback() {
                        @Override
                        public void done(ParseException e) {
                            setProgressBarIndeterminateVisibility(false);

                            if (e == null) {
                                // Success!
                                Intent intent = new Intent(ProfileCreation.this, MoodActivity.class);
                                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                                intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
                                startActivity(intent);
                            }
                            else {
                                AlertDialog.Builder builder = new AlertDialog.Builder(ProfileCreation.this);
                                builder.setMessage(e.getMessage())
                                    .setTitle(R.string.signup_error_title)
                                    .setPositiveButton(android.R.string.ok, null);
                                AlertDialog dialog = builder.create();
                                dialog.show();
                            }
                        }
                    });
                }
            }
        });

        setContentView(R.layout.activity_profile_creation);
        save = (Button) findViewById(R.id.button2);
        String picturePath = PreferenceManager.getDefaultSharedPreferences(this).getString("picturePath", "");
        if (!picturePath.equals("")) {
            ImageView imageView = (ImageView) findViewById(R.id.profilePicturePreview);
            imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));
        }

        SeekBar seekBar = (SeekBar) findViewById(R.id.seekBarDistance);
        final TextView seekBarValue = (TextView) findViewById(R.id.seekBarDistanceValue);

        seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {

            @Override
            public void onProgressChanged(SeekBar seekBar, int progress,
                    boolean fromUser) {
                // TODO Auto-generated method stub 
                seekBarValue.setText(String.valueOf(progress));
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
                // TODO Auto-generated method stub 
            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                // TODO Auto-generated method stub 
            }

        }); // Add this

        SeekBar seekBarMinimum = (SeekBar) findViewById(R.id.seekBarMinimumAge);
        final TextView txtMinimum = (TextView) findViewById(R.id.tMinAge);

        seekBarMinimum.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {

            @Override
            public void onProgressChanged(SeekBar seekBar, int progress,
                    boolean fromUser) {
                // TODO Auto-generated method stub 
                txtMinimum.setText(String.valueOf(progress));
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
                // TODO Auto-generated method stub 
            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                // TODO Auto-generated method stub 
            }

        }); // Add this

        SeekBar seekBarMaximum = (SeekBar) findViewById(R.id.seekBarMaximumAge);
        final TextView txtMaximum = (TextView) findViewById(R.id.tMaxAge);

        seekBarMaximum.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {

            @Override
            public void onProgressChanged(SeekBar seekBar, int progress,
                    boolean fromUser) {
                // TODO Auto-generated method stub 
                txtMaximum.setText(String.valueOf(progress));
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
                // TODO Auto-generated method stub 
            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                // TODO Auto-generated method stub 
            }

        }); // Add this




        Button buttonLoadImage = (Button) findViewById(R.id.btnPictureSelect);
        buttonLoadImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {
                Intent i = new Intent(
                        Intent.ACTION_PICK,
                        android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                startActivityForResult(i, RESULT_LOAD_IMAGE);
            }
        });
        save.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {
                // Locate the image in res > 
                Bitmap bitmap = BitmapFactory.decodeFile("picturePath");
                // Convert it to byte
                ByteArrayOutputStream stream = new ByteArrayOutputStream();
                // Compress image to lower quality scale 1 - 100
                bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);

                Object image = null;
                try {
                    String path = null;
                    image = readInFile(path);
                } catch (Exception e) {
                    e.printStackTrace();
                }

                // Create the ParseFile
                ParseFile file = new ParseFile("picturePath", (byte[]) image);
                // Upload the image into Parse Cloud
                file.saveInBackground();

                // Create a New Class called "ImageUpload" in Parse
                ParseObject imgupload = new ParseObject("Image");

                // Create a column named "ImageName" and set the string
                imgupload.put("Image", "picturePath");


                // Create a column named "ImageFile" and insert the image
                imgupload.put("ImageFile", file);

                // Create the class and the columns
                imgupload.saveInBackground();

                // Show a simple toast message


            }
        });
    }

        @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            super.onActivityResult(requestCode, resultCode, data);

            if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK
                    && null != data) {
                Uri selectedImage = data.getData();
                String[] filePathColumn = { MediaStore.Images.Media.DATA };

                Cursor cursor = getContentResolver().query(selectedImage,
                        filePathColumn, null, null, null);
                cursor.moveToFirst();

                int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
                String picturePath = cursor.getString(columnIndex);
                cursor.close();

                ImageView imageView = (ImageView) findViewById(R.id.profilePicturePreview);
                imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));

            }

        }

        private byte[] readInFile(String path) throws IOException {
            // TODO Auto-generated method stub
            byte[] data = null;
            File file = new File(path);
            InputStream input_stream = new BufferedInputStream(new FileInputStream(
                    file));
            ByteArrayOutputStream buffer = new ByteArrayOutputStream();
            data = new byte[16384]; // 16K
            int bytes_read;
            while ((bytes_read = input_stream.read(data, 0, data.length)) != -1) {
                buffer.write(data, 0, bytes_read);
            }
            input_stream.close();
            return buffer.toByteArray();

        }
    }

xml代码

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/dark_texture_blue" >

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="797dp"
        android:gravity="center"
        android:orientation="vertical" >

        <com.mikhaellopez.circularimageview.CircularImageView
            android:id="@+id/profilePicturePreview"
            android:layout_width="132dp"
            android:layout_height="120dp"
            android:layout_below="@+id/textView5"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="7dp"
            android:alpha="1" />

        <Button
            android:id="@+id/button2"
            android:layout_width="120dp"
            android:layout_height="60dp"
            android:layout_above="@+id/etxtage"
            android:layout_alignLeft="@+id/etxtname"
            android:alpha="0.8"
            android:background="#330099"
            android:text="Upload from Facebook"
            android:textColor="#ffffff"
            android:textSize="17sp"
            android:textStyle="bold" />

        <Button
            android:id="@+id/btnPictureSelect"
            android:layout_width="118dp"
            android:layout_height="60dp"
            android:layout_alignRight="@+id/etxtname"
            android:layout_below="@+id/profilePicturePreview"
            android:layout_marginRight="8dp"
            android:alpha="0.8"
            android:background="#000000"
            android:onClick="pickPhoto"
            android:text="Select photo from gallery"
            android:textColor="#ffffff"
            android:textSize="17sp"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/textView6"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/textView4"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="9dp"
            android:text="Preferred Name"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:textColor="#ADD8E6"
            android:textSize="20sp"
            android:textStyle="bold"
            android:typeface="serif" />

        <TextView
            android:id="@+id/textView4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="39dp"
            android:gravity="center"
            android:text="Profile Creation"
            android:textColor="#ffffff"
            android:textSize="28sp"
            android:textStyle="bold"
            android:typeface="serif" />

        <TextView
            android:id="@+id/textView3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/button2"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="14dp"
            android:text="Age"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:textColor="#ADD8E6"
            android:textSize="20sp"
            android:textStyle="bold"
            android:typeface="serif" />

        <EditText
            android:id="@+id/etxtage"
            android:layout_width="230dp"
            android:layout_height="wrap_content"
            android:layout_below="@+id/btnPictureSelect"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="35dp"
            android:ems="10"
            android:hint="Please type your age here"
            android:inputType="number"
            android:maxLength="2"
            android:textAlignment="center"
            android:textColor="#f2f2f2"
            android:textSize="18dp" />

        <EditText
            android:id="@+id/etxtname"
            android:layout_width="250dp"
            android:layout_height="wrap_content"
            android:layout_below="@+id/textView6"
            android:layout_centerHorizontal="true"
            android:ems="10"
            android:enabled="true"
            android:hint="Please type your name here"
            android:inputType="textPersonName"
            android:textColor="#ffffff"
            android:textSize="18sp" />

        <TextView
            android:id="@+id/textView5"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/etxtname"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="8dp"
            android:text="Upload your Profile Picture"
            android:textColor="#f2f2f2"
            android:textSize="18sp"
            android:textStyle="bold"
            android:typeface="sans" />

        <RadioGroup
            android:id="@+id/radioGroup1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/textView1"
            android:layout_toLeftOf="@+id/textView3" >

            <RadioButton
                android:id="@+id/radio0"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:checked="true"
                android:textColor="#f2f2f2"
                android:text="Male" />

            <RadioButton
                android:id="@+id/radio1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textColor="#f2f2f2"
                android:text="Female" />

        </RadioGroup>

         <SeekBar
            android:id="@+id/seekBarDistance"
            android:layout_width="250dp"
            android:progress="50"
            android:layout_centerHorizontal="true"
            android:layout_height="wrap_content"
            android:layout_below="@+id/textView12"
            android:layout_marginTop="11dp" />

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignLeft="@+id/textView5"
            android:layout_below="@+id/etxtheadline"
            android:layout_marginTop="38dp"
            android:text="I am a"
            android:textColor="#ADD8E6"
            android:textSize="20sp"
            android:textStyle="bold" />

        <RadioGroup
            android:id="@+id/radioGroup3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignLeft="@+id/textView4"
            android:layout_alignTop="@+id/radioGroup2"
            android:layout_marginTop="10dp" >
        </RadioGroup>

        <RadioGroup
            android:id="@+id/radioGroup2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignRight="@+id/etxtheadline"
            android:layout_below="@+id/textView2" >

            <RadioButton
                android:id="@+id/radio0"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:checked="true"
                android:text="Male"
                android:textColor="#f2f2f2" />

            <RadioButton
                android:id="@+id/radio1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Female"
                android:textColor="#f2f2f2" />
        </RadioGroup>

        <TextView
            android:id="@+id/textView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignBaseline="@+id/textView1"
            android:layout_alignBottom="@+id/textView1"
            android:layout_alignRight="@+id/radioGroup2"
            android:text="Looking for"
            android:textColor="#ADD8E6"
            android:textSize="20sp"
            android:textStyle="bold" />

        <SeekBar
            android:id="@+id/seekBarMinimumAge"
            android:layout_width="220dp"
            android:layout_height="wrap_content"
            android:layout_alignLeft="@+id/textView1"
            android:layout_below="@+id/textView7"
            android:layout_marginTop="11dp"
            android:progress="25" />

        <TextView
            android:id="@+id/textView7"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/etxtage"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="20dp"
            android:text="Minimum Age Looking For"
            android:textColor="#f2f2f2"
            android:textSize="16sp"
            android:textStyle="bold"
            android:typeface="serif" />

        <EditText
            android:id="@+id/etxtheadline"
            android:layout_width="270dp"
            android:layout_height="70dp"
            android:layout_alignLeft="@+id/button2"
            android:layout_below="@+id/textView8"
            android:ems="10"
            android:hint="A quick description of yourself"
            android:singleLine="true"
            android:textAlignment="center"
            android:textColor="#f2f2f2"
            android:textSize="18dp" >

            <requestFocus />
        </EditText>

        <TextView
            android:id="@+id/seekBarDistanceValue"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignRight="@+id/tMinAge"
            android:layout_below="@+id/seekBarDistance"
            android:text="50"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:textColor="#f2f2f2"
            android:textSize="20sp"
            android:textStyle="bold"
            android:typeface="serif" />

        <TextView
            android:id="@+id/textView12"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/radioGroup1"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="19dp"
            android:text="Search Distance (100KM)"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:textColor="#ADD8E6"
            android:textSize="20sp"
            android:textStyle="bold"
            android:typeface="serif" />

        <TextView
            android:id="@+id/tMinAge"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/seekBarMinimumAge"
            android:layout_centerHorizontal="true"
            android:text="25"
            android:textColor="#f2f2f2"
            android:textSize="18sp" />

        <TextView
            android:id="@+id/textView8"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/tMaxAge"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="15dp"
            android:text="Headline"
            android:textColor="#ADD8E6"
            android:textSize="20sp"
            android:textStyle="bold"
            android:typeface="serif" />

        <TextView
            android:id="@+id/textView14"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/tMinAge"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="15dp"
            android:text="Maximum Age Looking For"
            android:textColor="#f2f2f2"
            android:textSize="16sp"
            android:textStyle="bold"
            android:typeface="serif" />

         <SeekBar
            android:id="@+id/seekBarMaximumAge"
            android:layout_width="221dp"
            android:progress="50"
            android:layout_centerHorizontal="true"
            android:layout_height="wrap_content"
            android:layout_below="@+id/textView14"
            android:layout_marginTop="11dp" />

            <TextView
                android:id="@+id/tMaxAge"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@+id/seekBarMaximumAge"
                android:layout_centerHorizontal="true"
                android:text="50"
                android:textColor="#f2f2f2"
                android:textSize="18sp" />

            <CheckBox
                android:id="@+id/checkBox1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@+id/textView16"
                android:layout_centerHorizontal="true"
                android:text="I agree to the terms and Conditions"
                android:textColor="#D2D2D2"
                android:textColorHint="#ffffff" />

             <TextView
                 android:id="@+id/textView16"
                 android:layout_width="280dp"
                 android:layout_height="40dp"
                 android:layout_alignLeft="@+id/checkBox1"
                 android:layout_below="@+id/seekBarDistanceValue"
                 android:layout_marginTop="14dp"
                 android:gravity="center"
                 android:text="Click here to review the terms and conditions"
                 android:textColor="#99CCFF"
                 android:textSize="16sp" />

             <Button
                 android:id="@+id/btnReset"
                 android:layout_width="120dp"
                 android:layout_height="60dp"
                 android:layout_marginBottom="7dp"
                 android:layout_below="@+id/checkBox1"
                 android:layout_toLeftOf="@+id/btnPictureSelect"
                 android:alpha="0.8"
                 android:background="#660000"
                 android:layout_marginTop="14dp"
                 android:text="Reset"
                 android:textColor="#ffffff"
                 android:textSize="17sp"
                 android:textStyle="bold" />

             <Button
                 android:id="@+id/btnConfirm"
                 android:layout_width="120dp"
                 android:layout_height="60dp"
                 android:layout_below="@+id/checkBox1"
                 android:layout_toRightOf="@+id/seekBarDistanceValue"
                 android:alpha="0.8"
                 android:layout_marginTop="14dp"
                 android:layout_marginBottom="7dp"
                 android:background="#330099"
                 android:text="Confirm"
                 android:textColor="#ffffff"
                 android:textSize="17sp"
                 android:textStyle="bold" />

    </RelativeLayout>

</ScrollView>

最佳答案

正如我在您的代码中看到的那样..您设置了相同的内容 View setContentView(R.layout.activity_profile_creation); 两次..

第一个(完美):

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_profile_creation);`

第二个(产生问题):就在保存按钮

之前
    setContentView(R.layout.activity_profile_creation);
    save = (Button) findViewById(R.id.button2);

删除第二个,即保存按钮之前的一个。

关于java - 按钮无 react ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25105494/

相关文章:

Java Clip.play 在第三次调用时挂起

Android ScrollView 拒绝滚动到底部

java - 适用于 Java 和 Javascript 的兼容 AES 算法

android - 在 Android 中上下滑动更改布局

Android 按钮 2 次点击

java - 将多个实体(自定义对象)设置为 httpRequest

java - Android:EditText.getText().toString() 不起作用

java - 从 JSONObject 检索时 JSONArray 为 null

android - 如何使用loopJ SyncHttpClient进行同步调用?

java - RecyclerView 中的 CardView 垂直滚动