【问题标题】:Android studio 2.1.2 - error:cannot resolve method setText(java.lang.string)Android Studio 2.1.2 - 错误:无法解析方法 setText(java.lang.string)
【发布时间】:2016-07-16 16:46:04
【问题描述】:
package com.example.grghajj1.memecreator1;

import android.os.Bundle;  
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.TextView; 



public class BottomPictureFragment extends Fragment {

private static View topMemeText;
private static View bottomMemeText;

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup     container, @Nullable Bundle savedInstanceState) {  

    View view = inflater.inflate(R.layout.bottom_picture_fragment, container, false);  

    topMemeText = (TextView)view.findViewById(R.id.topMemeText);        
    bottomMemeText = (TextView)view.findViewById(R.id.bottomMemeText);  

    return view; 
}


public void setMemeText(String top, String bottom){
      topMemeText.setText(top);
      bottomMemeText.setText(bottom);
}

编译器显示错误:找不到符号方法 setText(String) 并且无法解析方法 setText(java.lang.String)。

请有人告诉我如何解决这个问题

【问题讨论】:

  • topMemeText bottomMemeText 声明为TextView 而不是View,因为setText 方法在TextView 中,而不是在TextView 的超类(视图)中
  • 移除 View 并改用 TextView
  • 是的,这是遗漏的部分,问题已解决,谢谢@ρяσѕρєяK
  • 是的,这是遗漏的部分,问题已解决,谢谢@Tapanparmar

标签: java android


【解决方案1】:

您已将 TextView 声明为 View,请更改以下几行:

private static View topMemeText;
private static View bottomMemeText;

到:

private static TextView topMemeText;
private static TextView bottomMemeText;

【讨论】:

  • 是的,这是遗漏的部分,问题已解决,谢谢
【解决方案2】:

只需将 View 更改为 TextView,因为 setText() 方法用于 TextViews 而不是 Views,Gradle 找不到您尝试调用的相应方法这是你的问题。只需尝试按原样声明组件并避免父类声明,例如视图。

【讨论】:

    猜你喜欢
    • 2018-05-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-05
    • 1970-01-01
    • 2016-11-24
    相关资源
    最近更新 更多