2013-07-20 105 views
2

我在Android的Java編程新手,我有一個代碼,以截圖:layoutroot解決不了

View content = findViewById(R.id.layoutroot); //it gives the "layoutroot cannot be resolved or is not a field" error 
content.setDrawingCacheEnabled(true);   
Bitmap bitmap = content.getDrawingCache(); 
File file = new File(Environment.getExternalStorageDirectory() + "/test.png"); 
      try 
      { 
       file.createNewFile(); 
       FileOutputStream ostream = new FileOutputStream(file); 
       bitmap.compress(CompressFormat.PNG, 100, ostream); 
       ostream.close(); 
      } 
      catch (Exception e) 
      { 
       e.printStackTrace(); 
      } 

它給人的錯誤:

"layoutroot cannot be resolved or is not a field"

我不不知道我是否必須定義「layoutroot」,只是不知道! 任何人都可以幫助我解決這個問題?謝謝!

回答

0

檢查,如果導入android.R

比刪除以下導入

import android.R; 

它應該是:

import your.application.packagename.R; 

編輯

你也必須定義layoutroot

+0

我沒有那個導入。但我沒有在任何地方定義過layoutroot,我有按鈕和列表視圖,但沒有按照說明。我必須定義它嗎? –

+0

仍然沒有解決它 –

+1

是的,你肯定會聲明! –

1

有一個錯字,當你有:

View content = findViewById(R.id.layouroot); 

你缺少一個 'T',它應該是:

View content = findViewById(R.id.layoutroot); 
+0

我已經添加了缺少的「t」,但它仍然給出了相同的錯誤 –