0

I'm new developer. Trying to build simple app using custom list view. And its open pretty well but when I click its not working anymore. After I add exception, I can see that Null Pointer Exception. Confused how do i fix it. Code are below: Please help me out. Problem Solved. Code Has Been Updated Below:

public class MainActivity extends AppCompatActivity {
private static final String TAG = "MainActivity";

private ListView mListView;
private Toolbar toolbarTop;
private TextView mTitle;
private String[] mCountryNames;
private int[] flags = {
        R.drawable.flag_afghanistan,
        R.drawable.flag_armenia,
        R.drawable.flag_australia,
        R.drawable.flag_azerbaijan,
        R.drawable.flag_bahrain,
        R.drawable.flag_bangladesh,
        R.drawable.flag_bhutan,
        R.drawable.flag_china,
        R.drawable.flag_hongkong,
        R.drawable.flag_india,
        R.drawable.flag_malaysia,
        R.drawable.flag_nepal,
        R.drawable.flag_pakistan,
        R.drawable.flag_philippines,
        R.drawable.flag_singapore,
        R.drawable.flag_thailand,
        R.drawable.flag_unitedstates
};

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Log.d(TAG, "onCreate: started");

    toolbarTop = findViewById(R.id.toolBarId);

    mTitle = toolbarTop.findViewById(R.id.toolbar_title);

    mListView = findViewById(R.id.listViewId);

    mCountryNames = getResources().getStringArray(R.array.country_names);

    CustomAdapter mAdapter = new CustomAdapter(this,mCountryNames,flags);

    mListView.setAdapter(mAdapter);

    mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            try{
                Log.d("Click", "onItemClick: Working ");
                Intent intent = new Intent(MainActivity.this,SecondActivity.class);
              intent.putExtra("CountryNames", mCountryNames[position].toString());
                startActivity(intent);
            }catch (Exception e){
                Toast.makeText(getApplicationContext(),"Something Went Wrong: "+" "+e,Toast.LENGTH_SHORT).show();

            }
        }
    });

Activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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=".MainActivity">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolBarId"
        android:gravity="center"
        android:layout_gravity="center"
        android:background="@color/colorPrimary"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <TextView
            android:text="Country Names"
            android:textSize="22sp"
            android:textColor="#fff"
            android:textStyle="bold"
            android:layout_gravity="center"
            android:id="@+id/toolbar_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

    </android.support.v7.widget.Toolbar>

    <ListView
        android:id="@+id/listViewId"
        android:listSelector="#faf5f7"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    </ListView>

</LinearLayout>

row_view.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:padding="10dp"
    android:layout_marginTop="15dp"
    tools:context=".MainActivity">

    <LinearLayout
        android:padding="5dp"
        android:background="@drawable/background"
        android:weightSum="4"
        android:layout_marginTop="5dp"
        android:layout_marginBottom="5dp"
        android:layout_width="match_parent"
        android:layout_height="65dp">

        <ImageView
            android:id="@+id/imageViewId"
            android:background="#bdc3c7"
            android:src="@drawable/flag_bangladesh"
            android:layout_weight="1"
            android:layout_width="0dp"
            android:layout_height="wrap_content" />

        <LinearLayout
            android:layout_weight="3"
            android:layout_width="0dp"
            android:layout_height="match_parent">

            <TextView
                android:id="@+id/textViewId"
                android:background="#7f8c8d"
                android:text="Hello World"
                android:textColor="#fff"
                android:textAppearance="@style/Base.TextAppearance.AppCompat.Large"
                android:gravity="center"
                android:textStyle="bold"
                android:layout_weight="2"
                android:layout_width="0dp"
                android:layout_height="match_parent" />

            <LinearLayout
                android:background="#95a5a6"
                android:layout_weight="0.5"
                android:layout_width="0dp"
                android:layout_height="match_parent">

                <ImageView
                    android:src="@drawable/arrow"
                    android:layout_gravity="center"
                    android:layout_weight="0.5"
                    android:layout_width="0dp"
                    android:layout_height="32dp" />

            </LinearLayout>

        </LinearLayout>

    </LinearLayout>

