Reputation: 36
I'm trying to make a button for my app, which will bring the screen to another page. However, I'm not successful in doing so.
I've tried many things, without a reliefing answer.
My project doesn't accept "Intent" in my program. My button I need to open a new layout is called "OptionButton"
Here's what I've got:
in MainActivity.java In the beginning I got this
public class MainActivity extends Activity {
private Button startButton;
private Button pauseButton;
private Button resetButton;
public Button OptionButton;
/** further I got this**/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
/** (I'm just mentioning this because I use savedInstanceState here too)**/
/**MainActivity.java And my code for my button is this **/
OptionButton = (Button) findViewById(R.id.Button1);
OptionButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
myClick(v); /* my method to call new intent or activity */
}
public void myClick(View v) {
Intent intent = new Intent(this, Background2.class);
startActivity(intent);// for calling the activity
}
});
}
}
}
I added this in AndroidManifest:
<activity android:name=".Background2"></activity>
and this in the 2nd class (java file in src map)
(package & imports, then this:
public class Background2 extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
}
}
_ I got 2 classes in src map: -Background2.java -MainActivity.java
Also 2 layout xml's:
In Activity_main, I got this for the button:
<Button
android:id="@+id/Button1"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="35dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:text="@string/OptionButtonLabel"/>
Still it's not working. What am I missing?
Ty all so much!
I've tried changing this:
button1 = (Button) findViewById(R.id.button1); button1.setOnClickListener(this);
But it didn't work.
Upvotes: 0
Views: 4422
Reputation: 36
Thank you for the answers! :)
I've tried installing it on my real device. Now, it opens, but it's the 2nd xml that opens, and when I click the button, it re-opens the same xml.
:-/
I don't get any error messages though when I changed your suggested solutions (from both of you)
Upvotes: 0
Reputation: 1744
You Main Activity has to implement OnClickListner
public class MainActivity extends Activity implements OnClickListener{
From Eclipse IDE press Ctrl+Shift+O it will automatically implement and import necessary functions
Upvotes: 0
Reputation: 44118
Inside of the listener you are calling this
(which refers to the listener itself), while what you want to refer to is the MainActivity
.
Simply change to
Intent intent = new Intent(MainActivity.this, Background2.class);
Upvotes: 2