0

So I have a code, that's only works in the MainActivity, and I don't know how to make it work, in the second activity.

import android.app.Activity;
import android.content.res.Resources;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import java.util.Random;


public class MainActivity extends Activity {

    private String[] myString;
    private static final Random rgenerator = new Random();

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


        Resources res = getResources();

        myString = res.getStringArray(R.array.myArray);

        String q = myString[rgenerator.nextInt(myString.length)];

        TextView tv = (TextView) findViewById(R.id.textView);
        tv.setText(q);
    }

    @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;
    }

    public void ButtonOnClick (View v) {
        Button button = (Button) v;
        setContentView(R.layout.activity_szabaly);
    }

    @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);
    }
}

When I start my app, in the first activity the text is changing(because it's a random text changing code), but when I press the button, that makes me go to the second activity, nothing happens, no action in the second activity.

What am I doing wrong?

My Manifest file

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.example" >

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".Szabaly"
            android:label="@string/title_activity_szabaly" >
        </activity>
    </application>

</manifest>
Tin_Ram
  • 99
  • 1
  • 11

2 Answers2

1

You're not actually starting a new activity on your button click. Instead you're just changing your view. This will not be cause to call the onCreate method again.

public void ButtonOnClick (View v) {
    Button button = (Button) v;
    setContentView(R.layout.activity_szabaly);
}

This code is only changing the view. To start a new activity you'd need to create a new class that extends Activity (just like MainActivity) and register it in the AndroidManifest.xml.

Then you'll be able to start the new activity on click by the following

public void ButtonOnClick (View v) {
    Intent i = new Intent(this, NewActivity.class);
    startActivity(i);
}

See this question for more detail on starting a new Activity from a button click.

Community
  • 1
  • 1
Paul Thompson
  • 3,290
  • 2
  • 31
  • 39
1

you add the android:onClick attribute in the layout ? this is bad practice

better implementing View.OnClickListener

public class MainActivity extends Activity {

private String[] myString;
private static final Random rgenerator = new Random();

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


    Resources res = getResources();

    myString = res.getStringArray(R.array.myArray);

    String q = myString[rgenerator.nextInt(myString.length)];

    TextView tv = (TextView) findViewById(R.id.textView);
    tv.setText(q);
    //finde button
    Button button =(Button)findViewById(R.id.button);
    //set OnClickListener
    button.setOnClickListener(this);
}

@Override
public void onClick(View v) {
    switch (v.getId()) {
        case R.id.button:
            Intent intent = new Intent(this, Szabaly.class);
            startActivity(intent );
            break;
       }
   }
 } 
gadfil
  • 417
  • 4
  • 13