Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7025865
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T00:02:21+00:00 2026-05-28T00:02:21+00:00

I have created a Custom View component and my layout file consists of a

  • 0

I have created a Custom View component and my layout file consists of a button and the CustomView.Whenever I press the button,I want to update the Filename in the CustomView class.This occurs by invoking the set_file method of the customview and the set_file method is called in the function perform();.However I get an error while doing so.

HERE are my 3 classes.

TestActivity.java

package com.android.pack;

import java.net.URISyntaxException;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.Toast;

    public class TestActivity extends Activity {
    /** Called when the activity is first created. */
    int height;
    int weight;
    Button select;
    customview cView;
    String path="";
        String pathchk="";
        String temp="";

     private static final int FILE_SELECT_CODE = 0;

        public void perform()
        {
            cView.set_file(pathchk);
            cView.postInvalidate();
        }
         public void showFileChooser(View v) {
            Intent intent = new Intent(Intent.ACTION_GET_CONTENT); 
            intent.setType("*/*"); 
            intent.addCategory(Intent.CATEGORY_OPENABLE);

            try {
                startActivityForResult(
                        Intent.createChooser(intent, "Select a File to Upload"),
                        FILE_SELECT_CODE);

            } catch (android.content.ActivityNotFoundException ex) {
                // Potentially direct the user to the Market with a Dialog
                Toast.makeText(this, "Please install a File Manager.", 
                        Toast.LENGTH_SHORT).show();
            }
        }
     protected void onActivityResult(int requestCode, int resultCode, Intent data)
     {
            switch (requestCode) {
                case FILE_SELECT_CODE:      
                if (resultCode == RESULT_OK) {  
                    // Get the Uri of the selected file 
                    Uri uri = data.getData();
                    Log.v("check", "File Uri: " + uri.toString());
                    // Get the path

                    try {
                        path = FileUtils.getPath(this, uri);
                        pathchk=path.substring(12);
                        perform();



                    } catch (URISyntaxException e) {
                        // TODO Auto-generated catch block
                        Log.v("error", "File Path: "+pathchk);
                    }
                    Log.v("check", "File Path: "+path);
                    // Get the file instance
                    // File file = new File(path);
                    // Initiate the upload
                }           
                break;
            }
        super.onActivityResult(requestCode, resultCode, data);
        }
    @Override

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        LayoutInflater li = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View v=li.inflate(R.layout.main, null);
        cView = (customview)findViewById(R.id.customview1);

       LinearLayout ll = new LinearLayout(this);
        ll.setOrientation(LinearLayout.VERTICAL);
        select=(Button)findViewById(R.id.button1);
       // select.setText("Select a File");
       /* select.setOnClickListener(new Button.OnClickListener(){

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                showFileChooser();
                Toast.makeText(getApplicationContext(), "The File "+path+" is loaded",Toast.LENGTH_LONG);           }





               }); */


       // LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
          //  LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);




        //ll.addView(select, new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));

       //ll.addView(cView, layoutParams);

        setContentView(R.layout.main); 



    }
}

customview.java

package com.android.pack;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.StringTokenizer;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.gesture.Gesture;
import android.gesture.GesturePoint;
import android.gesture.GestureStroke;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Point;
import android.os.Environment;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.View.*
;
import android.widget.Button;
import android.widget.Toast;

public class customview extends View implements OnClickListener 
{
    ArrayList<GesturePoint> child;
    ArrayList<Point> pnts;
    Point pnt=new Point();
     File myFile;
     BufferedReader br;
     FileInputStream in;
     InputStreamReader fin;
     GestureStroke gest;
     Gesture my_final=new Gesture();
     String Token;
     long Time=30000000;
     int flag=0; 
     boolean line=true;
     boolean points=true;
     Button b1;
     int height;
     int weight;
     String Filename="";

     ArrayList<Path> paths;

