2017-03-19 8 views
1

내 앱에서이 링크의 대화방을 사용하고있었습니다. 채팅방을 클릭하면 앱이 다운 됨

https://gist.github.com/puf/f49a1b07e92952b44f2dc36d9af04e3c

내 애플 흐름 :

로그인 -> MainActivity -> 목록보기 채팅에 가입에서 팀을 선택 -> ChatRoomActivity

전에서 팀 중 하나를 클릭하면 내 문제입니다 내 listview, 그것은 충돌합니다. chatroom에 대한 내 코드는 MainActivity를 ChatRoomActivity로 변경하고 로그인 팝업을 삭제한다는 것을 제외하고는 링크와 정확히 동일합니다. 나머지는 동일하게 유지됩니다. 누군가가 문제를 해결하거나 원인을 파악하도록 도와 줄 수 있습니까?

MainActivity :

public class MainActivity extends AppCompatActivity { 
public static final String TEAM_NAME = "com.example.user.stfv2.teamname"; 
public static final String TEAM_ID = "com.example.user.stfv2.teamid"; 

private FirebaseAuth firebaseAuth; 

EditText editTextName; 
EditText textDate; 
EditText textTime; 
Spinner spinnerSport; 
Button buttonAddTeam; 
ListView listViewTeams; 
DatePickerDialog datePickerDialog; 

List<Team> teams; 

DatabaseReference databaseTeams; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    firebaseAuth = FirebaseAuth.getInstance(); 

    if(firebaseAuth.getCurrentUser() == null){ 

     finish(); 

     startActivity(new Intent(this, SignInActivity.class)); 
    } 

    FirebaseUser user = firebaseAuth.getCurrentUser(); 

    databaseTeams = FirebaseDatabase.getInstance().getReference("teams"); 

    editTextName = (EditText) findViewById(R.id.editTextName); 
    spinnerSport = (Spinner) findViewById(R.id.spinnerSports); 
    listViewTeams = (ListView) findViewById(R.id.listViewTeams); 
    textDate = (EditText) findViewById(R.id.textDate); 
    textTime = (EditText) findViewById(R.id.textTime); 


    buttonAddTeam = (Button) findViewById(R.id.buttonAddTeam); 

    teams = new ArrayList<>(); 

    buttonAddTeam.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      Intent myIntent = new Intent(MainActivity.this, 
        AddActivity.class); 
      startActivity(myIntent); 
     } 
    }); 


    listViewTeams.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
     @Override 
     public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) { 

      Team team = teams.get(i); 


      Intent intent = new Intent(getApplicationContext(), ChatRoomActivity.class); 

      intent.putExtra(TEAM_ID, team.getTeamId()); 
      intent.putExtra(TEAM_NAME, team.getTeamName()); 

      startActivity(intent); 
     } 
    }); 

    listViewTeams.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() { 
     @Override 
     public boolean onItemLongClick(AdapterView<?> adapterView, View view, int i, long l) { 
      Team team = teams.get(i); 
      showUpdateDeleteDialog(team.getTeamId(), team.getTeamName()); 
      return true; 
     } 
    }); 

} 

