setContentView和findViewById错误

奎克·马丁内斯

我正在尝试将应用程序从android迁移到活动错误,而我又跳到setContentView和findViewById上,不再这样做了,我尝试了所有方法,即使我的基本水平的android都允许我

public class Tactica extends Fragment {
    private ImageView cuadrado;
    private int modificarX = 100;
    private int modificarY = 100;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.tactica);
          cuadrado = (ImageView) findViewById(R.id.imageView1);
          cuadrado.setOnTouchListener(handlerMover);
          View tactics = inflater.inflate(R.layout.tactica, container, false);
          return tactics;
    }
    ...
}
法希姆

使用此代码

@Override

    public View onCreateView(LayoutInflater inflater, ViewGroup container,
    Bundle savedInstanceState) {         
          View tactics = inflater.inflate(R.layout.tactica, container, false);
           cuadrado = (ImageView)tactics.findViewById(R.id.imageView1);
          cuadrado.setOnTouchListener(handlerMover);
          return tactics;
    }

在这里,您不必在onCreateView方法中使用这两行

super.onCreate(savedInstanceState);
setContentView(R.layout.tactica);

而且您必须在膨胀文件之后而不是之前初始化imageview控件

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

setContentView和findViewById

即使在setContentView()之后调用,findViewById()上的空对象引用错误

FindViewById返回null(在OnCreate内部和SetContentView之后仅返回一个)

Sherlock片段findViewById()和getIntent()错误

Calling findViewById() before setContentView() causes NullPointerException but not always?

Android:即使在setContentView之后,findViewById返回Null

SetContentView参数显示错误

setContentView和AsyncTask

如何修复此片段类中的setcontentview错误和内部类错误?

发生 findViewById 方法错误

findViewById中的错误ListView

按钮出现错误findViewById()

setContentView和Android中的按钮

更新 gradle 和 android 支持库版本后,在 xml 布局 setContentView(R.layout.main) 中显示的 TextView 错误

错误:方法调用不明确。AppCompactActivity和Activity中的findViewById(int)

setContentView(R.layout.main); 错误

错误setContentView(R.layout.activity_main)

“错误:无法解析符号 findViewById”

使用findViewById()时出现错误

在setContentView()之前调用findViewById()会导致NullPointerException,但并非总是如此吗?

findViewById <T>()和findViewById()as T有什么区别?

Android Studio-findViewById()导致错误

出现错误“无法解决findViewById(int)”

错误:找不到符号方法findviewbyid

“无法解析片段中的 findviewbyid”错误

findViewById 不断给我错误和头痛

findViewById(R.id.bAdd)错误

对于类型错误,未定义findViewById

Kotlin-解决DataBindingUtil.setContentView()中的错误