我想设置一个ImageView
,并希望为其保存一个特殊属性。
所以我设置ImageView
和
FrameLayout root1 = (FrameLayout) findViewById(root);
view.setBackgroundResource(R.drawable.seite2);
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(50, 8);
root1.addView(view, params);
view.bringToFront();
view.setX(paramsinx);
view.setY(paramsiny);
现在我想将属性保存到每个视图。例如,通过创建我为每个新视图设置属性name = tree。 稍后,我想将其编辑为例如name = plant或name = blueberry,并从图像中获取名称以识别它或者制作一些if条件。
我尝试了标签,但问题在于它是用于对象而不是名称或ID。
编辑输出:
> E/AndroidRuntime: FATAL EXCEPTION: main
> Process: com.example.appdesp_seminarsapp_entwicklung20172018, PID: 5319
> java.lang.IllegalStateException: Could not execute method for android:onClick
> at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:293)
> at android.view.View.performClick(View.java:5610)
> at android.view.View$PerformClick.run(View.java:22265)
> at android.os.Handler.handleCallback(Handler.java:751)
> at android.os.Handler.dispatchMessage(Handler.java:95)
> at android.os.Looper.loop(Looper.java:154)
> at android.app.ActivityThread.main(ActivityThread.java:6077)
> at java.lang.reflect.Method.invoke(Native Method)
> at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
> at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
> Caused by: java.lang.reflect.InvocationTargetException
> at java.lang.reflect.Method.invoke(Native Method)
> at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
> at android.view.View.performClick(View.java:5610)
> at android.view.View$PerformClick.run(View.java:22265)
> at android.os.Handler.handleCallback(Handler.java:751)
> at android.os.Handler.dispatchMessage(Handler.java:95)
> at android.os.Looper.loop(Looper.java:154)
> at android.app.ActivityThread.main(ActivityThread.java:6077)
> at java.lang.reflect.Method.invoke(Native Method)
> at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
> at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
> Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object
> android.widget.ImageView.getTag()' on a null object reference
> at com.example.appdesp_seminarsapp_entwicklung20172018.Klassisch.angemalt(Klassisch.java:313)
> at java.lang.reflect.Method.invoke(Native Method)
> at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
> at android.view.View.performClick(View.java:5610)
> at android.view.View$PerformClick.run(View.java:22265)
> at android.os.Handler.handleCallback(Handler.java:751)
> at android.os.Handler.dispatchMessage(Handler.java:95)
> at android.os.Looper.loop(Looper.java:154)
> at android.app.ActivityThread.main(ActivityThread.java:6077)
> at java.lang.reflect.Method.invoke(Native Method)
> at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
> at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
> Application terminated.
只做吐司并说" hi"作品。使用gettag会出现此错误。
String test = (String) img1.getTag();
Toast.makeText(Klassisch.this, "hi"+test, Toast.LENGTH_LONG).show();
(我创建了一个imageview img1) Edit2:感谢您正确插入代码
答案 0 :(得分:1)
你看上去正确,标签就是你所需要的!
使用setTag
方法设置您的姓名:
String defaultName = "tree";
view.setTag(defaultName);
...
view.setTag("plant");
当您需要它时,只需获取标记值并将其转换为String
String name = (String) view.getTag();
答案 1 :(得分:0)
我在测试项目中尝试过。
这有效,我的问题是: 我使用了全局变量img1和img1.getTag。我在onCreate中设置了img1:test(50,100,img1),我在上面声明了img1。 相反,我现在使用view.getTag()并通过参数传递视图。
2.解决方案:onCreate中的img1与img1在顶层不一样。
package com.example.test;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.View;import android.widget.FrameLayout;import android.widget.ImageView;import android.widget.Toast;public class MainActivity extends AppCompatActivity {ImageView img1;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);
ImageView img1 = new ImageView(this); // correct: img1=new ImageView(this) because already defined on top
test(50,100,img1);
getTag(img1);
}
public void test(int paramsinx, int paramsiny, View view) {
String name= "Marc";
FrameLayout root1 = (FrameLayout) findViewById(R.id.root);
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(50, 8);
root1.addView(view, params);
view.bringToFront();
view.setX(paramsinx);
view.setY(paramsiny);
view.setTag(name);
}
public void getTag(View view) {
String test = (String) view.getTag();
Toast.makeText(MainActivity.this, "hi"+test, Toast.LENGTH_LONG).show();//or directly: public void getTag() {
String test = (String) img1.getTag();
Toast.makeText(MainActivity.this, "hi"+test, Toast.LENGTH_LONG).show();
}
}