Android Studio中LoginTwoResult.java两段代码合并方法咨询
Hey there! I see you're new to Android Studio and ran into trouble combining two working code snippets into one. Let's break down the issues and fix this together.
What's Causing the Problem?
Your combined code fails for two key reasons:
- Duplicate
onCreatemethods: Java doesn't allow two methods with the same signature in a single class—this would throw a compilation error immediately. - Conflicting layout files: The first snippet uses
R.layout.logintwo_resultwhile the second usesR.layout.activity_menu. CallingsetContentView()twice replaces the entire UI, so theTextView_getIDfrom the first layout won't exist in the second, leading to aNullPointerExceptionwhen you try to use it.
Note: The Vulkan errors in your log are just emulator rendering issues (unrelated to your code logic)—you can ignore them or fix them later by switching the emulator to software rendering mode.
Step 1: Prepare Your Layout
First, decide where to show the welcome message ([ID]님 환영합니다):
Option 1: Show it in the Activity's main layout
Open activity_menu.xml and add this TextView inside your root layout (it will stay visible above the bottom navigation bar):
<TextView android:id="@+id/TextView_getID" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="18sp" android:layout_marginTop="20dp" android:layout_gravity="center_horizontal"/>
Option 2: Show it in the default fragment (Frag2)
If you want the welcome text inside Frag2 instead, we'll pass the ID from the Activity to the fragment later.
Step 2: Combined Working Code
Here's the merged LoginTwoResult.java with all logic in a single onCreate method, and proper variable handling:
public class LoginTwoResult extends AppCompatActivity { // Variables from first snippet private TextView TextView_getID; // Variables from second snippet private BottomNavigationView bottomNavigationView; private FragmentManager fm; private FragmentTransaction ft; private Frag1 frag1; private Frag2 frag2; private Frag3 frag3; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // Use the activity_menu layout (with added TextView if you chose Option 1) setContentView(R.layout.activity_menu); // Handle intent data (from first snippet) TextView_getID = findViewById(R.id.TextView_getID); Intent intent = getIntent(); Bundle bundle = intent.getExtras(); // Add null check to avoid crashes if no extras are passed if (bundle != null) { String ID = bundle.getString("ID"); String PassWord = bundle.getString("PassWord"); TextView_getID.setText(ID + "님 환영합니다"); } // Initialize bottom navigation and fragments (from second snippet) bottomNavigationView = findViewById(R.id.bottomNavi); bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() { @Override public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) { switch (menuItem.getItemId()) { case R.id.action_car: setFrag(0); break; case R.id.action_home: setFrag(1); break; case R.id.action_person: setFrag(2); break; } return true; } }); frag1 = new Frag1(); frag2 = new Frag2(); frag3 = new Frag3(); setFrag(1); // Show Frag2 by default } // Fragment replacement method (unchanged from second snippet) private void setFrag(int n) { fm = getSupportFragmentManager(); ft = fm.beginTransaction(); switch (n) { case 0: ft.replace(R.id.main_frame, frag1); ft.commit(); break; case 1: ft.replace(R.id.main_frame, frag2); ft.commit(); break; case 2: ft.replace(R.id.main_frame, frag3); ft.commit(); break; } } }
If You Chose Option 2 (Welcome Message in Frag2)
Adjust the code to pass the ID from the Activity to Frag2:
- In the Activity's
onCreatemethod, replacefrag2 = new Frag2();with:
String userId = bundle.getString("ID"); frag2 = Frag2.newInstance(userId);
- Add this static factory method to
Frag2.java:
public static Frag2 newInstance(String userId) { Frag2 frag = new Frag2(); Bundle args = new Bundle(); args.putString("USER_ID", userId); frag.setArguments(args); return frag; }
- In Frag2's
onViewCreatedmethod, retrieve the ID and set the text:
@Override public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); TextView welcomeText = view.findViewById(R.id.welcome_text); String userId = getArguments().getString("USER_ID"); welcomeText.setText(userId + "님 환영합니다"); }
Don't forget to add a TextView with id welcome_text to Frag2's layout file.
内容的提问来源于stack exchange,提问作者정유석

