Hello, I'm a beginner who just started studying development. I'm making a random drawing app for practice, and I'm practicing applying the code I studied in Java for the first time. But I'm confused because there are many reserved words that I can't see in Java that Eclipse uses...
Here's the question.
1. I made the intro screen, but the part next to the main activity column in the manifest did not have an error at first, but when I created another activity, it disappeared, so when I wrote it on the side again, it made an error like that.
Why is it like this? After that, the app will not run on the emulator.
2. This is the random drawing code that I made. I got it from the dice At first, int num = (int) (Math.random() *8) +1; then (see the book) A YouTuber did it differently, so I thought it was different on Android and changed it like this. And the if-else if statement was woven into the middle of the Java class. And the button here is the image button, and the id is set to button. Then, I followed along with the new @SuppressLint ("WrongViewCast"). // If you press the image button, random selection will be performed according to the number selected Each activity is moved to a different screen. Is there a wrong plan??
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
@SuppressLint("WrongViewCast") Button btn1 = (Button) findViewById(R.id.button);
btn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Random dice = new Random();
int num = dice.nextInt(8) + 1;
if(num==1) {
Intent intent = new Intent(MainActivity.this, gawii. class);
startActivity(intent);
} } else if(num==2) {
Intent intent = new Intent(MainActivity.this, money. class);
startActivity(intent);
} } else if(num==3) {
Intent intent = new Intent(MainActivity.this, Bomb. class);
startActivity(intent);
} } else if(num==4) {
Intent intent = new Intent(MainActivity.this, Bird. class);
startActivity(intent);
} } else if(num==5) {
Intent intent = new Intent(MainActivity.this, poo. class);
startActivity(intent);
} } else if(num==6) {
Intent intent = new Intent(MainActivity.this, Airplane. class);
startActivity(intent);
} } else if(num==7) {
Intent intent = new Intent(MainActivity.this, Heart. class);
startActivity(intent);
} } else {
Intent intent = new Intent(MainActivity.this, Hospital. class);
startActivity(intent);
}
}
});
}
}
Please give me some advice(Crying) And even if it's the same Java, it's different enough to learn again when it's used in app development or front-end How should I study? Crying.
android-studio java emulator
1. <activity android:name=".MainActivity" /> </activity>
Here
</activity>
Erase this.
Why did you write Button? If you made it ImageButton, see the view object in ImageButton.
© 2024 OneMinuteCode. All rights reserved.