error: cannot find symbol variable button2 / grml / wieso findet Android Studio den Button nicht??

  • Xamarin.Android

Es gibt 2 Antworten in diesem Thema. Der letzte Beitrag () ist von Morrison.

    error: cannot find symbol variable button2 / grml / wieso findet Android Studio den Button nicht??

    Hallo!

    Ich habe ein Codeschnipsel in dem ich jetzt ein Button einfügen möchte!
    Der Button ist in allen Ansichten vorhanden jedoch bekomme ich immer die Fehlermeldung:

    "error: cannot find symbol variable button2"

    Egal ob ich "findViewById(R.id.button2);" im onCreate oder onClick Event verwende, jedesmal das selbe!

    Zuerst erstelle ich eine Button Variable, dann versuche ich der Variable die ID vom Button zuzuweisen, aber klappt nicht..woran könnte es liegen?!?

    Spoiler anzeigen

    C#-Quellcode

    1. /*
    2. * Copyright (C) The Android Open Source Project
    3. *
    4. * Licensed under the Apache License, Version 2.0 (the "License");
    5. * you may not use this file except in compliance with the License.
    6. * You may obtain a copy of the License at
    7. *
    8. * http://www.apache.org/licenses/LICENSE-2.0
    9. *
    10. * Unless required by applicable law or agreed to in writing, software
    11. * distributed under the License is distributed on an "AS IS" BASIS,
    12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13. * See the License for the specific language governing permissions and
    14. * limitations under the License.
    15. */
    16. package com.google.android.gms.samples.vision.barcodereader;
    17. import android.content.Intent;
    18. import android.os.Bundle;
    19. import android.app.Activity;
    20. import android.util.Log;
    21. import android.view.View;
    22. import android.widget.Button;
    23. import android.widget.CompoundButton;
    24. import android.widget.TextView;
    25. import com.google.android.gms.common.api.CommonStatusCodes;
    26. import com.google.android.gms.vision.barcode.Barcode;
    27. /**
    28. * Main activity demonstrating how to pass extra parameters to an activity that
    29. * reads barcodes.
    30. */
    31. public class MainActivity extends Activity implements View.OnClickListener {
    32. // use a compound button so either checkbox or switch widgets work.
    33. private CompoundButton autoFocus;
    34. public static CompoundButton useFlash;
    35. private TextView statusMessage;
    36. private TextView barcodeValue;
    37. private Button okButton;
    38. private static final int RC_BARCODE_CAPTURE = 9001;
    39. private static final String TAG = "BarcodeMain";
    40. @Override
    41. protected void onCreate(Bundle savedInstanceState) {
    42. super.onCreate(savedInstanceState);
    43. setContentView(R.layout.activity_main);
    44. statusMessage = findViewById(R.id.status_message);
    45. barcodeValue = findViewById(R.id.barcode_value);
    46. autoFocus = findViewById(R.id.auto_focus);
    47. useFlash = findViewById(R.id.use_flash);
    48. okButton = findViewById(R.id.button2); // Hier meckert er!!!
    49. findViewById(R.id.read_barcode).setOnClickListener(this);
    50. // bOK.setOnClickListener(this);
    51. }
    52. /**
    53. * Called when a view has been clicked.
    54. *
    55. * @param v The view that was clicked.
    56. */
    57. @Override
    58. public void onClick(View v) {
    59. if (v.getId() == R.id.read_barcode) {
    60. // launch barcode activity.
    61. Intent intent = new Intent(this, BarcodeCaptureActivity.class);
    62. // intent.putExtra(BarcodeCaptureActivity.AutoFocus, autoFocus.isChecked());
    63. intent.putExtra(BarcodeCaptureActivity.UseFlash, useFlash.isChecked());
    64. startActivityForResult(intent, RC_BARCODE_CAPTURE);
    65. okButton.setVisibility(View.VISIBLE);
    66. }
    67. }



    Das gibt er mir noch inne Gradle Console:
    Spoiler anzeigen
    ​C:\Users\Morrison\Desktop\android-vision-master\visionSamples\barcode-reader\app\src\main\java\com\google\android\gms\samples\vision\barcodereader\MainActivity.java:60: error: cannot find symbol
    okButton = findViewById(button2);
    ^
    symbol: variable button2
    location: class MainActivity
    Note: Some input files use or override a deprecated API.
    Note: Recompile with -Xlint:deprecation for details.
    Note: C:\Users\Morrison\Desktop\android-vision-master\visionSamples\barcode-reader\app\src\main\java\com\google\android\gms\samples\vision\barcodereader\ui\camera\GraphicOverlay.java uses unchecked or unsafe operations.
    Note: Recompile with -Xlint:unchecked for details.
    1 error
    FAILED
    :app:buildInfoGeneratorDebug
    FAILURE: Build failed with an exception.
    * What went wrong:
    Execution failed for task ':app:compileDebugJavaWithJavac'.
    > Compilation failed; see the compiler error output for details.
    * Try:
    Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.
    * Get more help at help.gradle.org
    BUILD FAILED in 1s

    Dieser Beitrag wurde bereits 1 mal editiert, zuletzt von „Morrison“ ()

    Was mich grad wundert: Warum meckert dir die IDE wegen fehlender Konvertierung zwischen android.view.View zu android.widget.Button?

    Schreib doch bitte mal
    1.) welche IDE zu verwendest
    2.) Den AXML Code zur Definition des Buttons
    3.) Welche Android/Java Version Du verwendest und
    4.) Welche Gradle Config Du verwendest

    Lg Radinator
    In general (across programming languages), a pointer is a number that represents a physical location in memory. A nullpointer is (almost always) one that points to 0, and is widely recognized as "not pointing to anything". Since systems have different amounts of supported memory, it doesn't always take the same number of bytes to hold that number, so we call a "native size integer" one that can hold a pointer on any particular system. - Sam Harwell
    Android 3.0.1

    build.gradle:
    Spoiler anzeigen

    XML-Quellcode

    1. apply plugin: 'com.android.application'
    2. android {
    3. compileSdkVersion 27
    4. buildToolsVersion '26.0.2'
    5. defaultConfig {
    6. applicationId "com.google.android.gms.samples.vision.barcodereader"
    7. minSdkVersion 19
    8. targetSdkVersion 27
    9. versionCode 1
    10. versionName "1.0"
    11. }
    12. buildTypes {
    13. release {
    14. minifyEnabled false
    15. proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    16. }
    17. }
    18. }
    19. dependencies {
    20. compile fileTree(dir: 'libs', include: ['*.jar'])
    21. compile 'com.android.support:support-v4:27.1.0'
    22. // Important - the CameraSource implementation in this project requires version 8.1 or higher.
    23. compile 'com.google.android.gms:play-services-vision:11.8.0'
    24. compile 'com.android.support:design:27.1.0'
    25. }



    activity-main.xml
    Spoiler anzeigen

    XML-Quellcode

    1. <?xml version="1.0" encoding="utf-8"?>
    2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    3. xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    4. android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    5. android:paddingRight="@dimen/activity_horizontal_margin"
    6. android:paddingTop="@dimen/activity_vertical_margin"
    7. android:paddingBottom="@dimen/activity_vertical_margin"
    8. tools:context="com.google.android.gms.samples.vision.barcodereader.MainActivity">
    9. <TextView
    10. android:layout_width="match_parent"
    11. android:layout_height="wrap_content"
    12. android:textAppearance="?android:attr/textAppearanceLarge"
    13. android:text="@string/barcode_header"
    14. android:id="@+id/status_message"
    15. android:layout_alignParentEnd="true"
    16. android:textAlignment="center"
    17. android:layout_centerHorizontal="true" />
    18. <TextView
    19. android:layout_width="wrap_content"
    20. android:layout_height="wrap_content"
    21. android:textAppearance="?android:attr/textAppearanceLarge"
    22. android:id="@+id/barcode_value"
    23. android:layout_below="@+id/status_message"
    24. android:layout_alignParentStart="true"
    25. android:layout_marginTop="110dp"
    26. android:layout_alignEnd="@+id/status_message" />
    27. <Button
    28. android:layout_width="wrap_content"
    29. android:layout_height="wrap_content"
    30. android:text="@string/read_barcode"
    31. android:id="@+id/read_barcode"
    32. android:layout_alignParentBottom="true"
    33. android:layout_centerHorizontal="true" />
    34. <Switch
    35. android:layout_width="wrap_content"
    36. android:layout_height="wrap_content"
    37. android:text="@string/auto_focus"
    38. android:id="@+id/auto_focus"
    39. android:layout_above="@+id/read_barcode"
    40. android:layout_alignParentStart="true"
    41. android:layout_marginBottom="77dp"
    42. android:checked="false" />
    43. <Switch
    44. android:id="@+id/use_flash"
    45. android:layout_width="wrap_content"
    46. android:layout_height="wrap_content"
    47. android:layout_alignEnd="@+id/barcode_value"
    48. android:layout_alignTop="@+id/auto_focus"
    49. android:checked="false"
    50. android:text="@string/use_flash" />
    51. <Button
    52. android:id="@+id/button2"
    53. android:layout_width="wrap_content"
    54. android:layout_height="wrap_content"
    55. android:layout_alignBottom="@+id/auto_focus"
    56. android:layout_centerHorizontal="true"
    57. android:text="@string/button_AddOCR"
    58. android:visibility="invisible" />
    59. </RelativeLayout>