     Path mypath;
     Paint mypaint=new Paint();//
     public void set_file(String a)// to set the File
     {
         Filename=a;
         //clear_screen();
     }
     public customview(Context context)
     {
         super(context);
         this.setOnClickListener(this);
         mypaint.setColor(Color.RED);
         mypaint.setAntiAlias(true);
         mypaint.setStyle(Paint.Style.STROKE);
         mypaint.setStrokeWidth(5);
         myFile=Environment.getExternalStorageDirectory();

     }
     public customview(Context context,AttributeSet att)
     {
         super(context,att);
         this.setOnClickListener(this);
         mypaint.setColor(Color.RED);
         mypaint.setAntiAlias(true);
         mypaint.setStyle(Paint.Style.STROKE);
         mypaint.setStrokeWidth(5);
         myFile=Environment.getExternalStorageDirectory();
     }
     public void make_shape()
    {
        paths=new ArrayList<Path>();
        pnts=new ArrayList<Point>();
        height=this.getHeight();
        weight=this.getWidth();
        Log.v("Main_custom_height",height+"");
        Log.v("Main_custom_weight",weight+"");
          try
          {
            File file = new File(myFile,Filename);
            br=new BufferedReader(new FileReader(file));

            String read;
            while((read=br.readLine())!=null)
            {

                //Toast.makeText(Create_pathActivity.this,read,Toast.LENGTH_SHORT).show();
                if(read.equals(".PEN_UP"))
                {
                    flag=0;
                    gest=new GestureStroke(child);
                    mypath=gest.getPath();
                    paths.add(mypath);
                    my_final.addStroke(gest);

                }
                if(flag==1)
                {
                    StringTokenizer st = new StringTokenizer(read);
                         for(int i=0;i<=2;i++)
                         {
                             if(i==0)
                             {
                                 Token=st.nextToken();
                                 pnt.x=Integer.parseInt(Token);
                                 //pnt.x=pnt.x+200;
                                 //pnt.x=pnt.x/5;

                                 Log.v("PointX",Token);
                             }
                             if(i==1)
                             {
                                 Token=st.nextToken();
                                 pnt.y=Integer.parseInt(Token);
                                 //pnt.y=pnt.y+165;
                                 //pnt.y=pnt.y/5;
                                 Log.v("PointY",Token);
                             }
                             if(i==2)
                             {

                                 Time=Time+10;
                                 GesturePoint temp=new GesturePoint(pnt.x,pnt.y,Time);
                                 child.add(temp);
                             }

                        }
                         Point p=new Point();
                         p.x=pnt.x;
                         p.y=pnt.y;
                         pnts.add(p);


                }
                if(read.equals(".PEN_DOWN"))
                {
                    flag=1;
                    child=new ArrayList<GesturePoint>();
                }
            }

        }catch (Exception e)
        {


        }
    }


    protected void onDraw(Canvas canvas)
    {
        if(points==true && line==false)
        {

                for (Point point : pnts) {
                    canvas.drawCircle(point.x, point.y, 3,mypaint);
                }   

        }
        if(points==false && line==true)
        {
                            for( int i=0;i<paths.size();i++)
                {
                    canvas.drawPath(paths.get(i), mypaint);
                }

        }

    }
    public void scale()
    {
        int S_width = 320, S_height = 410, width, height, DiffW, DiffH;
          int max_x = 0, max_y = 0, min_x = 320, min_y = 410;
          int new_x, new_y;
          int i;
          float scale;


          for(i=0; i<pnts.size(); i++)
          {
            if( pnts.get(i).x > max_x )
                max_x = pnts.get(i).x;

            if( pnts.get(i).x < min_x )
                min_x = pnts.get(i).x;

            if( pnts.get(i).y > max_y )
                max_y = pnts.get(i).y;

            if( pnts.get(i).y < min_y )
                min_y = pnts.get(i).y;

          }

            width = max_x - min_x;
            height = max_y - min_y;

            DiffW = Math.abs(S_width - width);
            DiffH = Math.abs(S_height - height);

            if( DiffW > DiffH )
                scale = width/300;
            else
                scale = height/390;

            //scaling
            for(i=0; i<pnts.size(); i++)
            {
                pnts.get(i).x /= scale;
                pnts.get(i).y /= scale;
            }

            new_x = max_x - 10;
            new_y = min_y - 10;

            //top-left shifting
            for(i=0; i<pnts.size(); i++)
            {
                pnts.get(i).x -= new_x;
                pnts.get(i).y -= new_y;
            }

    }



    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        if(Filename.equals(""))
        {
            Toast.makeText(getContext(),"Select a file ist", Toast.LENGTH_SHORT);
            return;
        }
        make_shape();

        AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
        builder.setCancelable(true);
        builder.setTitle("Draw a line or a path");
        builder.setInverseBackgroundForced(true);
        builder.setPositiveButton("Line", new DialogInterface.OnClickListener() {
         @Override
        public void onClick(DialogInterface dialog, int which) {
            line=true;
            points=false;
            invalidate();
              }
        });
        builder.setNegativeButton("Point", new DialogInterface.OnClickListener() {
          @Override
         public void onClick(DialogInterface dialog, int which) {
       line=false;
       points=true;
       //scale();
       invalidate();
          }
        });
        AlertDialog alert = builder.create();
        alert.show();


    }


}

**FileUtils.java**


package com.android.pack;

import java.net.URISyntaxException;

import android.content.Context;
import android.database.Cursor;
import android.net.Uri;

public class FileUtils {

