Turkcell | Geleceği Yazanlar

Soru & Cevap

ListView Uygulama Durduruldu

12.06.2019 - 22:20

//Telefona kabloyla bağlıyken logdan aldığım hata
2019-06-12 22:08:03.314 27611-27611/com.example.nideokuluk E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.nideokuluk, PID: 27611
    java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference
        at com.example.nideokuluk.MainActivity.onNavigationItemSelected(MainActivity.java:110)
        at android.support.design.widget.NavigationView$1.onMenuItemSelected(NavigationView.java:170)
        at android.support.v7.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:840)
        at android.support.v7.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:158)
        at android.support.v7.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:991)
        at android.support.design.internal.NavigationMenuPresenter$1.onClick(NavigationMenuPresenter.java:352)
        at android.view.View.performClick(View.java:5609)
        at android.view.View$PerformClick.run(View.java:22287)
        at android.os.Handler.handleCallback(Handler.java:751)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:154)
        at android.app.ActivityThread.main(ActivityThread.java:6111)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)

//kod
package com.example.nideokuluk;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class MainActivity extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener {

    String[] kucukler_sporcu_isimleri={"a","b","c"};
    String[] yildizlar_sporcu_isimleri={"a","b","c"};
    String[] kadetler_sporcu_isimleri={"a","b","c"};
    String[] gencler_sporcu_isimleri={"a","b","c"};
    String[] buyukler_sporcu_isimleri={"a","b","c"};
    ListView kucukler_liste;
    ListView yildizlar_liste;
    ListView kadetler_liste;
    ListView gencler_liste;
    ListView buyukler_liste;


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

        //kucukler_liste=(ListView) findViewById(R.id.kucukler_sporcu);

        //ArrayAdapter<String> kucukler_adapter=new ArrayAdapter<String>
        // (this, android.R.layout.simple_list_item_1, android.R.id.text1, kucukler_sporcu_isimleri);

        //kucukler_liste.setAdapter(kucukler_adapter);

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

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.addDrawerListener(toggle);
        toggle.syncState();

        NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);
    }

    @Override
    public void onBackPressed() {
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        if (drawer.isDrawerOpen(GravityCompat.START)) {
            drawer.closeDrawer(GravityCompat.START);
        } else {
            super.onBackPressed();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement

        return super.onOptionsItemSelected(item);
    }

    @SuppressWarnings("StatementWithEmptyBody")
    @Override
    public boolean onNavigationItemSelected(MenuItem item) {
        // Handle navigation view item clicks here.
        int id = item.getItemId();

        if (id == R.id.nav_kucuk) {
            Intent intent = new Intent(this,kucukler.class);
            startActivity(intent);

            kucukler_liste=(ListView) findViewById(R.id.kucukler_sporcu);

            ArrayAdapter<String> kucukler_adapter=new ArrayAdapter<String>
                    (this, android.R.layout.simple_list_item_1, android.R.id.text1, kucukler_sporcu_isimleri);

            kucukler_liste.setAdapter(kucukler_adapter);//Sorunlu!!!
        } else if (id == R.id.nav_yildiz) {
            Intent intent = new Intent(this,yildizlar.class);
            startActivity(intent);

            yildizlar_liste=(ListView) findViewById(R.id.yildizlar_sporcu);

            ArrayAdapter<String> yildizlar_adapter=new ArrayAdapter<String>
                    (this, android.R.layout.simple_list_item_1, android.R.id.text1, yildizlar_sporcu_isimleri);

            yildizlar_liste.setAdapter(yildizlar_adapter);//Sorunlu!!!

        } else if (id == R.id.nav_kadet) {
            Intent intent = new Intent(this,kadetler.class);
            startActivity(intent);

            kadetler_liste=(ListView) findViewById(R.id.kadetler_sporcu);

            ArrayAdapter<String> kadetler_adapter=new ArrayAdapter<String>
                    (this, android.R.layout.simple_list_item_1, android.R.id.text1, kadetler_sporcu_isimleri);

            kadetler_liste.setAdapter(kadetler_adapter);//Sorunlu!!!

        } else if (id == R.id.nav_genc) {
            Intent intent = new Intent(this,gencler.class);
            startActivity(intent);

            gencler_liste=(ListView) findViewById(R.id.gencler_sporcu);

            ArrayAdapter<String> gencler_adapter=new ArrayAdapter<String>
                    (this, android.R.layout.simple_list_item_1, android.R.id.text1, gencler_sporcu_isimleri);

            gencler_liste.setAdapter(gencler_adapter);//Sorunlu!!!

        } else if (id == R.id.nav_buyuk) {
            Intent intent = new Intent(this,buyukler.class);
            startActivity(intent);

            buyukler_liste=(ListView) findViewById(R.id.buyukler_sporcu);

            ArrayAdapter<String> buyukler_adapter=new ArrayAdapter<String>
                    (this, android.R.layout.simple_list_item_1, android.R.id.text1, buyukler_sporcu_isimleri);

            buyukler_liste.setAdapter(buyukler_adapter);//Sorunlu!!!

        }

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        drawer.closeDrawer(GravityCompat.START);
        return true;
    }
}

Android Studio'da ListView'e setAdapter ettiğimde "Uygulama Durduruldu" hatası alıyorum eğer setAdpter bölümünü yorum yaparsam sayfaya giriyor ama listeyi set etmiyor normal olarak.

733 Görüntülenme

1 Cevap

Sait ORHAN
23.07.2019 - 10:06

110. satırda item değişkeni null değer döndüğünden dolayı uygulama çöküyor görünüyor.

item null ise metoddan çıkacak şekilde düzenlerseniz sorun çözülür sanırım.

  • Filtered HTML

    Plain text

    Basic HTML

  • Dikey Sekmeler