2015-09-29 26 views
1

我正在开发一个允许通过Instagram分享内容的应用程序。我的编码有错误。我不能将instagramApp导入我的编码。它说不能解决符号instagramApp.Can有人指导我解决这个问题。编码和错误日志如下。无法解析符号InstagramApp

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

错误日志:

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 

任何建议和指导来解决这个问题将是非常有益的。谢谢。

回答

1

这条线:

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

告诉你正在使用的包不存在。所以这条路上没有文件。您应该将InstagramApp java文件放在该路径上,或将软件包名称更改为正确的路径。


TL; DR:你试图导入这两个文件:

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

但他们不是您所提供的路径上。您必须将InstagramApp.java文件放在您的instagram文件夹下才能导入。


编辑:您可以阅读this related question关于Java import了解什么是你的代码错误。您还可以阅读关于软件包的this tutorial

第二编辑:您需要将this file置于您的项目下才能导入工作。您无法简单地复制和粘贴示例而无需获取所需的文件。你错过了一个,这就是你得到错误的原因。

+0

是的,我已经添加了进口,但“InstagramApp”以红色突出显示“无法解析符号InstagramApp”。 @Mauker – marian

+0

我知道。导入行在那里,但您要导入的文件不是,这就是问题所在。 – Mauker

+0

我该如何解决这个问题?你有什么建议吗? @Mauker – marian