如何在AndroidStudio中修复错误'找不到合适的方法'?

时间:2016-12-17 09:56:10

标签: android

当错误发生时,我一直在AndroidStudio开发一些代码。然后我删除了我已更改的最后一段代码,但错误仍然存​​在!即使在Clean Project项目不再建造之后!

我在文件compile 'com.jjoe64:graphview:4.2.1'中的dependencies中使用了库build.gradle,这是我正在使用的foll代码:

activity_main.xml:     

    <com.jjoe64.graphview.GraphView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/graph" />
</RelativeLayout>

MainActivity.class

public class MainActivity extends AppCompatActivity {

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


        GraphView graph = (GraphView) findViewById(graph);
    }
}

这是完整的错误消息,这没有任何意义:

Error:(23, 39) error: no suitable method found for findViewById(GraphView)
method Activity.findViewById(int) is not applicable
(argument mismatch; GraphView cannot be converted to int)
method AppCompatActivity.findViewById(int) is not applicable
(argument mismatch; GraphView cannot be converted to int)

我打赌你不会得到那个错误,但是我知道。代码工作得很好,但突然发生了这个错误。也许我需要重启AndroidStudio?或者是否有另一个Clean命令?

2 个答案:

答案 0 :(得分:3)

使用

GraphView graph = (GraphView) findViewById(R.id.graph);

而不是

GraphView graph = (GraphView) findViewById(graph);

答案 1 :(得分:3)

在这一行

 GraphView graph = (GraphView) findViewById(graph);

您正在提供刚刚声明为graph的参数的findViewById对象,当然,编译器正在抱怨重载方法findViewById不存在参数a GraphView

findViewById的参数是int,您在布局中声明的视图ID,并列在R.id下。喜欢

  GraphView graph = (GraphView) findViewById(R.id.graph);