我创建了一个简单的绘图应用程序,通过我的布局文件中的changeColor()
通过图像按钮调用主活动中的android:onClick="changeColor"
方法。我的主要线性布局中的图像按钮和通过对话框显示的布局。 2个布局具有相同的图像按钮代码,但当我尝试通过对话框中的布局XML文件更改颜色时,它会崩溃。我得到的错误说它无法找到changeColor()
方法。解决我的错误的最佳方法是什么。
MainActivity.java
public class MainActivity extends ActionBarActivity implements OnClickListener {
private PaintView pView;
private ImageButton currentColor;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
pView = (PaintView) findViewById(R.id.drawing);
LinearLayout paintLayout = (LinearLayout) findViewById(R.id.paint_colors);
currentColor = (ImageButton) paintLayout.getChildAt(0);
currentColor.setImageDrawable(getResources().getDrawable(
R.drawable.paint_pressed));
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
switch (item.getItemId()) {
case R.id.color:
Toast.makeText(getApplicationContext(), "edit selected",
Toast.LENGTH_SHORT).show();
Dialog brushColorDialog = new Dialog(this);
brushColorDialog.setTitle("Brush Color:");
brushColorDialog.setContentView(R.layout.colors);
brushColorDialog.show();
return true;
case R.id.brush:
Dialog brushSizeDialog = new Dialog(this);
brushSizeDialog.setTitle("Brush Size:");
brushSizeDialog.setContentView(R.layout.brush_chooser);
Toast.makeText(getApplicationContext(), "Brush Dialog goes here",
Toast.LENGTH_SHORT).show();
brushSizeDialog.show();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
public void changeColor(View view) {
// use chosen color
if (view != currentColor) {
// update color
ImageButton imgView = (ImageButton) view;
String color = view.getTag().toString();
pView.setColor(color);
imgView.setImageDrawable(getResources().getDrawable(
R.drawable.paint_pressed));
currentColor.setImageDrawable(getResources().getDrawable(
R.drawable.paint));
currentColor = (ImageButton) view;
}
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
}
colors.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="400dp"
android:gravity="center"
android:orientation="vertical" >
<LinearLayout
android:id="@+id/paint_colors"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<ImageButton
android:layout_width="@dimen/large_tile"
android:layout_height="@dimen/large_tile"
android:layout_margin="0dp"
android:background="#FFFF0000"
android:contentDescription="@string/paint"
android:onClick=""
android:src="@drawable/paint"
android:tag="#FFFF0000" />
<ImageButton
android:layout_width="@dimen/large_tile"
android:layout_height="@dimen/large_tile"
android:layout_margin="0dp"
android:background="#FF800000"
android:contentDescription="@string/paint"
android:onClick="changeColor"
android:src="@drawable/paint"
android:tag="#FF800000" />
<ImageButton
android:layout_width="@dimen/large_tile"
android:layout_height="@dimen/large_tile"
android:layout_margin="0dp"
android:background="#FFFFFF00"
android:contentDescription="@string/paint"
android:onClick="changeColor"
android:src="@drawable/paint"
android:tag="#FFFFFF00" />
<ImageButton
android:layout_width="@dimen/large_tile"
android:layout_height="@dimen/large_tile"
android:layout_margin="0dp"
android:background="#FF808000"
android:contentDescription="@string/paint"
android:onClick="changeColor"
android:src="@drawable/paint"
android:tag="#FF808000" />
</LinearLayout>
<!-- Bottom Row -->
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<ImageButton
android:layout_width="@dimen/large_tile"
android:layout_height="@dimen/large_tile"
android:layout_margin="0dp"
android:background="#FF00FF00"
android:contentDescription="@string/paint"
android:onClick="changeColor"
android:src="@drawable/paint"
android:tag="#FF00FF00" />
<ImageButton
android:layout_width="@dimen/large_tile"
android:layout_height="@dimen/large_tile"
android:layout_margin="0dp"
android:background="#FF008000"
android:contentDescription="@string/paint"
android:onClick="changeColor"
android:src="@drawable/paint"
android:tag="#FF008000" />
activity_main.xml中
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFCCCCCC"
android:orientation="vertical"
tools:context=".MainActivity" >
<net.soloq.paint4.PaintView
android:id="@+id/drawing"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_marginBottom="3dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="3dp"
android:layout_weight="1"
android:background="#FFFFFFFF" />
<!-- Top Row -->
<LinearLayout
android:id="@+id/paint_colors"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<ImageButton
android:layout_width="@dimen/large_brush"
android:layout_height="@dimen/large_brush"
android:layout_margin="2dp"
android:background="#FF660000"
android:contentDescription="@string/paint"
android:onClick="changeColor"
android:src="@drawable/paint"
android:tag="#FF660000" />
<ImageButton
android:layout_width="@dimen/large_brush"
android:layout_height="@dimen/large_brush"
android:layout_margin="2dp"
android:background="#FFFF0000"
android:contentDescription="@string/paint"
android:onClick="changeColor"
android:src="@drawable/paint"
android:tag="#FFFF0000" />
activity_main.xml中的图像按钮工作正常,但它在colors.xml
上崩溃提前致谢!
答案 0 :(得分:0)
从colors.xml
...
android:onClick="changeColor"
并将id
添加到ImageButton
XML,如下所示......
<ImageButton
android:id="@+id/colorImageButton"
android:layout_width="@dimen/large_tile"
android:layout_height="@dimen/large_tile"
android:layout_margin="0dp"
android:background="#FF800000"
android:contentDescription="@string/paint"
android:src="@drawable/paint"
android:tag="#FF800000" />
然后,将OnClickListener
设置如下......
Dialog brushColorDialog = new Dialog(this);
brushColorDialog.setTitle("Brush Color:");
brushColorDialog.setContentView(R.layout.colors);
ImageButton imageBtn = (ImageButton)brushColorDialog.findViewById(R.id.colorImageButton);
imageBtn.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
//add your code here
}
});
brushColorDialog.show();
答案 1 :(得分:0)
我遇到了同样的问题,如果我没有弄错的话,问题出现在剧中,主题引用应用程序搜索主题中的点击委托。搜索主题引用并删除它(如果存在)。