我的 MainActivity 中有一个列表视图作为侧边栏。我想在 onCreate() 中将列表视图项颜色设置为自定义颜色。所以我正在使用 BaseAdapter 并覆盖它的视图。我正在将 listview 设置为 ArrayAdapter。但是我的应用程序在启动时崩溃了。任何人都可以帮我解决这个问题。
这是我的MainActivity
代码
import java.util.ArrayList;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView listview=(ListView)findViewById(R.id.listView);
ArrayList<String > list = new ArrayList<String>();
list.add("sohail");
list.add("aziz");
list.add("hassan");
CustomAdapter adapter=new CustomAdapter(this,list);
listview.setAdapter(adapter);
listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// Show Alert
}
});
}
@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;
}
}
这是我要覆盖我的视图的基本适配器。
import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import java.util.ArrayList;
/**
* Created by polak_000 on 10/2/13.
*/
public class CustomAdapter extends BaseAdapter{
Context mycontext;
ArrayList<String> contactsList;
LayoutInflater minflater;
public CustomAdapter(Context context, ArrayList<String> list)
{
this.mycontext=context;
contactsList=list;
minflater =LayoutInflater.from(context);
}
@Override
public int getCount()
{
return contactsList.size();
}
@Override
public Object getItem(int position)
{
return contactsList.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position,View convertView,ViewGroup parent)
{
if (convertView !=null)
{
if (position ==1)
{
convertView.setBackgroundColor(Color.BLUE);
}
}
return convertView;
}
}
这是我的 MainActivity 布局。
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<ListView
android:id="@+id/listView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="@android:color/holo_green_light"
android:minHeight="50dp"
/>
</RelativeLayout>
这是我的目录
10-02 14:11:22.125: D/AndroidRuntime(2036): 关闭 VM 10-02 14:11:22.125: W/dalvikvm(2036): threadid=1: 线程退出未捕获异常 (group=0x41465700) 10 -02 14:11:22.215:D/dalvikvm(2036):GC_FOR_ALLOC 释放 80K,5% 释放 2940K/3072K,暂停 34ms,总共 36ms 10-02 14:11:22.225:E/AndroidRuntime(2036):致命例外:主要 10-02 14:11:22.225: E/AndroidRuntime(2036): java.lang.NullPointerException 10-02 14:11:22.225: E/AndroidRuntime(2036): 在 android.widget.AbsListView.obtainView(AbsListView.java :2179) 10-02 14:11:22.225: E/AndroidRuntime(2036): 在 android.widget.ListView.measureHeightOfChildren(ListView.java:1247) 10-02 14:11:22.225: E/AndroidRuntime(2036):在 android.widget.ListView.onMeasure(ListView.java:1159) 10-02 14:11:22.225: E/AndroidRuntime(2036): 在 android.view。View.measure(View.java:15848) 10-02 14:11:22.225: E/AndroidRuntime(2036): 在 android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:728) 10-02 14:11:22.225: E/AndroidRuntime(2036): 在 android.widget.RelativeLayout.onMeasure(RelativeLayout.java:477) 10-02 14:11:22.225: E/AndroidRuntime(2036): 在 android.view.View.measure(View.java :15848) 10-02 14:11:22.225: E/AndroidRuntime(2036): 在 android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5012) 10-02 14:11:22.225: E/AndroidRuntime(2036):在 android.widget.FrameLayout.onMeasure(FrameLayout.java:310) 10-02 14:11:22.225: E/AndroidRuntime(2036): 在 android.view.View.measure(View.java:15848) 10-02 14 :11:22.225: E/AndroidRuntime(2036): 在 android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5012) 10-02 14:11:22。225: E/AndroidRuntime(2036): 在 com.android.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:302) 10-02 14:11:22.225: E/AndroidRuntime(2036): 在 android.view.View .measure(View.java:15848) 10-02 14:11:22.225: E/AndroidRuntime(2036): 在 android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5012) 10-02 14:11:22.225: E /AndroidRuntime(2036): 在 android.widget.FrameLayout.onMeasure(FrameLayout.java:310) 10-02 14:11:22.225: E/AndroidRuntime(2036): 在 com.android.internal.policy.impl.PhoneWindow$ DecorView.onMeasure(PhoneWindow.java:2189) 10-02 14:11:22.225: E/AndroidRuntime(2036): 在 android.view.View.measure(View.java:15848) 10-02 14:11:22.225: E/AndroidRuntime(2036): 在 android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1905) 10-02 14:11:22.225:E/AndroidRuntime(2036): 在 android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1104) 10-02 14:11:22.225: E/AndroidRuntime(2036): 在 android.view.ViewRootImpl.performTraversals(ViewRootImpl.java :1284) 10-02 14:11:22.225: E/AndroidRuntime(2036): 在 android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1004) 10-02 14:11:22.225: E/AndroidRuntime(2036):在 android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5481) 10-02 14:11:22.225: E/AndroidRuntime(2036): 在 android.view.Choreographer$CallbackRecord.run(Choreographer.java:749) 10-02 14:11:22.225: E/AndroidRuntime(2036): 在 android.view.Choreographer.doCallbacks(Choreographer.java:562) 10-02 14:11:22.225: E/AndroidRuntime(2036): 在 android. view.Choreographer.doFrame(Choreographer.java:532) 10-02 14:11:22.225:E/AndroidRuntime(2036): 在 android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735) 10-02 14:11:22.225: E/AndroidRuntime(2036): 在 android.os.Handler.handleCallback(Handler .java:730) 10-02 14:11:22.225: E/AndroidRuntime(2036): 在 android.os.Handler.dispatchMessage(Handler.java:92) 10-02 14:11:22.225: E/AndroidRuntime(2036) ): 在 android.os.Looper.loop(Looper.java:137) 10-02 14:11:22.225: E/AndroidRuntime(2036): 在 android.app.ActivityThread.main(ActivityThread.java:5103) 10- 02 14:11:22.225: E/AndroidRuntime(2036): 在 java.lang.reflect.Method.invokeNative(Native Method) 10-02 14:11:22.225: E/AndroidRuntime(2036): 在 java.lang.reflect .Method.invoke(Method.java:525) 10-02 14:11:22.225: E/AndroidRuntime(2036): 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller。运行(ZygoteInit.java:737)10-02 14:11:22.225:E/AndroidRuntime(2036):在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)10-02 14:11: 22.225:E/AndroidRuntime(2036):在 dalvik.system.NativeStart.main(本机方法)