以下是MainActivity.java
之后的onCreate
,仅包含几个if/else
块:
// Simple array with a list of languages
String[] languages = {"Arabic", "Armenian", "Cantonese", "Cherokee",
"Czech", "Dutch", "English", "French", "Gaelic", "German",
"Greek", "Haitian Creole", "Hawaiian", "Italian", "Japanese", "Korean",
"Mandarin", "Navajo", "Philippines Tagalog", "Polish", "Portuguese", "Romanian",
"Russian", "Spanish", "Swedish", "Vietnamese"};
// Simple array with a list of the spelling for Merry Christmas in language
final String[] merrychristmas = {"Eid Melad Majeed", "Shnorhavor Surb Tsnund",
"Sing-daan Faai-lohk", "ulihelisdi hunadetiyisgvi", "veselé vánoce",
"vrolijk kerstfeest", "Merry Christmas!", "joyeux Noël", "Nollaig Shona Duit",
"frohe Weihnacten", "Kalá Christoúgenna", "Jwaye Nwèl", "Mele Kalikimaka",
"Buon Natale", "Merīkurisumasu", "jilgohun-christmas-bonoseyo",
"Shèndàn kuàilè", "Nizhónígo Késhmish Da'doohleel",
"Mąligayang Pasko", "Wesołych Świąt", "Feliz Natal", "Crăcuin fericit",
"schastlivogo Rozhdestva", "Feliz Navidad", "god jul", "Chúc Mừng Giáng Sinh"};
CustomAdapter adapter = new CustomAdapter();
listView1 = (ListView) findViewById(R.id.listView1);
View header = (View) getLayoutInflater().inflate(R.layout.listview_header_row, null);
listView1.addHeaderView(header);
listView1.setAdapter(adapter);
listView1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
String selectedLanguage = (String) listView1.getItemAtPosition(position);
LinearLayout layout = new LinearLayout(MainActivity.this);
layout.setBackgroundResource(R.drawable.circle);
TextView tv = new TextView(MainActivity.this);
// set the TextView properties
tv.setTextColor(Color.WHITE);
tv.setTextSize(20);
tv.setPaddingRelative(22,108,0,0);
tv.setGravity(Gravity.FILL);
tv.setText(merrychristmas[position]);
layout.addView(tv);
Toast toast = new Toast(MainActivity.this);
if (position == 0) {
Toast.makeText(getApplicationContext(),
merrychristmas[position], LENGTH_LONG);
// set the layout as the toast view
toast.setView(layout);
toast.setGravity(Gravity.CENTER_HORIZONTAL, 0, 0);
toast.show();
_player = MediaPlayer.create(MainActivity.this, R.raw.arabic);
_player.start();
} else if (position == 1) {
Toast.makeText(getApplicationContext(),
merrychristmas[position], LENGTH_LONG);
// set the layout as the toast view
toast.setView(layout);
toast.setGravity(Gravity.CENTER_HORIZONTAL, 0, 0);
toast.show();
_player = MediaPlayer.create(MainActivity.this, R.raw.armenian);
_player.start();
这是我尝试实现的自定义适配器,用于更改listview
的文本颜色(使用黑色文本可以正常工作!)
public class CustomAdapter extends BaseAdapter {
public String rowText;
Context context;
int layoutResourceId;
// Simple array with a list of languages
String[] languages = {"Arabic", "Armenian", "Cantonese", "Cherokee",
"Czech", "Dutch", "English", "French", "Gaelic", "German",
"Greek", "Haitian Creole", "Hawaiian", "Italian", "Japanese", "Korean",
"Mandarin", "Navajo", "Philippines Tagalog", "Polish", "Portuguese", "Romanian",
"Russian", "Spanish", "Swedish", "Vietnamese"};
LayoutInflater mInflater = ((Activity)context).getLayoutInflater();
@Override
public int getCount() {
return languages.length;
}
@Override
public Object getItem(int position) {
return languages[position];
}
@Override
public long getItemId(int position) {
return(position);
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.listview_item_row, null);
holder = new ViewHolder();
holder.rowText = (TextView) convertView.findViewById(R.id.textView1);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.rowText.setText(languages[position]);
holder.rowText.setBackgroundResource(R.color.xmas_green);
return convertView;
}
public class ViewHolder {
TextView rowText;
}
}
这是activity_main.xml
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/listView1"
android:divider="@android:color/white"
android:dividerHeight="1dp">
</ListView>
<View android:layout_width="50dp"
android:layout_height="50dp"
android:background="@drawable/circle" />
</LinearLayout>
这是标题行布局:
<TextView
android:id="@+id/txtHeader"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:textSize="22sp"
android:padding="10dp"
android:textStyle="bold"
android:textColor="@color/white"
android:background="@color/xmas_green"
android:text="Click on a language to listen"
android:focusable="false"
android:focusableInTouchMode="false"/>
这是项目行布局:
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_vertical"
android:textSize="18sp"
android:textColor="@color/white"
android:id="@+id/txtLanguage"/>
</LinearLayout>
这是mylist_layout.xml
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/textView1"
android:textColor="@color/white"
android:textSize="20sp"
android:paddingTop="10dp"
android:paddingBottom="10dp"/>
</LinearLayout>
最后,这是toast_view.xml:`
<View android:layout_width="match_parent"
android:layout_height="fill_parent"
android:background="@drawable/circle" >
</View>
</LinearLayout>
我完成了项目&gt;清洁然后重建多次。我没有触摸清单文件。 (看,我一直在研究这个可爱的信息保险库!)在此先感谢,我很感激你的帮助!
这是LogCat输出....谢谢!
10-04 15:00:02.780 865-865/com.blogspot.joyouslybeingjoy.merryxmasindifflangs
D/dalvikvm﹕ Not late-enabling CheckJNI (already on)
10-04 15:00:06.230 865-865/com.blogspot.joyouslybeingjoy.merryxmasindifflangs
D/dalvikvm﹕ GC_FOR_ALLOC freed 48K, 5% free 2949K/3076K, paused 47ms, total 49ms
10-04 15:00:06.230 865-865/com.blogspot.joyouslybeingjoy.merryxmasindifflangs
I/dalvikvm-heap﹕ Grow heap (frag case) to 3.559MB for 635812-byte allocation
10-04 15:00:06.340 865-865/com.blogspot.joyouslybeingjoy.merryxmasindifflangs
D/dalvikvm﹕ GC_FOR_ALLOC freed 0K, 4% free 3570K/3700K, paused 39ms, total 39ms
10-04 15:00:06.410 865-865/com.blogspot.joyouslybeingjoy.merryxmasindifflangs
D/AndroidRuntime﹕ Shutting down VM
10-04 15:00:06.410 865-865/com.blogspot.joyouslybeingjoy.merryxmasindifflangs
W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0xb3ababa8)
10-04 15:00:06.430 865-865/com.blogspot.joyouslybeingjoy.merryxmasindifflangs
E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.blogspot.joyouslybeingjoy.merryxmasindifflangs, PID: 865
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.blogspot.
joyouslybeingjoy.
merryxmasindifflangs/com.blogspot.joyouslybeingjoy.merryxmasindifflangs.MainActivity}:
java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.blogspot.joyouslybeingjoy.merryxmasindifflangs.CustomAdapter.<init>
(CustomAdapter.java:27)
at com.blogspot.joyouslybeingjoy.merryxmasindifflangs.MainActivity.onCreate
(MainActivity.java:81)
at android.app.Activity.performCreate(Activity.java:5231)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)
10-04 15:00:09.430 865-865/com.blogspot.joyouslybeingjoy.merryxmasindifflangs
I/Process﹕
Sending signal. PID: 865 SIG: 9
答案 0 :(得分:0)
您的问题位于CustomAdapter.java的第27行 让我假设 - 第27行是:
LayoutInflater mInflater = ((Activity)context).getLayoutInflater();
因此(Activity)context
在实例初始化过程中null
会让您遇到问题。
的修改
将初始化移动到构造函数。如果需要 - 在那里传递上下文参数