안드로이드 Activity의 EditText 값을 fragment의 커스텀 리스트뷰의 값으로 받아오는 과정에서 오류가 뭘까요?
조회수 433회
Fragment로 커뮤니티 게시판을 만들고 있습니다 Activity에 작성한 글을 Fragment 안에 있는 커스텀 리스트뷰에 넣으려고합니다 구글링으로 방법을 찾아서 하고 있는데 java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.os.Bundle.getString(java.lang.String)' on a null object reference at com.example.project.ChatFragment.onCreateView(ChatFragment.java:33) 이렇게 오류 메세지가 뜨네요
어떻게 해결해야할지 모르겠습니다 엑티비티와 프래그먼트 코드 올립니다 혹시 몰라서 BaseAdater 코드도 올립니다
//엑티비티
package com.example.project;
import android.app.Activity;
import android.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
public class Comunity extends AppCompatActivity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.comunity);
EditText cet1=(EditText)findViewById(R.id.cet1);
EditText cet2=(EditText)findViewById(R.id.cet2);
Button cbtn1=(Button)findViewById(R.id.cbtn1);
Button cbtn2=(Button)findViewById(R.id.cbtn2);
FragmentManager manager=getSupportFragmentManager();
FragmentTransaction transaction=manager.beginTransaction();
ChatFragment chatFragment = new ChatFragment();
Bundle bundle=new Bundle();
cbtn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
cbtn2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String cTitle=cet1.getText().toString();
String cText=cet2.getText().toString();
bundle.getString("mainTitle",cTitle);
bundle.getString("mainText1",cText);
transaction.replace(R.id.cbtn2,chatFragment).commit();
chatFragment.setArguments(bundle);
}
});
}
}
//Fragment
package com.example.project;
import android.content.Intent;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.TextView;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
public class ChatFragment extends Fragment {
private View view;
private ListView chatList;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
view=inflater.inflate(R.layout.fragment_chat,container,false);
chatList=view.findViewById(R.id.chatList1);
ChatListItem clAdapter=new ChatListItem();
FloatingActionButton fab2=view.findViewById(R.id.fab2);
TextView chatcustom1=view.findViewById(R.id.chatcustom1);
TextView chatcustom2=view.findViewById(R.id.chatcustom2);
Bundle bundle=getArguments();
chatList.setAdapter(clAdapter);
clAdapter.addChatList("맨시티 첼시 4:4 난타전","qoad123");
clAdapter.addChatList("나이키 축구화 대박 세일","mzkxn753");
clAdapter.addChatList("매칭 비매너 개쩌네","okc654");
String Title=bundle.getString("mainTitle",null);
String Text=bundle.getString("mainText1",null);
chatcustom1.setText(Title);
chatcustom2.setText(Text);
fab2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(getActivity(), Comunity.class));
}
});
clAdapter.addChatList(Title,Text);
clAdapter.notifyDataSetChanged();
return view;
}
}
//BaseAdapter
package com.example.project;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import java.util.ArrayList;
public class ChatListItem extends BaseAdapter {
ArrayList<ChatList> chatlist=new ArrayList<>();
@Override
public int getCount() {
return chatlist.size();
}
@Override
public Object getItem(int position) {
return chatlist.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Context c= parent.getContext();
if (convertView==null){
LayoutInflater li= (LayoutInflater) c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView=li.inflate(R.layout.chatcustom,parent,false);
}
TextView chatcustom1=convertView.findViewById(R.id.chatcustom1);
TextView chatcustom2=convertView.findViewById(R.id.chatcustom2);
ChatList cl=chatlist.get(position);
chatcustom1.setText(cl.getTitle());
chatcustom2.setText(cl.getWriter());
return convertView;
}
public void addChatList(String title, String writer){
ChatList cl=new ChatList();
cl.setTitle(title);
cl.setWriter(writer);
chatlist.add(cl);
}
}
댓글 입력