Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
137 views
in Technique[技术] by (71.8m points)

android - setText Textview from custum imageview

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)
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...