Android Studio跳转页面按钮无效问题求助
Hey there! Let's get that Proceed button working for you. I spotted the main issue right away in your MainActivity code, plus a couple of other checks to make sure everything runs smoothly.
The Root Cause: Empty Custom Method
In your button click listener, you're calling a custom startActivityFromFragment() method—but this method is completely empty! It doesn't execute any logic to launch your FirstQuiz activity. That's exactly why clicking the button does nothing at all.
Fix 1: Use the Correct Activity Launch Method
Replace your empty custom method with Android's built-in startActivity() to properly launch FirstQuiz. Here's the corrected MainActivity code:
package com.example.john.quiz1; import android.content.Intent; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.Button; public class MainActivity extends AppCompatActivity { Button btn; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btn = (Button) findViewById(R.id.button); btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // Use Android's built-in method to launch the activity startActivity(new Intent(MainActivity.this, FirstQuiz.class)); } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.menu_main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { int id = item.getItemId(); if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } }
Fix 2: Verify FirstQuiz is Registered in AndroidManifest.xml
Even with the corrected launch code, your app might fail silently if FirstQuiz isn't registered in your manifest. Make sure you add this entry inside the <application> tag of your AndroidManifest.xml:
<activity android:name=".FirstQuiz" />
Extra Tip: Improve Layout Switching in FirstQuiz
While not related to your current button issue, I noticed you're using setContentView() multiple times to switch layouts in FirstQuiz. This can cause memory leaks and lose UI state. For a better approach, consider using a ViewSwitcher, FrameLayout with fragments, or toggling view visibility instead. But that's a polish step you can tackle later!
Give these fixes a try, and your Proceed button should launch FirstQuiz without any problems.
内容的提问来源于stack exchange,提问作者JHUB

