어디서 안드로이드 응용 프로그램을 만들었는지, 시작 화면 다음에 메뉴를보고 싶습니다. 올바르게 트리거하고 ArrayAdapter
을 사용하여 사용자 지정 ListView
을 만듭니다. 하지만 모듈을 실행하면 스플래시가 끝난 후 닫히고 logcat은 NullPointerException
을 나타냅니다. 그러나 내가 아는 한 잘못한 것은 없습니다. 제발 도와주세요! :)findViewById에서보기를 사용할 때 NullPointerException이 발생했습니다.
감사합니다.
코드 :
package com.sliit.droidman.main;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import com.sliit.droidman.R;
public class MainActivity extends Activity implements OnItemClickListener{
ArrayList<String> Methods = new ArrayList<String>();
ArrayList<String> MethodDiscription = new ArrayList<String>();
ArrayList<Integer> Icons = new ArrayList<Integer>();
ArrayList<Object> Objects = new ArrayList<Object>();
private ListItemAdapter adapter;
ListView lv;
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
Toast.makeText(
this,
"Title => " + Methods.get(position) + "=> n Description"
+ MethodDiscription.get(position), Toast.LENGTH_SHORT).show();
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_menu);
lv = (ListView) findViewById(R.layout.main_menu);
fillArrayList();
lv.setOnItemClickListener(this);
}
public void RefreshListView() {
Objects.clear();
for (int i = 0; i < Methods.size(); i++) {
Object obj = new Object();
Objects.add(obj);
}
Log.d("object array", "" + Objects.size());
adapter = new ListItemAdapter(Objects, 1);
lv.setAdapter(adapter);
}
private class ListItemAdapter extends ArrayAdapter<Object>{
ViewHolder HolderView;
public ListItemAdapter(List<Object> Item, int ID) {
super(MainActivity.this, R.layout.listitem_row , Item);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflator = getLayoutInflater();
convertView = inflator.inflate(R.layout.listitem_row, null);
HolderView = new ViewHolder();
HolderView.AppEvent = (TextView) convertView.findViewById(R.id.Event);
HolderView.EventDisc = (TextView) convertView.findViewById(R.id.EventDescription);
HolderView.iv = (ImageView) convertView.findViewById(R.id.avatar);
convertView.setTag(HolderView);
HolderView.AppEvent.setText(Methods.get(position));
HolderView.EventDisc.setText(MethodDiscription.get(position));
HolderView.iv.setBackgroundResource(Icons.get(position));
return convertView;
}
private class ViewHolder {
TextView AppEvent;
TextView EventDisc;
ImageView iv;
}
}
public void fillArrayList() {
Methods.clear();
MethodDiscription.clear();
Icons.clear();
Methods.add(0,"Applications");
Methods.add(1,"System Resources");
Methods.add(2,"Statistics");
Methods.add(3,"Threats");
MethodDiscription.add(0, "bla bla bla");
MethodDiscription.add(1, "bla bla bla");
MethodDiscription.add(2, "bla bla bla");
MethodDiscription.add(3, "bla bla bla");
Icons.add(0, R.drawable.ic_launcher);
Icons.add(1, R.drawable.ic_launcher);
Icons.add(2, R.drawable.ic_launcher);
Icons.add(3, R.drawable.ic_launcher);
}
}
오류가 발생한 장소에서 로그 캣 : 당신의 ID를 검색하지 않는
이07-05 01:44:50.678: W/dalvikvm(1907): threadid=1: thread exiting with uncaught exception (group=0x40a13300)
07-05 01:44:50.728: E/AndroidRuntime(1907): FATAL EXCEPTION: main
07-05 01:44:50.728: E/AndroidRuntime(1907): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.sliit.droidman/com.sliit.droidman.main.MainActivity}: java.lang.NullPointerException
07-05 01:44:50.728: E/AndroidRuntime(1907): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
07-05 01:44:50.728: E/AndroidRuntime(1907): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
07-05 01:44:50.728: E/AndroidRuntime(1907): at android.app.ActivityThread.access$600(ActivityThread.java:130)
07-05 01:44:50.728: E/AndroidRuntime(1907): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
07-05 01:44:50.728: E/AndroidRuntime(1907): at android.os.Handler.dispatchMessage(Handler.java:99)
07-05 01:44:50.728: E/AndroidRuntime(1907): at android.os.Looper.loop(Looper.java:137)
07-05 01:44:50.728: E/AndroidRuntime(1907): at android.app.ActivityThread.main(ActivityThread.java:4745)
07-05 01:44:50.728: E/AndroidRuntime(1907): at java.lang.reflect.Method.invokeNative(Native Method)
07-05 01:44:50.728: E/AndroidRuntime(1907): at java.lang.reflect.Method.invoke(Method.java:511)
07-05 01:44:50.728: E/AndroidRuntime(1907): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
07-05 01:44:50.728: E/AndroidRuntime(1907): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
07-05 01:44:50.728: E/AndroidRuntime(1907): at dalvik.system.NativeStart.main(Native Method)
07-05 01:44:50.728: E/AndroidRuntime(1907): Caused by: java.lang.NullPointerException
07-05 01:44:50.728: E/AndroidRuntime(1907): at com.sliit.droidman.main.MainActivity.onCreate(MainActivity.java:47)
07-05 01:44:50.728: E/AndroidRuntime(1907): at android.app.Activity.performCreate(Activity.java:5008)
07-05 01:44:50.728: E/AndroidRuntime(1907): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
07-05 01:44:50.728: E/AndroidRuntime(1907): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
07-05 01:44:50.728: E/AndroidRuntime(1907): ... 11 more
LogCat을 게시하십시오. – Nerd
그것은 나쁜 코드 조각입니다. – pawelzieba
나쁜 코드를 알려주십시오. –