for some reason the run method here has no trouble setting the content view, but when i try to retrieve the button from the xml it returns null and does not allow me to set the onclick listener. my code is listed here below.
v.post(new Runnable() {
@Override
public void run() {
setContentView(R.layout.win);
Button submit = (Button) findViewById(R.id.submit);
submit.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
HighScoresHelper scores = new HighScoresHelper("HighScoreV2.txt", GameActivity.this);
scores.addScore("Test", Math.round((frame/FPS) *100)/100.0);
startActivity(new Intent(GameActivity.this, HighScores.class));
finish();
}
});
}
});
and yes, the spelling and everything is all correct and matches up with the xml, here is the xml code
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="YOU WON!!!, enter in your name and click submit"
android:id="@+id/textView3" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/name" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Submit"
android:id="@+id/submit"
android:layout_gravity="center_horizontal" />
</LinearLayout>
Thank you
So, the problem was that I was calling this inside the inner-class to the activity. What I needed to do was inflate the view and save it as a global variable. I did all of this inside the onCreate method.
LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
winView = inflater.inflate(R.layout.win, null);
submit = (Button) winView.findViewById(R.id.submitScore);
submit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
and then inside my inner class I called
v.post(new Runnable() {
@Override
public void run() {
setContentView(winView);
}
});
to change the view when necessary, v was the inner-class instance variable of the main class.
Just to clarify, main class - "GameActivity" and the inner-class was labeled "OurView". v was a global varible of type OurView inside the class GameActivity. I would post the entire class, but its 300+ lines long.
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.