private void showUpdateDeleteDialog(final String teamId, String teamName) { 

    AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this); 
    LayoutInflater inflater = getLayoutInflater(); 
    final View dialogView = inflater.inflate(R.layout.update_dialog, null); 
    dialogBuilder.setView(dialogView); 

    final EditText editTextName = (EditText) dialogView.findViewById(R.id.editTextName); 
    final Spinner spinnerSport = (Spinner) dialogView.findViewById(R.id.spinnerSports); 
    final Button buttonUpdate = (Button) dialogView.findViewById(R.id.buttonUpdateTeam); 
    final Button buttonDelete = (Button) dialogView.findViewById(R.id.buttonDeleteTeam); 
    final EditText textDate = (EditText) dialogView.findViewById(R.id.textDate); 
    final EditText textTime = (EditText) dialogView.findViewById(R.id.textTime); 

    dialogBuilder.setTitle(teamName); 
    final AlertDialog b = dialogBuilder.create(); 
    b.show(); 

    textDate.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 

      final Calendar c = Calendar.getInstance(); 
      int mYear = c.get(Calendar.YEAR); 
      int mMonth = c.get(Calendar.MONTH); 
      int mDay = c.get(Calendar.DAY_OF_MONTH); 

      datePickerDialog = new DatePickerDialog(MainActivity.this, 
        new DatePickerDialog.OnDateSetListener() { 

         @Override 
         public void onDateSet(DatePicker view, int year, 
               int monthOfYear, int dayOfMonth) { 

          textDate.setText(dayOfMonth + "/" 
            + (monthOfYear + 1) + "/" + year); 

         } 
        }, mYear, mMonth, mDay); 
      datePickerDialog.show(); 
     } 
    }); 

    textTime.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      Calendar mcurrentTime = Calendar.getInstance(); 
      int hour = mcurrentTime.get(Calendar.HOUR_OF_DAY); 
      int minute = mcurrentTime.get(Calendar.MINUTE); 
      TimePickerDialog mTimePicker; 
      mTimePicker = new TimePickerDialog(MainActivity.this, new TimePickerDialog.OnTimeSetListener() { 
       @Override 
       public void onTimeSet(TimePicker timePicker, int selectedHour, int selectedMinute) { 
        textTime.setText(selectedHour + ":" + selectedMinute); 
       } 
      }, hour, minute, true); 
      mTimePicker.setTitle("Select Time"); 
      mTimePicker.show(); 

     } 
    }); 


    buttonUpdate.setOnClickListener(new View.OnClickListener() { 

     FirebaseUser user = firebaseAuth.getCurrentUser(); 

     @Override 
     public void onClick(View view) { 
      String name = editTextName.getText().toString().trim(); 
      String sport = spinnerSport.getSelectedItem().toString(); 
      String owner = user.getUid(); 
      String date = textDate.getText().toString(); 
      String time = textTime.getText().toString(); 
      if (!TextUtils.isEmpty(name)) { 
       updateTeam(teamId, name, sport, owner, date, time); 
       b.dismiss(); 
      } 
     } 
    }); 


    buttonDelete.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 

      deleteTeam(teamId); 
      b.dismiss(); 
     } 
    }); 
} 

private boolean updateTeam(String id, String name, String sport, String owner, String date, String time) { 

    DatabaseReference dR = FirebaseDatabase.getInstance().getReference("teams").child(id); 

    Team team = new Team(id, name, sport, owner, date, time); 
    dR.setValue(team); 
    Toast.makeText(getApplicationContext(), "Team Updated", Toast.LENGTH_LONG).show(); 
    return true; 
} 

private boolean deleteTeam(String id) { 

    DatabaseReference dR = FirebaseDatabase.getInstance().getReference("teams").child(id); 


    dR.removeValue(); 


    DatabaseReference drTracks = FirebaseDatabase.getInstance().getReference("tracks").child(id); 

    drTracks.removeValue(); 
    Toast.makeText(getApplicationContext(), "Team Deleted", Toast.LENGTH_LONG).show(); 

    return true; 
} 

@Override 
protected void onStart() { 
    super.onStart(); 

    databaseTeams.addValueEventListener(new ValueEventListener() { 
     @Override 
     public void onDataChange(DataSnapshot dataSnapshot) { 


      teams.clear(); 


      for (DataSnapshot postSnapshot : dataSnapshot.getChildren()) { 

       Team team = postSnapshot.getValue(Team.class); 

       teams.add(team); 
      } 

      TeamList teamAdapter = new TeamList(MainActivity.this, teams); 

      listViewTeams.setAdapter(teamAdapter); 
     } 

     @Override 
     public void onCancelled(DatabaseError databaseError) { 

     } 
    }); 
} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    MenuInflater inflater = getMenuInflater(); 
    inflater.inflate(R.menu.menu_main, menu); 
    return true; 
} 

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 

    switch (item.getItemId()) { 
     case R.id.action_logout: 

      firebaseAuth.signOut(); 

      finish(); 

      startActivity(new Intent(this, SignInActivity.class)); 
      return true; 
     default: 
      return super.onOptionsItemSelected(item); 
    } 
} 
} 