    public static String getPath(Context context, Uri uri) throws URISyntaxException {
        if ("content".equalsIgnoreCase(uri.getScheme())) {
            String[] projection = { "_data" };
            Cursor cursor = null;

            try {
                cursor = context.getContentResolver().query(uri, projection, null, null, null);
                int column_index = cursor
                .getColumnIndexOrThrow("_data");
                if (cursor.moveToFirst()) {
                    return cursor.getString(column_index);
                }
            } catch (Exception e) {
                // Eat it
            }
        }

        else if ("file".equalsIgnoreCase(uri.getScheme())) {
            return uri.getPath();
        }

        return null;
    }
}

and here is my main.xml file

<?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" >

     <com.android.pack.customview
        android:id="@+id/customview1"
        android:layout_width="wrap_content"
        android:layout_height="419dp"
        android:layout_weight="0.63"
       />


    <Button
        android:id="@+id/button1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="select a file" 
        android:onClick="showFileChooser"/>

</LinearLayout>

My log cat is

01-09 12:04:42.038: D/szipinf(4990): Initializing inflate state
01-09 12:04:42.248: D/dalvikvm(4990): GC_CONCURRENT freed 281K, 50% free 2860K/5639K, external 0K/0K, paused 4ms+8ms
01-09 12:04:48.668: V/check(4990): File Uri: content://org.openintents.cmfilemanager/mimetype//mnt/sdcard/points.txt
01-09 12:04:48.698: D/AndroidRuntime(4990): Shutting down VM
01-09 12:04:48.698: W/dalvikvm(4990): threadid=1: thread exiting with uncaught exception (group=0x40018560)
01-09 12:04:48.698: E/AndroidRuntime(4990): FATAL EXCEPTION: main
01-09 12:04:48.698: E/AndroidRuntime(4990): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=0, result=-1, data=Intent { act=android.intent.action.VIEW dat=content://org.openintents.cmfilemanager/mimetype//mnt/sdcard/points.txt }} to activity {com.android.pack/com.android.pack.TestActivity}: java.lang.NullPointerException
01-09 12:04:48.698: E/AndroidRuntime(4990):     at android.app.ActivityThread.deliverResults(ActivityThread.java:2653)
01-09 12:04:48.698: E/AndroidRuntime(4990):     at android.app.ActivityThread.handleSendResult(ActivityThread.java:2695)
01-09 12:04:48.698: E/AndroidRuntime(4990):     at android.app.ActivityThread.access$2000(ActivityThread.java:123)
01-09 12:04:48.698: E/AndroidRuntime(4990):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:969)
01-09 12:04:48.698: E/AndroidRuntime(4990):     at android.os.Handler.dispatchMessage(Handler.java:99)
01-09 12:04:48.698: E/AndroidRuntime(4990):     at android.os.Looper.loop(Looper.java:130)
01-09 12:04:48.698: E/AndroidRuntime(4990):     at android.app.ActivityThread.main(ActivityThread.java:3835)
01-09 12:04:48.698: E/AndroidRuntime(4990):     at java.lang.reflect.Method.invokeNative(Native Method)
01-09 12:04:48.698: E/AndroidRuntime(4990):     at java.lang.reflect.Method.invoke(Method.java:507)
01-09 12:04:48.698: E/AndroidRuntime(4990):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:847)
01-09 12:04:48.698: E/AndroidRuntime(4990):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:605)
01-09 12:04:48.698: E/AndroidRuntime(4990):     at dalvik.system.NativeStart.main(Native Method)
01-09 12:04:48.698: E/AndroidRuntime(4990): Caused by: java.lang.NullPointerException
01-09 12:04:48.698: E/AndroidRuntime(4990):     at com.android.pack.TestActivity.perform(TestActivity.java:32)
01-09 12:04:48.698: E/AndroidRuntime(4990):     at com.android.pack.TestActivity.onActivityResult(TestActivity.java:64)
01-09 12:04:48.698: E/AndroidRuntime(4990):     at android.app.Activity.dispatchActivityResult(Activity.java:3908)
01-09 12:04:48.698: E/AndroidRuntime(4990):     at android.app.ActivityThread.deliverResults(ActivityThread.java:2649)
01-09 12:04:48.698: E/AndroidRuntime(4990):     ... 11 more

Appreciate any help!!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T00:02:22+00:00Added an answer on May 28, 2026 at 12:02 am

    You have not done SetcontentView to call findviewby id. You will have to call

    cView = (customview)v.findViewById(R.id.customview1);

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have created a Dynamic View Panel custom control and want to add a
I have a custom tableview cell created programmatically that consists of: a background view
I have created a custom view by extending Relative Layout and it looks like
I have created a custom view for my Toast Messages. I want my toast
I have created a custom view hierarchy in an XIB that I want to
I have created a custom component which will contain reference to view(bound to a
I have created a custom list view, each row has it's own custom selector,
I have created a custom annotation view. In the setSelected method i have implemented
I have created custom posts and I want one page in my site to
friends, i have created custom title bar using following titlebar.xml file with code <?xml

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.