setText Textview from custum imageview

▼魔方 西西 提交于 2019-12-14 04:11:25

问题


I have an activity which has custom imageView. in imageView, I want to call a method from the activity, and that method wants setText Textview.

is it possible?

I have done these code but I've encountered an error.

MainActivity:

package com.example.helloworld;

import android.os.Bundle;
import android.app.Activity;
import android.util.Log;

public class MainActivity extends Activity {

   @Override
   public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
   }


   pubic void myFunction(int score){
       ((TextView)findViewById(R.id.txtScore)).setText(score+"");
   }

 }

XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
   android:orientation="vertical" >

   <TextView android:id="@+id/txtScore"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="This is a TextView" />

   <com.example.MyImageView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
       />
</LinearLayout>

MyImageViewClass:

public class MyImageViewClass extends ImageView {

     public MyImageViewClass(Context context, AttributeSet attrs) {
          super(context, attrs);
      }

      protected void onDraw(Canvas canvas) {
             ((MainActivity)context).myFunction(12);
       }

 }

when call myFunction() from imageView و Such an error will appear:

 Caused by: android.view.InflateException: Binary XML file line #9: Binary XML file line #9: Error inflating class com.example.adisun.gamesss.ImageViews.GmaeMemClassicImageView
        at android.view.LayoutInflater.inflate(LayoutInflater.java:551)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:429)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:380)
        at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:287)
        at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:139)
        at com.example.adisun.gamesss.activity.GameActivity.onCreate(GameActivity.java:19)
        at android.app.Activity.performCreate(Activity.java:6904)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1136)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3266)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3415) 
        at android.app.ActivityThread.access$1100(ActivityThread.java:229) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1821) 
        at android.os.Handler.dispatchMessage(Handler.java:102) 
        at android.os.Looper.loop(Looper.java:148) 
        at android.app.ActivityThread.main(ActivityThread.java:7331) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120) 
     Caused by: android.view.InflateException: Binary XML file line #9: Error inflating class com.example.adisun.gamesss.ImageViews.GmaeMemClassicImageView
        at android.view.LayoutInflater.createView(LayoutInflater.java:657)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:776)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:716)
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:847)
        at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:810)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:527)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:429) 
        at android.view.LayoutInflater.inflate(LayoutInflater.java:380) 
        at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:287) 
        at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:139) 
        at com.example.adisun.gamesss.activity.GameActivity.onCreate(GameActivity.java:19) 
        at android.app.Activity.performCreate(Activity.java:6904) 
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1136) 
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3266) 
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3415) 
        at android.app.ActivityThread.access$1100(ActivityThread.java:229) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1821) 
        at android.os.Handler.dispatchMessage(Handler.java:102) 
        at android.os.Looper.loop(Looper.java:148) 
        at android.app.ActivityThread.main(ActivityThread.java:7331) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120) 
     Caused by: java.lang.reflect.InvocationTargetException
        at java.lang.reflect.Constructor.newInstance(Native Method)
        at android.view.LayoutInflater.createView(LayoutInflater.java:631)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:776) 
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:716) 
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:847) 
        at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:810) 
        at android.view.LayoutInflater.inflate(LayoutInflater.java:527) 
        at android.view.LayoutInflater.inflate(LayoutInflater.java:429) 
        at android.view.LayoutInflater.inflate(LayoutInflater.java:380) 
        at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:287) 
        at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:139) 
        at com.example.adisun.gamesss.activity.GameActivity.onCreate(GameActivity.java:19) 
        at android.app.Activity.performCreate(Activity.java:6904) 
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1136) 
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3266) 
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3415) 
        at android.app.ActivityThread.access$1100(ActivityThread.java:229) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1821) 
        at android.os.Handler.dispatchMessage(Handler.java:102) 
        at android.os.Looper.loop(Looper.java:148) 
        at android.app.ActivityThread.main(ActivityThread.java:7331) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120) 
     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
        at com.example.adisun.gamesss.activity.GameActivity.setScore(GameActivity.java:38)
        at com.example.adisun.gamesss.ImageViews.GmaeMemClassicImageView.initialize(GmaeMemClassicImageView.java:75)

回答1:


The problem Here ((MainActivity)context).myFunction(12) is getting called inside onDraw. this is why you are getting a NPE->

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference

You can not access a View from onDraw of a another view because layout for the Activity is not inflated Yet . Even if you can this is never a way cause onDraw get called several times whenever invalidate get called.

Look for some elegant way to solve your problem . Your problem is pretty much unclear right not so i can not suggest anything.



来源:https://stackoverflow.com/questions/55315028/settext-textview-from-custum-imageview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!