</LinearLayout>

Please help me out

Logcat

    --------- beginning of system
2018-10-02 07:55:31.899 30580-2768/? I/ActivityManager: Killing 3578:com.google.android.gms:car/u0a12 (adj 906): empty #17
2018-10-02 07:55:33.056 30580-30596/? E/memtrack: Couldn't load memtrack module
2018-10-02 07:55:33.057 30580-30596/? W/android.os.Debug: failed to get memory consumption info: -1
2018-10-02 07:55:34.439 30580-30596/? E/memtrack: Couldn't load memtrack module
2018-10-02 07:55:34.439 30580-30596/? W/android.os.Debug: failed to get memory consumption info: -1
2018-10-02 07:55:34.460 30580-30596/? E/memtrack: Couldn't load memtrack module
2018-10-02 07:55:34.460 30580-30596/? W/android.os.Debug: failed to get memory consumption info: -1

Screenshot - Look Exception in Toast Message enter image description here

  • Please add the logcat and your XML layout to your question – OneCricketeer Oct 02 '18 at 01:20
  • Please add your logcat or which line that it said null pointer exceptionn? – Anonymous-E Oct 02 '18 at 01:36
  • Hi @SopheakSok, Thanks for the reply. Yes I'll add –  Oct 02 '18 at 01:47
  • 2
    Try to change this from `intent.putExtra("CountryNames", mListView.getItemAtPosition(position).toString())` to `intent.putExtra("CountryNames", mCountryNames[position].toString())`, get directly from the array? – SpringMaple Oct 02 '18 at 02:03
  • The lines you posted from your logcat here have nothing to do with this error. Find the correct lines and they will tell you which line threw the error. – Code-Apprentice Oct 02 '18 at 02:05
  • Thanks to all mates who reply to my question. And I found the problem. Thanks to @SpringMaple first and secondly **bQuiet**. Code is working now. Love you guys :) –  Oct 02 '18 at 02:13
  • 1
    @booleantrue Check out https://stackoverflow.com/questions/3965654/getitematposition-not-returning-value-in-listview too, I believe it helps in explaining why your `mListView.getItemAtPosition` isn't working. – SpringMaple Oct 02 '18 at 02:15

1 Answers1

0

I just update the intent. Hope it works :)

intent.putExtra("CountryNames", mCountryNames[position].toString());

public class MainActivity extends AppCompatActivity {
    private static final String TAG = "MainActivity";

    private ListView mListView;
    private Toolbar toolbarTop;
    private TextView mTitle;
    private String[] mCountryNames;
    private int[] flags = {
            R.drawable.flag_afghanistan,
            R.drawable.flag_armenia,
            R.drawable.flag_australia,
            R.drawable.flag_azerbaijan,
            R.drawable.flag_bahrain,
            R.drawable.flag_bangladesh,
            R.drawable.flag_bhutan,
            R.drawable.flag_china,
            R.drawable.flag_hongkong,
            R.drawable.flag_india,
            R.drawable.flag_malaysia,
            R.drawable.flag_nepal,
            R.drawable.flag_pakistan,
            R.drawable.flag_philippines,
            R.drawable.flag_singapore,
            R.drawable.flag_thailand,
            R.drawable.flag_unitedstates
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Log.d(TAG, "onCreate: started");

        toolbarTop = findViewById(R.id.toolBarId);

        mTitle = toolbarTop.findViewById(R.id.toolbar_title);

        mListView = findViewById(R.id.listViewId);

        mCountryNames = getResources().getStringArray(R.array.country_names);

        CustomAdapter mAdapter = new CustomAdapter(this,mCountryNames,flags);

        mListView.setAdapter(mAdapter);

        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                try{
                    Log.d("Click", "onItemClick: Working ");
                    Intent intent = new Intent(MainActivity.this,SecondActivity.class);
                  intent.putExtra("CountryNames", mCountryNames[position].toString());
                    startActivity(intent);
                }catch (Exception e){
                    Toast.makeText(getApplicationContext(),"Something Went Wrong: "+" "+e,Toast.LENGTH_SHORT).show();

                }
            }
        });