0

I'm a new in programming,

I want to search in a TextView. I have this code for searching in my TextView. I am using a custom toolbar

package com.example.mohsin.customlistview;

import android.graphics.Typeface;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.ParcelableSpan;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.style.StyleSpan;
import android.view.View;
import android.widget.EditText;
import android.widget.ScrollView;
import android.widget.TextView;
import android.support.v7.widget.Toolbar;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;

public class loi52055 extends AppCompatActivity {


        TextView textloi2;
        ScrollView textViewWrapper;
        EditText texttoolbar;
        String target;
        int selected;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_loi52055);

            Toolbar toolbar = findViewById(R.id.toolbark);
            setSupportActionBar(toolbar);

            textloi2 = findViewById(R.id.textloi52055);
            textViewWrapper =  findViewById(R.id.textViewWrapper);

            // textloi.setMovementMethod(new ScrollingMovementMethod());

            String data = "";

            StringBuffer dahirbuffer = new StringBuffer();

            InputStream is = this.getResources().openRawResource(R.raw.loi5205);

            BufferedReader reader = new BufferedReader(new InputStreamReader(is));



            if(is != null){
                try {
                    while((data=reader.readLine())!= null){
                        dahirbuffer.append(data + "\n");
                    }
                    textloi2.setText(dahirbuffer);
                    is.close();

                }catch(Exception e){
                    e.printStackTrace();
                }
            }
            textloi2.setMovementMethod(new CustomMovementMethod());

            textloi2.setTextIsSelectable(true);
            texttoolbar = findViewById(R.id.edittexttoolbar1);
            target = texttoolbar.getText().toString();
            selected = changeTextView(textloi2, target, selected);
            findViewById(R.id.next).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    selected++;
                }
            });
            findViewById(R.id.previous).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    selected--;
                }
            });





        }


    private int changeTextView(TextView tv, String target, int selected) {
        if (selected < 0) {
            selected = 0;
        }
        String bString = tv.getText().toString();
        int startSpan = 0, endSpan = 0;
        Spannable spanRange = new SpannableString(bString);

        int currentIndex = 0;

        while (true) {
            startSpan = bString.indexOf(target, endSpan);
            endSpan = startSpan + target.length();

            boolean isLast = bString.indexOf(target, endSpan) < 0;
            if (startSpan < 0)
                break;

            ParcelableSpan span;
            if (currentIndex == selected || isLast && selected > currentIndex) {
                // selected span
                span = new StyleSpan(Typeface.BOLD_ITALIC);
                if (isLast && selected > currentIndex) {
                    // normalize if selected out of bounds
                    selected = currentIndex;
                }
            } else {
                // deselected span
                span = new StyleSpan(Typeface.BOLD);
            }

            currentIndex++;
            spanRange.setSpan(
                    span,
                    startSpan,
                    endSpan,
                    Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
        tv.setText(spanRange);

        if (currentIndex == 0) {
            // text not found
            return -1;
        } else {
            return selected;
        }
    }

}

But when I run it I have this :

java.lang.NullPointerException: Attempt to invoke virtual method'android.text.Editable android.widget.EditText.getText()' on a null object reference

layout :

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".loi52055"
    >


    <ScrollView
        android:id="@+id/scrollview1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        >

        <TextView
            android:id="@+id/textloi52055"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="4dp"
            android:focusable="true"
            android:longClickable="true"
            android:scrollbars="vertical"
            android:textColor="#000000"
            android:textIsSelectable="true" />
    </ScrollView>

</RelativeLayout>
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115

1 Answers1

0

Your problem is not with the TextView but with the EditText

texttoolbar = findViewById(R.id.edittexttoolbar1);

You have to add an EditText with id edittexttoolbar1 in your activity_loi52055 layout or include the layout inside the one that contains edittexttoolbar1

bMain
  • 324
  • 3
  • 11
  • But the edittext is in the toolbar – kheir-eddine elhain Jan 13 '20 at 17:15
  • In that case, you find the toolbar first (`Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);`) then you get the edit text (`toolbar.findViewById(R.id.edittexttoolbar1);` You have to use the ID of your toolbar, if you have a specific one – bMain Jan 14 '20 at 14:53