【问题标题】:unable to set content with existing layout无法使用现有布局设置内容
【发布时间】:2013-06-01 03:55:31
【问题描述】:

我正在构建一个 nfc 读取标签应用程序。但 我找不到设置布局。问题在
setContentView(R.layout.read);
txtRead = (TextView) findViewById(R.id.txt_read);

这是我的代码:

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
 try{
        requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
        setContentView(R.layout.read);
        getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.read);

     //    nfcAdapter = NfcAdapter.getDefaultAdapter(null);
     //    detectedTag =getIntent().getParcelableExtra(NfcAdapter.EXTRA_TAG);


        txtRead  = (TextView) findViewById(R.id.txt_read);

        pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, 
                new Intent(this,getClass()).
                addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);

        IntentFilter tagDetected = new IntentFilter(NfcAdapter.ACTION_TAG_DISCOVERED);
        IntentFilter filter2     = new IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED);
       // readTagFilters = new IntentFilter[]{tagDetected,filter2};
        Toast.makeText(this, "no Exception caught in on create", Toast.LENGTH_LONG).show();
 }
 catch(Exception e) { Toast.makeText(this, "Exception caught in on create", Toast.LENGTH_LONG).show(); }
    }

【问题讨论】:

  • 从标头包含中删除 android.R 或清理项目:D
  • 尝试从活动中删除 packagename.R 的导入行并清理项目。

标签: android layout nfc oncreate


【解决方案1】:

稍后尝试 setContentView:

    requestWindowFeature(Window.FEATURE_CUSTOM_TITLE); 
    getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.read);
    setContentView(R.layout.read);

请检查您导入的 R。也许您导入了 android.R 而不是从包中导入了 R。

【讨论】:

  • 仍然无法正常工作,它说“读取无法解析或不是字段”
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多