1

i am learning on TextSuggestion leson with AndroidStudio. Here's my code :

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context="com.zihadrizkyef.belajar_textsuggestion.MainActivity"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="16dp">

    <EditText
        android:id="@+id/etText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="30dp"
        android:hint="Enter Text"/>

    <TextView
        android:id="@+id/tvSuggestion"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:layout_marginBottom="5dp"
        android:hint="Suggestion"/>

    <Button
        android:id="@+id/btSuggestion"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Suggestion"/>

</LinearLayout>

MainActivity.java

package com.zihadrizkyef.belajar_textsuggestion;

import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.textservice.SentenceSuggestionsInfo;
import android.view.textservice.SpellCheckerSession;
import android.view.textservice.SuggestionsInfo;
import android.view.textservice.TextInfo;
import android.view.textservice.TextServicesManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements SpellCheckerSession.SpellCheckerSessionListener {
    EditText etText;
    TextView tvSuggestion;
    Button btSuggestion;
    SpellCheckerSession scs;

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

        TextServicesManager tsm = (TextServicesManager)getSystemService(TEXT_SERVICES_MANAGER_SERVICE);
        scs = tsm.newSpellCheckerSession(null, null, this, true);

        etText = (EditText)findViewById(R.id.etText);
        tvSuggestion = (TextView)findViewById(R.id.tvSuggestion);
        btSuggestion = (Button)findViewById(R.id.btSuggestion);

        btSuggestion.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                TextInfo[] ti = new TextInfo[1];
                ti[0] = new TextInfo(etText.getText().toString());

                scs.getSentenceSuggestions(ti, 3);
            }
        });
    }

    @Override
    public void onGetSuggestions(SuggestionsInfo[] results) {

    }

    @Override
    public void onGetSentenceSuggestions(SentenceSuggestionsInfo[] results) {
        System.out.println(results.length);
    }
}

I tried to put "A" on edit text then clicked the suggestion button. But it always throws null pointer exception. I've try search about it on google and here but not found any article talking about this problem

---EDIT---

Of course my post different with this question. Mine asking about SpellCheckerSession.getSentenceSuggestion that throws null, and this question asking about "what is null exception pointer".

Community
  • 1
  • 1
zihadrizkyef
  • 1,849
  • 3
  • 23
  • 46

0 Answers0