How to solve the cannot find Symbol variable problem?

Asked

Viewed 1,014 times

0

I am trying to upload an image in android studio but I have a problem that appears an error cannot find Symbol variable in RESULT_LOAD_IMAGE someone knows why this error

my code goes below:

Click function

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.imageToUpload:
                Intent galleryIntent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                startActivityForResult(galleryIntent, RESULT_LOAD_IMAGE);
                break;

            case R.id.bUploadImage:

                break;
            case R.id.bDownloadImage:

                break;
        }
    }

Mainactivity class

// as bibliotecas usadas
import android.graphics.Bitmap.Config;
import android.app.AlertDialog;
import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.provider.MediaStore;
import android.speech.RecognizerIntent;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.widget.DrawerLayout;
import android.text.Html;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.webkit.JavascriptInterface;
import android.webkit.WebView;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Toast;

import java.util.ArrayList;


//A classe MainActivity 

public class MainActivity extends Activity implements  View.OnClickListener{

    ImageView imageToUpload, downloadedImage;
    Button bUploadImage, bDownloadImage;
    EditText uploadImageName, downloadImageName;

    // declare properties

    private static final int SPEECH_REQUEST_CODE = 2;
    private String[] mNavigationDrawerItemTitles;
    private DrawerLayout mDrawerLayout;
    private ListView mDrawerList;

    private ActionBarDrawerToggle mDrawerToggle;

    // nav drawer title
    private CharSequence mDrawerTitle;

    WebView myWebView;

    // used to store app title
    private CharSequence mTitle;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // for proper titles
        mTitle = mDrawerTitle = getTitle();

        // initialize properties
        mNavigationDrawerItemTitles = getResources().getStringArray(R.array.navigation_drawer_items_array);
        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        mDrawerList = (ListView) findViewById(R.id.left_drawer);

        // list the drawer items
        ObjectDrawerItem[] drawerItem = new ObjectDrawerItem[6];

        drawerItem[0] = new ObjectDrawerItem(R.drawable.ic_home, "Home");
        drawerItem[1] = new ObjectDrawerItem(R.drawable.ic_manufacturing, "Manufacturing");
        drawerItem[2] = new ObjectDrawerItem(R.drawable.ic_engineering, "Engineering");
        drawerItem[3] = new ObjectDrawerItem(R.drawable.ic_quality, "Quality");
        drawerItem[4] = new ObjectDrawerItem(R.drawable.ic_orderexecution, "Order Execution");
        drawerItem[5] = new ObjectDrawerItem(R.drawable.ic_exit, "Exit");

        // Pass the folderData to our ListView adapter
        DrawerItemCustomAdapter adapter = new DrawerItemCustomAdapter(this, R.layout.listview_item_row, drawerItem);

        // Set the adapter for the list view
        mDrawerList.setAdapter(adapter);

        // set the item click listener
        mDrawerList.setOnItemClickListener(new DrawerItemClickListener());


        // for app icon control for nav drawer
        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        mDrawerToggle = new ActionBarDrawerToggle(
                this,                  /* host Activity */
                mDrawerLayout,         /* DrawerLayout object */
                R.drawable.ic_drawer_white,  /* nav drawer icon to replace 'Up' caret */
                R.string.drawer_open,  /* "open drawer" description */
                R.string.drawer_close  /* "close drawer" description */
                ) {

            /** Called when a drawer has settled in a completely closed state. */
            public void onDrawerClosed(View view) {
                super.onDrawerClosed(view);
                getActionBar().setTitle(Html.fromHtml("<font color='#ffffff'>"+mTitle+"</font>"));
            }

            /** Called when a drawer has settled in a completely open state. */
            public void onDrawerOpened(View drawerView) {
                super.onDrawerOpened(drawerView);
                //getActionBar().setTitle(mDrawerTitle);
                getActionBar().setTitle(Html.fromHtml("<font color='#ffffff'>Merweb Mobile</font>"));

            }
        };

