我試圖做一個簡單的應用程序,但我被困在這個討厭的錯誤,我無法找出原因...Android的 - Java的NullPointerException異常
01-06 18:05:04.734: E/AndroidRuntime(1102): FATAL EXCEPTION: main
01-06 18:05:04.734: E/AndroidRuntime(1102): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.coindroid/com.example.coindroid.EUActivity}: java.lang.NullPointerException
01-06 18:05:04.734: E/AndroidRuntime(1102): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
01-06 18:05:04.734: E/AndroidRuntime(1102): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
01-06 18:05:04.734: E/AndroidRuntime(1102): at android.app.ActivityThread.access$600(ActivityThread.java:141)
01-06 18:05:04.734: E/AndroidRuntime(1102): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
01-06 18:05:04.734: E/AndroidRuntime(1102): at android.os.Handler.dispatchMessage(Handler.java:99)
01-06 18:05:04.734: E/AndroidRuntime(1102): at android.os.Looper.loop(Looper.java:137)
01-06 18:05:04.734: E/AndroidRuntime(1102): at android.app.ActivityThread.main(ActivityThread.java:5041)
01-06 18:05:04.734: E/AndroidRuntime(1102): at java.lang.reflect.Method.invokeNative(Native Method)
01-06 18:05:04.734: E/AndroidRuntime(1102): at java.lang.reflect.Method.invoke(Method.java:511)
01-06 18:05:04.734: E/AndroidRuntime(1102): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
01-06 18:05:04.734: E/AndroidRuntime(1102): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
01-06 18:05:04.734: E/AndroidRuntime(1102): at dalvik.system.NativeStart.main(Native Method)
01-06 18:05:04.734: E/AndroidRuntime(1102): Caused by: java.lang.NullPointerException
01-06 18:05:04.734: E/AndroidRuntime(1102): at com.example.coindroid.EUActivity.onCreate(EUActivity.java:30)
01-06 18:05:04.734: E/AndroidRuntime(1102): at android.app.Activity.performCreate(Activity.java:5104)
01-06 18:05:04.734: E/AndroidRuntime(1102): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
01-06 18:05:04.734: E/AndroidRuntime(1102): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
01-06 18:05:04.734: E/AndroidRuntime(1102): ... 11 more
是這樣,它似乎我的問題是在第30行,這是這樣的:list.setAdapter(new EUAdapter(this)); 有什麼不對? 我的活動僅僅是這樣的:
package com.example.coindroid;
import java.util.ArrayList;
import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
public class EUActivity extends Activity {
ListView list;
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);
setContentView(R.layout.activity_eu);
list = (ListView) findViewById(R.layout.activity_eu);
list.setAdapter(new EUAdapter(this));
}
}
class SingleRow { String title; String description; int image; SingleRow(String title, String description, int image) { this.title = title; this.description = description; this.image = image; } } class EUAdapter extends BaseAdapter{
ArrayList<SingleRow> list; Context context; EUAdapter(Context c) { context = c; list = new ArrayList<SingleRow>(); Resources res = c.getResources(); String[] titles = res.getStringArray(R.array.titles); String[] descriptions = res.getStringArray(R.array.description); int[] images = {R.drawable.queenvictoria1841jpg};
for(int i=0;i<1;i++) { list.add(new SingleRow(titles[i], descriptions[i], images[i])); }
}
@Override public int getCount() {
return list.size(); }
@Override public Object getItem(int i) {
return list.get(i); }
@Override public long getItemId(int i) {
return i; }
@Override public View getView(int i, View view, ViewGroup viewGroup) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); View row
= inflater.inflate(R.layout.single_row, viewGroup, false);
TextView title = (TextView)row.findViewById(R.id.name); TextView description = (TextView)row.findViewById(R.id.year); ImageView image = (ImageView)row.findViewById(R.id.imageView1);
SingleRow temp = list.get(i);
title.setText(temp.title); description.setText(temp.description); image.setImageResource(temp.image);
return row; }
}
提前謝謝你們!
歡迎使用堆棧溢出。要發佈代碼示例,請使用編輯器菜單中的「{}」按鈕,而不是用於創建引號的'''',它們都會創建灰色背景,但只有'{}'會保留行分隔符,並且會添加正確的格式。格式化的問題有更高的回答機會(如果他們發現作者沒有足夠的努力來創建它們,有些人傾向於跳過問題) – Pshemo
我想如果你想得到某人的幫助,你將不得不修復格式 –
什麼是行no:30 in EUActivity.java ??? – Lal