我该如何修复空指针异常

时间:2018-10-02 01:18:43

标签: android nullpointerexception

我是新开发人员。尝试使用自定义列表视图构建简单的应用。它的打开效果很好,但是当我单击它时,它不再起作用了。添加异常后,我可以看到空指针异常。困惑我该如何解决。代码如下:请帮帮我。 问题已解决。代码已更新如下:

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>

请帮助我

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

截屏-Toast消息中的外观异常 enter image description here

1 个答案:

答案 0 :(得分:1)

我只是更新意图。希望它能工作:)

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();

                }
            }
        });