Я делаю простой чат-приложение. Но когда я начала чата деятельности это метание исключение NullPointer ..android.content.Context.getSystemService (java.lang.String) throwing nullpointerexception
Так что это ошибка,
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.buckydroid.chat, PID: 1293
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.buckydroid.chat/com.buckydroid.chat.ChatView}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object android.content.Context.getSystemService(java.lang.String)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2665)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6126)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object android.content.Context.getSystemService(java.lang.String)' on a null object reference
at android.view.LayoutInflater.from(LayoutInflater.java:232)
at android.widget.ArrayAdapter.<init>(ArrayAdapter.java:181)
at android.widget.ArrayAdapter.<init>(ArrayAdapter.java:114)
at com.buckydroid.chat.ChatAdapter.<init>(ChatAdapter.java:0)
at com.buckydroid.chat.ChatView.onCreate(ChatView.java:31)
at android.app.Activity.performCreate(Activity.java:6679)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2618)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6126)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
А вот мой chatadapter код
private List<ChatDataProvider> chat_list= new ArrayList<ChatDataProvider>();
private TextView chat_text;
Context ctx;
public ChatAdapter(Context context, int resource) {
super(context, resource);
ctx = context;
}
@Override
public void add(ChatDataProvider object) {
chat_list.add(object);
super.add(object);
}
@Override
public int getCount() {
return chat_list.size();
}
@Override
public ChatDataProvider getItem(int position) {
return chat_list.get(position);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView == null){
LayoutInflater layoutInflater =(LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = layoutInflater.inflate(R.layout.single_chat_message,parent,false);
}
chat_text = (TextView)convertView.findViewById(R.id.message_text);
String Message;
boolean POSITION;
ChatDataProvider provider = getItem(position);
Message=provider.message;
POSITION = provider.position;
chat_text.setText(Message);
chat_text.setBackgroundResource(POSITION ? R.drawable.bubble : R.drawable.bubble);
RelativeLayout.LayoutParams layoutParams= new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
if (!POSITION){
layoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
}else {
layoutParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
}
chat_text.setLayoutParams(layoutParams);
return convertView;
}
}
И я пытаюсь начать активность ChatView из основного вида деятельности.
Вот код класса chatview Java
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.chat_view);
chat_list = (ListView)findViewById(R.id.message_list);
chat_text = (EditText)findViewById(R.id.chat_input_text);
send_btn = (ImageButton) findViewById(R.id.send);
chatAdapter = new ChatAdapter(context,R.layout.single_chat_message);
chat_list.setAdapter(chatAdapter);
chat_list.setTranscriptMode(AbsListView.TRANSCRIPT_MODE_ALWAYS_SCROLL);
send_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
chatAdapter.add(new ChatDataProvider(position,chat_text.getText().toString()));
position = !position;
chat_text.setText("");
}
});
}
Видимо, 'CTX 'is' null ', хотя это поможет, если вы разместите полную трассировку стека. Проверьте, где вы вызываете конструктор 'ChatAdapter', и смотрите, почему вы передаете ему« null ». – CommonsWare
no it's not @Charuka – Doge
добавил полный журнал @CommonsWare – Doge