【发布时间】:2014-03-18 06:29:05
【问题描述】:
我正在创建一个简单的绘图应用程序,它通过我的布局文件中的android:onClick="changeColor" 的图像按钮在我的主要活动中调用changeColor() 方法。我的主要线性布局和通过对话框显示的布局中有图像按钮。这两种布局具有相同的图像按钮代码,但是当我尝试通过对话框中的布局 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 上崩溃
提前致谢!
【问题讨论】:
-
请张贴您的日志猫。而你错过了
break声明 -
03-18 01:44:54.031: E/AndroidRuntime(17948): FATAL EXCEPTION: main 03-18 01:44:54.031: E/AndroidRuntime(17948): Process: net.soloq.paint4, PID: 17948 03-18 01:44:54.031: E/AndroidRuntime(17948): java.lang.IllegalStateException: Could not find a method changeColor(View) in the activity class android.view.ContextThemeWrapper for onClick handler on view class android.widget.ImageButton 03-18 01:44:54.031: E/AndroidRuntime(17948): at android.view.View$1.onClick(View.java:3817) 03-18 01:44:54.031: E/AndroidRuntime(17948): at android.view.View.performClick(View.java:4445)
标签: java android xml android-layout