gpt4 book ai didi

java - Android Studio 中的 "Expected resource of type layout/menu"

转载 作者:行者123 更新时间:2023-11-30 01:12:36 24 4
gpt4 key购买 nike

    package com.example.nimehr.streaming;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import com.example.videostream.VideoViewActivity;

public class MainActivity extends Activity {
Button button;

public MainActivity() {
}

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(2130903040);
this.button = (Button)this.findViewById(R.id.myButton);
this.button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, VideoViewActivity.class);
MainActivity.this.startActivity(intent);
}
});
}

public boolean onCreateOptionsMenu(Menu menu) {
this.getMenuInflater().inflate(2131165184, menu);
return true;
}
}

我在 this.setContentView(2130903040);2131165184 中的 2130903040 收到错误“类型布局/菜单的预期资源” >分别在 this.getMenuInflater().inflate(2131165184, menu); 中。我在此处修复了 this.button = (Button)this.findViewById(2131230720);,方法是将其更改为 R.id.MyButton

最佳答案

不要这样做:

this.setContentView(2130903040);

R 类的存在是为了避免像这样显式使用 id,并用更易读的更清晰的代码替换它:

this.setContentView(R.layout.layout_name_here);

来自文档:

When your application is compiled, aapt generates the R class, which contains resource IDs for all the resources in your res/ directory.

关于java - Android Studio 中的 "Expected resource of type layout/menu",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38272974/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com