我正在尝试构建一个简单的导航抽屉,但我不断收到这个恼人的错误。我对Android很新,所以我真的不知道如何修复/意味着什么。任何帮助表示赞赏。
爪哇:
package com.google.helpstl2;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Toast;
import java.util.List;
public class Home extends ActionBarActivity implements OnItemClickListener {
private DrawerLayout drawerLayout;
private ListView listView;
private String[] planets;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
drawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);
planets = getResources().getStringArray(R.array.planets);
listView = (ListView) findViewById(R.id.drawerList);
listView.setAdapter(new ArrayAdapter<>(this, android.R.layout.simple_expandable_list_item_1, planets));
listView.setOnItemClickListener(this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_home, 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
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(this,planets[position],Toast.LENGTH_LONG).show();
}
}
相关XML?
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawerLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- The main content view -->
<FrameLayout
android:id="@+id/mainContent"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<ListView android:id="@+id/drawerList"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="left"
/>
</android.support.v4.widget.DrawerLayout>
Logcat错误输出是:
Process: com.google.helpstl2, PID: 2205 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.google.helpstl2/com.google.helpstl2.Home}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference
答案 0 :(得分:0)
可能是包含内容的 不是 一次检查这件事。 您可以在错误行添加空检查:<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawerLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- The main content view -->
<FrameLayout
android:id="@+id/mainContent"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<ListView android:id="@+id/drawerList"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="left"
/>
</android.support.v4.widget.DrawerLayout>
activity_main.xml
,因此您在找到ID时会获得NPE:drawerList
if(listView != null ) {
listView.setAdapter(new ArrayAdapter<>(this, android.R.layout.simple_expandable_list_item_1, planets));
listView.setOnItemClickListener(this);
}