        // Set the drawer toggle as the DrawerListener
        mDrawerLayout.setDrawerListener(mDrawerToggle);

        // enable ActionBar app icon to behave as action to toggle nav drawer
        getActionBar().setDisplayHomeAsUpEnabled(true);
        getActionBar().setHomeButtonEnabled(true);

        if (savedInstanceState == null) {
            // on first time display view for first nav item
            selectItem(0);
        }

        //Para fazer o upload de imagens
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        imageToUpload = (ImageView) findViewById(R.id.imageToUpload);
        downloadedImage = (ImageView) findViewById(R.id.downloadedImage);

        bUploadImage = (Button) findViewById(R.id.bUploadImage);
        bDownloadImage = (Button) findViewById(R.id.bDownloadImage);

        uploadImageName = (EditText)  findViewById(R.id.etUploadName);
        downloadImageName = (EditText) findViewById(R.id.etDownloadName);

        imageToUpload.setOnClickListener(this);
        bUploadImage.setOnClickListener(this);
        bDownloadImage.setOnClickListener(this);
    }

    // Create an intent that can start the Speech Recognizer activity
    private void displaySpeechRecognizer() {
        Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
        intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
                RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
        // Start the activity, the intent will be populated with the speech text
        try {
            startActivityForResult(intent, SPEECH_REQUEST_CODE);
        } catch (ActivityNotFoundException a) {
            Toast t = Toast.makeText(getApplicationContext(), "Ops! Your device doesn't support Speech to Text", Toast.LENGTH_SHORT);
            t.show();
        }

    }

    // Aqui estamos pegando o codigo de Barras
    public void onActivityResult(int requestCode, int resultCode, Intent intent, Intent data) {
        IntentResult scanResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, intent);
        Fragment fragment = null;
        FragmentManager fragmentManager = getFragmentManager();
