我正在尝试使用导航抽屉将数据放入幻灯片菜单应用程序的列表视图中。我创建了一个列表视图并定义了适配器,但是当我运行它时,我在logcat中得到了空指针
这是我的代码:
package chnt.appchef.com;
import android.os.Bundle;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.support.v4.widget.DrawerLayout;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class MainActivity extends Activity {
String[] menu;
DrawerLayout dLayout;
ListView dList;
ArrayAdapter<String> adapter;
ArrayAdapter<String> adapter1;
String[] countryArray = {"India", "Pakistan", "USA", "UK"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ArrayAdapter adapter1 = new ArrayAdapter<String>(this,
R.layout.activity_listview, countryArray);
ListView listView = (ListView) findViewById(R.id.list);
listView.setAdapter(adapter);
menu = new String[]{"Home","Android","Windows","Linux","Raspberry Pi","WordPress","Videos","Contact Us"};
dLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
dList = (ListView) findViewById(R.id.left_drawer);
adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,menu);
dList.setAdapter(adapter);
dList.setSelector(android.R.color.holo_blue_dark);
dList.setOnItemClickListener(new OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> arg0, View v, int position, long id) {
dLayout.closeDrawers();
Bundle args = new Bundle();
args.putString("Menu", menu[position]);
Fragment detail = new DetailFragment();
detail.setArguments(args);
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction().replace(R.id.content_frame, detail).commit();
}
});
}
}
这是详细的片段类
package chnt.appchef.com;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class DetailFragment extends Fragment {
TextView text;
@Override
public View onCreateView(LayoutInflater inflater,ViewGroup container, Bundle args) {
View view = inflater.inflate(R.layout.menu_detail_fragment, container, false);
String menu = getArguments().getString("Menu");
text= (TextView) view.findViewById(R.id.detail);
text.setText(menu);
return view;
}
}
我也定义了列表视图
logcat的
12-26 16:12:57.796: E/AndroidRuntime(19145): FATAL EXCEPTION: main
12-26 16:12:57.796: E/AndroidRuntime(19145): java.lang.RuntimeException: Unable to start activity ComponentInfo{chnt.appchef.com/chnt.appchef.com.MainActivity}: java.lang.NullPointerException
12-26 16:12:57.796: E/AndroidRuntime(19145): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2308)
12-26 16:12:57.796: E/AndroidRuntime(19145): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2362)
12-26 16:12:57.796: E/AndroidRuntime(19145): at android.app.ActivityThread.access$700(ActivityThread.java:168)
12-26 16:12:57.796: E/AndroidRuntime(19145): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1329)
12-26 16:12:57.796: E/AndroidRuntime(19145): at android.os.Handler.dispatchMessage(Handler.java:99)
12-26 16:12:57.796: E/AndroidRuntime(19145): at android.os.Looper.loop(Looper.java:137)
12-26 16:12:57.796: E/AndroidRuntime(19145): at android.app.ActivityThread.main(ActivityThread.java:5493)
12-26 16:12:57.796: E/AndroidRuntime(19145): at java.lang.reflect.Method.invokeNative(Native Method)
12-26 16:12:57.796: E/AndroidRuntime(19145): at java.lang.reflect.Method.invoke(Method.java:525)
12-26 16:12:57.796: E/AndroidRuntime(19145): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1209)
12-26 16:12:57.796: E/AndroidRuntime(19145): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1025)
12-26 16:12:57.796: E/AndroidRuntime(19145): at dalvik.system.NativeStart.main(Native Method)
12-26 16:12:57.796: E/AndroidRuntime(19145): Caused by: java.lang.NullPointerException
12-26 16:12:57.796: E/AndroidRuntime(19145): at chnt.appchef.com.MainActivity.onCreate(MainActivity.java:31)
12-26 16:12:57.796: E/AndroidRuntime(19145): at android.app.Activity.performCreate(Activity.java:5372)
12-26 16:12:57.796: E/AndroidRuntime(19145): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1104)
12-26 16:12:57.796: E/AndroidRuntime(19145): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2270)
12-26 16:12:57.796: E/AndroidRuntime(19145): ... 11 more
12-26 16:13:00.066: I/Process(19145): Sending signal. PID: 19145 SIG: 9
答案 0 :(得分:1)
您正在创建并初始化实例adapter1,但是将实例适配器分配给尚未初始化且为null的listview。
所以改变
listView.setAdapter(adapter);
到
listView.setAdapter(adapter1);
答案 1 :(得分:0)
这里是创建adapter1并分配适配器。看看这里:
ArrayAdapter adapter1 = new ArrayAdapter<String>(this,
R.layout.activity_listview, countryArray);
ListView listView = (ListView) findViewById(R.id.list);
listView.setAdapter(adapter);
改变这个:
listView.setAdapter(adapter1);
答案 2 :(得分:0)
将listview.setAdapter(适配器)更改为listview.setAdapter(adapter1)