로그 캣 :

03-19 01:40:25.709 2995-2995/? E/AndroidRuntime: FATAL EXCEPTION: main 
              Process: com.example.user.stfv2, PID: 2995 
              java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.user.stfv2/com.example.user.stfv2.ChatRoomActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference 
               at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416) 
               at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
               at android.app.ActivityThread.-wrap11(ActivityThread.java) 
               at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
               at android.os.Handler.dispatchMessage(Handler.java:102) 
               at android.os.Looper.loop(Looper.java:148) 
               at android.app.ActivityThread.main(ActivityThread.java:5417) 
               at java.lang.reflect.Method.invoke(Native Method) 
               at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
               at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
               Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference 
               at com.example.user.stfv2.ChatRoomActivity.setUsername(ChatRoomActivity.java:58) 
               at com.example.user.stfv2.ChatRoomActivity.onCreate(ChatRoomActivity.java:73) 
               at android.app.Activity.performCreate(Activity.java:6237) 
               at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107) 
               at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369) 
               at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)  
               at android.app.ActivityThread.-wrap11(ActivityThread.java)  
               at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)  
               at android.os.Handler.dispatchMessage(Handler.java:102)  
               at android.os.Looper.loop(Looper.java:148)  
               at android.app.ActivityThread.main(ActivityThread.java:5417)  
               at java.lang.reflect.Method.invoke(Native Method)  
               at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)  
               at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)  

편집 1 :

layout_chat_room :

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:paddingBottom="@dimen/activity_vertical_margin" 
android:paddingLeft="@dimen/activity_horizontal_margin" 
android:paddingRight="@dimen/activity_horizontal_margin" 
android:paddingTop="@dimen/activity_vertical_margin" 
tools:context="com.example.user.stfv2.MainActivity"> 

<RelativeLayout 
    android:orientation="horizontal" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:id="@+id/header" 
    android:gravity="end"> 

    <ImageView 
     android:layout_width="36dp" 
     android:layout_height="36dp" 
     android:id="@+id/userIcon" 
     android:foregroundGravity="center" 
     android:layout_alignParentStart="true" 
     android:layout_alignParentLeft="true" /> 

    <TextView 
     android:layout_width="141dp" 
     android:layout_height="wrap_content" 
     android:id="@+id/usernameTxt" 
     android:layout_toRightOf="@+id/userIcon" 
     android:layout_alignTop="@+id/userIcon" 
     android:layout_alignBottom="@+id/userIcon" 
     android:gravity="center_vertical" 
     tools:text="Username" 
     android:layout_weight="0" /> 

    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Sign in" 
     android:id="@+id/loginBtn" 
     android:layout_alignParentEnd="true" 
     android:layout_alignParentRight="true" /> 
    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Sign out" 
     android:id="@+id/logoutBtn" 
     android:layout_alignParentEnd="true" 
     android:layout_alignParentRight="true" /> 
</RelativeLayout> 

<android.support.v7.widget.RecyclerView 
    android:id="@+id/messagesList" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentStart="true" 
    tools:listitem="@android:layout/two_line_list_item" 
    android:layout_above="@+id/footer" 
    android:layout_below="@+id/header" /> 

<LinearLayout 
    android:orientation="horizontal" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_alignParentBottom="true" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentStart="true" 
    android:id="@+id/footer"> 

    <ImageButton 
     android:layout_width="36dp" 
     android:layout_height="36dp" 
     android:id="@+id/imageBtn" 
     android:background="@android:drawable/ic_menu_gallery" /> 

    <EditText 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/messageTxt" 
     android:layout_gravity="bottom" 
     android:layout_weight="1" 
     android:inputType="textShortMessage|textAutoCorrect" /> 

    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Send" 
     android:id="@+id/sendBtn" 
     android:layout_gravity="bottom" /> 
</LinearLayout> 
</RelativeLayout> 

ChatRoomActivity :

