编辑:这个问题已经解决了!
我正面临一个希望很简单的问题。当用户点击图像/区域时,我需要在onTouch位置显示/添加图像。 这是布局:
activity_test_diff.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"
tools:context=".TestDiff"
android:orientation="vertical">
<RelativeLayout
android:id="@+id/relative_layout"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<ImageView
android:id="@+id/img_rectangle"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="fitCenter"
android:visibility="invisible"/>
<ImageView
android:id="@+id/img_test_diff1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="fitCenter" />
<ImageView
android:id="@+id/click_check_ok"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/check_green_48dp"/>
<ImageView
android:id="@+id/click_check_ko"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone"
android:src="@drawable/close_red_48dp"/>
</RelativeLayout>
<ImageView
android:id="@+id/img_test_diff2"
android:layout_width="match_parent"
android:layout_weight="1"
android:layout_height="0dp"
android:scaleType="fitCenter"/>
</LinearLayout>
相对布局的父组是一个LinearLayout,但我不在乎是否有人在RelativeLayout之外触摸。这是处理触摸的代码:
TestDiff.java
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test_diff);
RelativeLayout relative = (RelativeLayout) findViewById(R.id.relative_layout);
relative.setOnTouchListener(this);
}
@Override
public boolean onTouch(View v, MotionEvent ev) {
final int action = ev.getAction();
final int evX = (int) ev.getX();
final int evY = (int) ev.getY();
switch (action) {
case MotionEvent.ACTION_DOWN:
int x = (int) ev.getX();
int y = (int) ev.getY();
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
ImageView iv = new ImageView(getApplicationContext());
lp.setMargins(x, y, 0, 0);
iv.setLayoutParams(lp);
iv.setImageDrawable(getResources().getDrawable(
R.drawable.check_green_48dp));
((ViewGroup) v).addView(iv);
return true;
case MotionEvent.ACTION_UP:
// On the UP, we do the click action.
// The hidden image (img_rectangle) has three different hotspots on it.
// The colors are red, blue, and yellow.
// Use img_rectangle to determine which region the user
return true;
default:
return false;
}
}
这就是即将到来的错误:
Attempt to invoke virtual method 'void android.widget.RelativeLayout.setOnTouchListener(android.view.View$OnTouchListener)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646)
意味着有人可以弄清楚发生了什么! 谢谢
答案 0 :(得分:0)
:
<RelativeLayout
android:id="@+id/relative_layout"
你拥有的java代码中的
RelativeLayout relative = (RelativeLayout) findViewById(R.id.layout_relative)
;
你逆转了亲戚布局词