Search code examples
javaandroidinstagramandroid-sharing

Cannot resolve symbol InstagramApp


I'm developing an application that allows sharing contents through Instagram. My coding had errors. I cant import the instagramApp into my coding. It says cannot resolve symbol instagramApp.Can someone guide me into solving this issue. The coding and error log are as follows.

MainActivity.java

package com.example.dothis.instagram;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.example.dothis.instagram.InstagramApp;
import    com.example.dothis.instagram.InstagramApp.OAuthAuthenticationListener;

public class MainActivity extends Activity {

private InstagramApp mApp;
private Button btnConnect;
private TextView tvSummary;

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

    mApp = new InstagramApp(this, ApplicationData.CLIENT_ID,
            ApplicationData.CLIENT_SECRET, ApplicationData.CALLBACK_URL);
    mApp.setListener(listener);

    tvSummary = (TextView) findViewById(R.id.tvSummary);

    btnConnect = (Button) findViewById(R.id.btnConnect);
    btnConnect.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View view) {
            if (mApp.hasAccessToken()) {
                final AlertDialog.Builder builder = new AlertDialog.Builder(
                        MainActivity.this);
                builder.setMessage("Disconnect from Instagram?")
                        .setCancelable(false)
                        .setPositiveButton("Yes",
                                new DialogInterface.OnClickListener() {
                                    public void onClick(
                                            DialogInterface dialog, int id) {
                                        mApp.resetAccessToken();
                                        btnConnect.setText("Connect");
                                        tvSummary.setText("Not connected");
                                    }
                                })
                        .setNegativeButton("No",
                                new DialogInterface.OnClickListener() {
                                    public void onClick(
                                            DialogInterface dialog, int id) {
                                        dialog.cancel();
                                    }
                                });
                final AlertDialog alert = builder.create();
                alert.show();
            } else {
                mApp.authorize();
            }
        }
    });

    if (mApp.hasAccessToken()) {
        tvSummary.setText("Connected as " + mApp.getUserName());
        btnConnect.setText("Disconnect");
    }

}

OAuthAuthenticationListener listener = new OAuthAuthenticationListener() {

    @Override
    public void onSuccess() {
        tvSummary.setText("Connected as " + mApp.getUserName());
        btnConnect.setText("Disconnect");
    }

    @Override
    public void onFail(String error) {
        Toast.makeText(MainActivity.this, error,   Toast.LENGTH_SHORT).show();
    }
};
}

Error log :

Information:Gradle tasks [:app:assembleDebug]
:app:preBuild
:app:compileDebugNdk
:app:preDebugBuild
:app:checkDebugManifest
:app:preReleaseBuild
:app:prepareComAndroidSupportAppcompatV72103Library UP-TO-DATE
:app:prepareComAndroidSupportSupportV42103Library UP-TO-DATE
:app:prepareDebugDependencies
:app:compileDebugAidl UP-TO-DATE
:app:compileDebugRenderscript UP-TO-DATE
:app:generateDebugBuildConfig UP-TO-DATE
:app:generateDebugAssets UP-TO-DATE
:app:mergeDebugAssets UP-TO-DATE
:app:generateDebugResValues UP-TO-DATE
:app:generateDebugResources UP-TO-DATE
:app:mergeDebugResources UP-TO-DATE
:app:processDebugManifest UP-TO-DATE
:app:processDebugResources UP-TO-DATE
:app:generateDebugSources UP-TO-DATE
:app:compileDebugJava
/Users/marian/AndroidStudioProjects/Instagram/app/src/main/java/com/example/dothis/instagram/MainActivity.java
Error:(13, 36) error: cannot find symbol class InstagramApp
Error:(14, 49) error: package com.example.dothis.instagram.InstagramApp does not exist
Error:(18, 13) error: cannot find symbol class InstagramApp
Error:(74, 5) error: cannot find symbol class OAuthAuthenticationListener
Error:(25, 32) error: cannot find symbol variable main
Error:(27, 20) error: cannot find symbol class InstagramApp
Error:(74, 48) error: cannot find symbol class OAuthAuthenticationListener
Error:Execution failed for task ':app:compileDebugJava'.
> Compilation failed; see the compiler error output for details.
Information:BUILD FAILED
Information:Total time: 3.909 secs
Information:8 errors
Information:0 warnings
Information:See complete output in console

Any suggestions and guidance to solve this issue would be really helpful. Thank you.


Solution

  • This line:

    error: package com.example.dothis.instagram.InstagramApp does not exist Error:(18, 13)
    

    Is telling that the package you're using doesn't exist. So there's no file on that path. You should place the InstagramApp java file on that path, or change the package name to the correct path.


    TL;DR: You're trying to import those two files:

    import com.example.dothis.instagram.InstagramApp;
    import com.example.dothis.instagram.InstagramApp.OAuthAuthenticationListener;
    

    But they're not on the path you're providing. You have to place the InstagramApp.java file under your instagram folder for the import to work.


    EDIT: You can read this related question about java import to understand what's wrong with your code. You could also read this tutorial about packages.

    Second edit: You need to put this file under your project for the import to work. You can't simply copy and paste the example without getting the required files. You're missing one, that's why you're getting the error.