public class ChatRoomActivity extends AppCompatActivity { 
private static final String TAG = "ChatRoomActivity"; 

static final int RC_PHOTO_PICKER = 1; 

private Button sendBtn; 
private EditText messageTxt; 
private RecyclerView messagesList; 
private ChatMessageAdapter adapter; 
private ImageButton imageBtn; 
private TextView usernameTxt; 
private View loginBtn; 
private View logoutBtn; 

private FirebaseApp app; 
private FirebaseDatabase database; 
private FirebaseAuth auth; 
private FirebaseStorage storage; 

private DatabaseReference databaseRef; 
private StorageReference storageRef; 

private String username; 

private void setUsername(String username) { 
    Log.d(TAG, "setUsername("+String.valueOf(username)+")"); 
    if (username == null) { 
     username = "Android"; 
    } 
    boolean isLoggedIn = !username.equals("Android"); 
    this.username = username; 
    this.usernameTxt.setText(username); 
    this.logoutBtn.setVisibility(isLoggedIn ? View.VISIBLE : View.GONE); 
    this.loginBtn .setVisibility(isLoggedIn ? View.GONE : View.VISIBLE); 
} 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    sendBtn = (Button) findViewById(R.id.sendBtn); 
    messageTxt = (EditText) findViewById(R.id.messageTxt); 
    messagesList = (RecyclerView) findViewById(R.id.messagesList); 
    imageBtn = (ImageButton) findViewById(R.id.imageBtn); 
    loginBtn = findViewById(R.id.loginBtn); 
    logoutBtn = findViewById(R.id.logoutBtn); 
    usernameTxt = (TextView) findViewById(R.id.usernameTxt); 
    setUsername("Android"); 

    LinearLayoutManager layoutManager = new LinearLayoutManager(this); 
    messagesList.setHasFixedSize(false); 
    messagesList.setLayoutManager(layoutManager); 

    // Show an image picker when the user wants to upload an imasge 
    imageBtn.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View v) { 
      Intent intent = new Intent(Intent.ACTION_GET_CONTENT); 
      intent.setType("image/jpeg"); 
      intent.putExtra(Intent.EXTRA_LOCAL_ONLY, true); 
      startActivityForResult(Intent.createChooser(intent, "Complete action using"), RC_PHOTO_PICKER); 
     } 
    }); 
    // Show a popup when the user asks to sign in 
    loginBtn.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View v) { 
      LoginDialog.showLoginPrompt(ChatRoomActivity.this, app); 
     } 
    }); 
    // Allow the user to sign out 
    logoutBtn.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View v) { 
      auth.signOut(); 
     } 
    }); 

    adapter = new ChatMessageAdapter(this); 
    messagesList.setAdapter(adapter); 
    adapter.registerAdapterDataObserver(new RecyclerView.AdapterDataObserver() { 
     public void onItemRangeInserted(int positionStart, int itemCount) { 
      messagesList.smoothScrollToPosition(adapter.getItemCount()); 
     } 
    }); 

    // Get the Firebase app and all primitives we'll use 
    app = FirebaseApp.getInstance(); 
    database = FirebaseDatabase.getInstance(app); 
    auth = FirebaseAuth.getInstance(app); 
    storage = FirebaseStorage.getInstance(app); 

    // Get a reference to our chat "room" in the database 
    databaseRef = database.getReference("chat"); 

    sendBtn.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View v) { 
      ChatMessage chat = new ChatMessage(username, messageTxt.getText().toString()); 
      // Push the chat message to the database 
      databaseRef.push().setValue(chat); 
      messageTxt.setText(""); 
     } 
    }); 
    // Listen for when child nodes get added to the collection 
    databaseRef.addChildEventListener(new ChildEventListener() { 
     public void onChildAdded(DataSnapshot snapshot, String s) { 
      // Get the chat message from the snapshot and add it to the UI 
      ChatMessage chat = snapshot.getValue(ChatMessage.class); 
      adapter.addMessage(chat); 
     } 

     public void onChildChanged(DataSnapshot dataSnapshot, String s) { } 
     public void onChildRemoved(DataSnapshot dataSnapshot) { } 
     public void onChildMoved(DataSnapshot dataSnapshot, String s) { } 
     public void onCancelled(DatabaseError databaseError) { } 
    }); 

    // When the user has entered credentials in the login dialog 
    LoginDialog.onCredentials(new OnSuccessListener<LoginDialog.EmailPasswordResult>() { 
     public void onSuccess(LoginDialog.EmailPasswordResult result) { 
      // Sign the user in with the email address and password they entered 
      auth.signInWithEmailAndPassword(result.email, result.password); 
     } 
    }); 

    // When the user signs in or out, update the username we keep for them 
    auth.addAuthStateListener(new FirebaseAuth.AuthStateListener() { 
     public void onAuthStateChanged(FirebaseAuth firebaseAuth) { 
      if (firebaseAuth.getCurrentUser() != null) { 
       // User signed in, set their email address as the user name 
       setUsername(firebaseAuth.getCurrentUser().getEmail()); 
      } 
      else { 
       // User signed out, set a default username 
       setUsername("Android"); 
      } 
     } 
    }); 
} 

