drawable-mdpi フォルダーに c1.png という画像があります。これをJavaでレイアウトしたい:
package ...;
import android.os.Bundle;
//import android.provider.MediaStore.Images;
import android.app.ActionBar.LayoutParams;
import android.app.Activity;
import android.view.Menu;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
public class MainActivity extends Activity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
LinearLayout layout = (LinearLayout)findViewById(getCurrentFocus().getId() );
ImageView img = new ImageView(this);
ViewGroup.LayoutParams lp = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
img.setLayoutParams(lp);
img.setImageDrawable(getResources().getDrawable(R.drawable.c1));
layout.addView(img);
setContentView(layout);//Doesn't matter, if it's commented or not. The error still exists.
}
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
起動時に「残念ながら、%application_name が停止しました」というメッセージが表示されました。id パラメータで R.id.* を使用する多くの例を見ましたが、それも (R.id.linearLayout1 のように) 停止したか、R にそのような定数がありませんでした (R.id.c1 のように)。これどうやってするの?