I created a android studio button for my app and when I click on the 'Proceed' button it doesn't work. I don't get any errors it just doesn't work. When the user clicks the 'Proceed' button I want to go to the First Quiz.java.
I have looked at all other similar questions but cannot seem to find what I am doing wrong.
MainActivity.java
package com.example.john.quiz1;
import android.content.DialogInterface;
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;
import android.view.View.OnClickListener;
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) {
startActivityFromFragment(new Intent(MainActivity.this, FirstQuiz.class));
}
private void startActivityFromFragment(Intent intent) {
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="160dp"
android:text="Welcome to Road Sign Quiz"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#0000ff"
android:textSize="24sp" />
<Button
android:id="@+id/button"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView2"
android:layout_centerHorizontal="true"
android:layout_marginTop="40dp"
android:background="#0000ff"
android:text="proceed"
android:textColor="#ffffff"
android:textSize="25dp" />
</RelativeLayout>
FirstQuiz.java
package com.example.john.quiz1;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.Button;
import android.widget.RadioGroup;
import com.example.john.quiz1.R;
public class FirstQuiz extends AppCompatActivity {
private RadioGroup radioGroup;
private Button nxt;
int pos;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.quiz1);
radioGroup = (RadioGroup) findViewById(R.id.radioGroup);
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId){
if (checkedId == R.id.radio1) {
setContentView(R.layout.final2);
}
else if (checkedId == R.id.radio2){
setContentView(R.layout.final2);
}
else if (checkedId == R.id.radio3) {
setContentView(R.layout.final1);
}
else{
setContentView(R.layout.final2);
}
}
});
}
}
Your startActivityFromFragment
method should be outside of your onClickListener()
. This method is also empty, so it's not going to do anything when you call it.
It also looks like onCreateOptionsMenu
and onOptionsItemSelected
is outside your MainActivity
class.
Try putting only startActivity(intent) and also android:clickable="true" and android:focusable="true" in your button xml.
Also I've noticed that your startActivityFromFragment() method won't do anything because you are calling an empty method, so in that method you should launch the intent.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.