public void onActivityResult(int requestCode, int resultCode, Intent data) { 
    if (requestCode == RC_PHOTO_PICKER && resultCode == RESULT_OK) { 
     Uri selectedImageUri = data.getData(); 

     // Get a reference to the location where we'll store our photos 
     storageRef = storage.getReference("chat_photos"); 
     // Get a reference to store file at chat_photos/<FILENAME> 
     final StorageReference photoRef = storageRef.child(selectedImageUri.getLastPathSegment()); 

     // Upload file to Firebase Storage 
     photoRef.putFile(selectedImageUri) 
       .addOnSuccessListener(this, new OnSuccessListener<UploadTask.TaskSnapshot>() { 
        public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) { 
         // When the image has successfully uploaded, we get its download URL 
         Uri downloadUrl = taskSnapshot.getDownloadUrl(); 
         // Set the download URL to the message box, so that the user can send it to the database 
         messageTxt.setText(downloadUrl.toString()); 
        } 
       }); 
    } 
} 
} 
+0

내가 chatroom 활동에 textview 중 하나에 문제가 있다고 생각합니다. 어쩌면 당신의 견해와 관련이 없었을 수도 있습니다. 사용하기 전에 모든 위젯이 연결되었는지 확인하십시오. – arisalsaila

+0

chatroom의 유일한 textview는 사용자 이름입니다. 미안하지만 당신이 말하는 것을 이해하지 못합니다. – Zephyros

+0

미안하지만, 나는 많은 textview가 있습니다. 메신저, 문제는 null textview, 어쩌면 그것은 초기화되지 않았습니다. java와 xml 레이아웃 사이에 연결되었는지 확인하십시오 (예 : id findView (R.id.tv_username)). – arisalsaila

답변

1

문제는 잘못된 XML 파일을 정의하고 있다는 것입니다. ChatroomActivity에서 코드를 사용하면 XML 파일을 변경하는 것을 잊었습니다. 현재 setContentView(R.layout.activity_main)으로 설정되어 있지만 setContentView(R.layout.layout_chat_room)이어야합니다.

+0

내 ChatRoomActivity 코드는 링크의 MainActivity와 정확히 같으며 동일한 코드와 XML 레이아웃을 사용했지만 여전히 동일한 오류가 발생합니다. – Zephyros

+0

글쎄, 똑같은 것은 아닙니다. 내가 말했듯이, 그것을 보는 것이 도움이 될 것입니다. 그리고 XML 코드를 ChatroomActivity로 보는 것이 좋을 것입니다. 게시 할 필요는 없지만 도움이 될 것입니다. 그것은 당신의 선택입니다. 당신은 그것을 변경했고, 우리는 정확히 무슨 일이 일어나는지 볼 필요가 있습니다. – BlackHatSamurai

+0

당신은 이름을 바꾸고 팝업을 꺼냈다 고 했지요. 우리는 당신이 무엇을 바꿨는지 정확히 볼 필요가 있습니다. 모든 것이 정확히 동일하다고 가정하면 도움이되지 않습니다. ChatroomActivity의 XML과 사용하고있는 코드를 알려 주시면 도움을받을 수 있습니다. – BlackHatSamurai