//        Toast t = Toast.makeText(getApplicationContext(), requestCode + " " + resultCode, Toast.LENGTH_SHORT);
//        t.show();
        if (requestCode == 49374) {
            if (scanResult != null) {

                String re = scanResult.getContents();

                if (re == null) {
                    fragmentManager.beginTransaction().replace(R.id.content_frame, new HomeFragment()).commit();
                } else {
                    Bundle bundle = new Bundle();
                    bundle.putString("barcode", re);

                    fragment = new SEARCHfragment();
                    fragment.setArguments(bundle);
                    fragmentManager.beginTransaction().replace(R.id.content_frame, fragment
                    ).commit();
                }
            } else
                finish();
        }
        if (requestCode == 2) {
            if (resultCode == RESULT_OK && null != intent) {

                ArrayList<String> text = intent
                        .getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);

                Bundle bundle = new Bundle();
                bundle.putString("barcode", text.get(0));

                fragment = new VOICEfragment();
                fragment.setArguments(bundle);
                fragmentManager.beginTransaction().replace(R.id.content_frame, fragment
                ).commit();
                //Toast t = Toast.makeText(getApplicationContext(), text.get(0), Toast.LENGTH_SHORT);
                //t.show();
            }
        }


        //Upload de Imagens
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && data != null){
            Uri selectedImage = data.getData();
            imageToUpload.setImageURI(selectedImage);
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if(event.getAction() == KeyEvent.ACTION_DOWN){
            switch(keyCode)
            {
                case KeyEvent.KEYCODE_BACK:

                    new AlertDialog.Builder(this)
                        .setIcon(android.R.drawable.ic_dialog_alert)
                        .setTitle("Exit!")
                        .setMessage("Are you sure you want to close?")
                        .setPositiveButton("Yes", new DialogInterface.OnClickListener()
                        {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                finish();
                            }

                        })
                            .setNegativeButton("No", null)
                            .show();

                    if(myWebView.canGoBack()){
                        myWebView.goBack();
                    }
                    return true;
            }

        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        // The action bar home/up action should open or close the drawer.
        // ActionBarDrawerToggle will take care of this.
       if (mDrawerToggle.onOptionsItemSelected(item)) {
           return true;
       }

       if(item.getItemId()==R.id.action_scan){
           //Toast.makeText(this, "SCAN selected", Toast.LENGTH_SHORT).show();
           IntentIntegrator integrator = new IntentIntegrator(MainActivity.this);
           integrator.initiateScan();
           return true;
       }

        if(item.getItemId()==R.id.action_microfone){
            displaySpeechRecognizer();
            return true;
        }

       return super.onOptionsItemSelected(item);
    }

    // to change up caret
    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        // Sync the toggle state after onRestoreInstanceState has occurred.
        mDrawerToggle.syncState();
    }


    // navigation drawer click listener
    private class DrawerItemClickListener implements ListView.OnItemClickListener {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            selectItem(position);
        }

    }

    private void selectItem(int position) {

        // update the main content by replacing fragments

        Fragment fragment = null;

        switch (position) {
        case 0:
            fragment = new HomeFragment();
            break;
        case 1:
            fragment = new MFGfragment();
            break;
        case 2:
            fragment = new ENGfragment();
            break;
        case 3:
            fragment = new QUAfragment();
            break;
        case 4:
            fragment = new OEfragment();
            break;
        case 5:
            break;
        default:
            break;
        }

        if (fragment != null) {
            FragmentManager fragmentManager = getFragmentManager();
            fragmentManager.beginTransaction().replace(R.id.content_frame, fragment).commit();

            // update selected item and title, then close the drawer
            mDrawerList.setItemChecked(position, true);
            mDrawerList.setSelection(position);
            setTitle(mNavigationDrawerItemTitles[position]);
            mDrawerLayout.closeDrawer(mDrawerList);

        } else {
            //mDrawerLayout.closeDrawer(mDrawerList);
            // error in creating fragment
            System.exit(0);
            //Log.e("MainActivity", "Error in creating fragment");
        }
    }

    @Override
    public void setTitle(CharSequence title) {
        mTitle = title;
        getActionBar().setTitle(Html.fromHtml("<font color='#ffffff'>"+mTitle+"</font>"));
    }






    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.imageToUpload:
                Intent galleryIntent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                startActivityForResult(galleryIntent, RESULT_LOAD_IMAGE);
                break;

            case R.id.bUploadImage:

                break;
            case R.id.bDownloadImage:

                break;
        }
    }
}

XML code

<ScrollView  xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:padding="10dp"
    android:layout_height="match_parent">

<LinearLayout
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <ImageView
        android:id="@+id/imageToUpload"
        android:layout_gravity="center_horizontal"
        android:layout_width="150dp"
        android:layout_height="150dp" />

    <EditText
        android:id="@+id/etUploadName"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <Button
        android:id="@+id/bUploadImage"
        android:text="Upload Image"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <ImageView
        android:id="@+id/downloadedImage"
        android:layout_gravity="center_horizontal"
        android:layout_width="150dp"
        android:layout_height="150dp" />

    <EditText
        android:id="@+id/etDownloadName"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <Button
        android:id="@+id/bDownloadImage"
        android:text="Download Image"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</LinearLayout>

</ScrollView>

2 answers

2

This message is a generic message from the Java language. The compiler is indicating that it does not know what RESULT_LOAD_IMAGE is.

Probably, you must have copied the code from somewhere and came with this value, which by the characteristics, according to Code Conventions, must be a constant defined in some class that you should be importing or extending.

In the case of this particular api, it is a variable that will represent the code that identifies Intent, the result of its operation as RESULT_LOAD_IMAGE.

Create this constant in your class like this, for example:

private static final int RESULT_LOAD_IMAGE = 1;

Reference: Android Developers Site

  • Sebastian, welcome to Stackoverflow. I congratulate him on his good answer which, in addition to solving the PA problem, provides an explanation of why it happened.

0

You need to declare this variable with some value, for example:

private static final int RESULT_LOAD_IMAGE = 1;

Browser other questions tagged

You are not signed in. Login or sign up in order to post.