Cannot get a simple Hello World app to compile - Android Q&A, Help & Troubleshooting

Ok, so I'm a very newbe programmer. I do know a little C++. But I wanted to see if I could create a basic Hello World android app. I found a video on YouTube that I'm trying to follow along. I'm pretty sure my code is identical to his code. But mine won't compile.
Here's the code...
package com.example.simplehelloworldapplication;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends AppCompatActivity {
private TextView textView;
private Button button;
@override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView=findViewById(R.id.text_view);
button=findViewById(R.id.Button_click);
button.setOnClickListener(new View.OnClickListener() {
boolean visible;
@override
public void onClick(View v) {
visible = !visible;
button.setVisibility(visible? View.VISIBLE:View.VISIBLE);
}
});
}
}
When I attempt to compile the code, I get 3 errors...
error: incompatible types: no unique maximal instance exists for type variable T with upper bounds Button,View
(It's refering to this error: button=findViewById(R.id.Button_click)
error: cannot find symbol method setVisibility(int)
error: cannot find symbol method setOnClickListener(<anonymous OnClickListener>)
Any suggestions to fix these errors would be greatly appreciated.

Ok, so I figured it out. Not from the code above, but I found training on androids website that showed me how to do it and it worked. I now have an app on my phone I created that asks for input and then sends it (to nowhere)

Related

[Q] My app keeps crashing on Eclipse

hey guys,
I started learning android development and I've built a simple app that suppose to display a background and a few buttons that do nothing (for now). but I cant seem to get this app to work on the emulator w/o crashing. I've tried to look for an error for hours, but nothing came up.
I was hoping you could help me trace my problem. here's the code:
package com.reflecti0n.imri;
import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
public class MainMenu extends Activity implements View.OnClickListener {
ImageButton bStartGame, bHowToPlay, bQuitGame;
MediaPlayer soundtrack;
int counter;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
setVar();
soundtrack = MediaPlayer.create(MainMenu.this, R.raw.soundtrack);
soundtrack.start();
bStartGame.setOnClickListener(this);
bHowToPlay.setOnClickListener(this);
bQuitGame.setOnClickListener(this);
}
public void setVar(){
bStartGame = (ImageButton) findViewById(R.id.bStart);
bHowToPlay = (ImageButton) findViewById(R.id.bHowTo);
bQuitGame = (ImageButton) findViewById(R.id.bQuit);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
counter++;
}
}
Click to expand...
Click to collapse
the code is updated in the manifest as the 2nd activity class to work after the splash.
splash code:
package com.reflecti0n.imri;
import android.os.Bundle;
import android.content.Intent;
import android.app.Activity;
public class Splash extends Activity {
@Override
protected void onCreate(Bundle Reflection) {
// TODO Auto-generated method stub
super.onCreate(Reflection);
setContentView(R.layout.splash);
Thread timer = new Thread(){
public void run(){
try{
sleep(3000);
}catch(InterruptedException e){
e.printStackTrace();
}finally{
Intent ourIntent = new Intent("com.reflecti0n.imri.MAINMENU");
startActivity(ourIntent);
}
}
};
timer.run();
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
finish();
}
}
Click to expand...
Click to collapse
thanks alot!
Before digging into the code you need to be sure that your development environment was installed correctly by running Hello, World program (developer.android.com/resources/tutorials/hello-world.html).
So have you tried it first?
Sent from my GT-I9003 using Tapatalk
eduCoder said:
Before digging into the code you need to be sure that your development environment was installed correctly by running Hello, World program (developer.android.com/resources/tutorials/hello-world.html).
So have you tried it first?
Sent from my GT-I9003 using Tapatalk
Click to expand...
Click to collapse
Yes, I have tried it. I've been running several programs on my environment before I started writing this app...
The thing is according to Eclipse there's nothing wrong with the syntax, But when I try to run it, it crashes.
I've tried to isolate the problem, and when I remove the StartActivity to the MainMenu from the splash it runs the slash app w/o a problem.
I also double-checked the Manifest to make sure it's set correctly (splash is LAUNCHER/MAIN, mainmenu is DEFAULT/SPLASH).

[Q] Linking Strings between Activities and WebViews

Hello XDA,
So I've not found anyone properly discussing this. If they do, please link me to the page and don't flame
I've been making an app for my school and so far, I've been doing alright. But now I have run into a problem which seems to be very hard to solve for me alone.
Namely, I have an EditText and a Button in one Activity and a WebView as a different Activity.
I'm trying to get the EditText String into the WebView URL (String being variable, Button initiating the WebView), but get a lot of errors, some not even related to that.
I would really appreciate it if someone could have a look at the code and help me with this
MyWebView.java:
Code:
package bas.sie.Antonius;
import android.app.Activity;
import android.os.Bundle;
import android.widget.EditText;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class MyWebView extends Activity {
WebView mWebView;
EditText mEtxtStudentNum;
static String StudentNumFromHome = bas.sie.Antonius.Home.StudentNum;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.webview);
mWebView = (WebView) findViewById(R.id.webview);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.setWebViewClient(new WebViewClient());
mWebView.loadUrl("http://carmelcollegegouda.nl/site_ant/roosters/standaardroosters/Lee1_" + StudentNumFromHome + ".htm");
}
}
Home.java (Homescreen):
Code:
package bas.sie.Antonius;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class Home extends Activity {
Button mBtnStSchedule;
static EditText mEtxtStudentNum;
static final String StudentNum = mEtxtStudentNum.getText().toString();
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button next = (Button) findViewById(R.id.BtnStSchedule);
next.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent myIntent = new Intent(view.getContext(), MyWebView.class);
startActivityForResult(myIntent, 0);
}
});
// TODO Auto-generated method stub
}
}
AntoniusActivity.java (Main):
Code:
package bas.sie.Antonius;
import android.app.TabActivity;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.KeyEvent;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.TabHost;
public class AntoniusActivity extends TabActivity {
WebView mWebView;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Resources res = getResources(); // Resource object to get Drawables
TabHost tabHost = getTabHost(); // The activity TabHost
TabHost.TabSpec spec; // Resusable TabSpec for each tab
Intent intent; // Reusable Intent for each tab
// Create an Intent to launch an Activity for the tab (to be reused)
intent = new Intent().setClass(this, Home.class);
// Initialize a TabSpec for each tab and add it to the TabHost
spec = tabHost.newTabSpec("home").setIndicator("Home",
res.getDrawable(R.drawable.ic_tab_home))
.setContent(intent);
tabHost.addTab(spec);
// Do the same for the other tabs
intent = new Intent().setClass(this, External.class);
spec = tabHost.newTabSpec("external").setIndicator("External",
res.getDrawable(R.drawable.ic_tab_external))
.setContent(intent);
tabHost.addTab(spec);
intent = new Intent().setClass(this, Contact.class);
spec = tabHost.newTabSpec("contact").setIndicator("Contact",
res.getDrawable(R.drawable.ic_tab_contact))
.setContent(intent);
tabHost.addTab(spec);
tabHost.setCurrentTab(2);
}
private class HelloWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK) && mWebView.canGoBack()) {
mWebView.goBack();
return true;
}
return super.onKeyDown(keyCode, event);
}
}
In AntoniusActivity, the private class HelloWebViewClient is underlined with yellow (never used), specifically the bit "HelloWebViewClient".
It's also throwing FC's at startup, and errors in LogCat, but I'll post those later on, as it's 10.30 PM here, and school goes on
Thanks in advance,
bassie1995
No suggestions yet?
How you doin'? Greetings from my GT-I9000!

[Q]ERROR: Void is an invalid type for the variable

I just started developing apps and I've come across the error: Void is an invalid type for the variable button1Click. I got the error on the following line of code
public void button1Click; {
It's in my java class, I'm trying to open an activity from another. This is my entire code looks like this
package trial.play;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.app.Activity;
import android.content.Intent;
public class Food extends Activity implements View.OnClickListener {
Button button1;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.singer);
button1 = (Button) findViewById(R.id.button1);
button1.setOnClickListener(this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// TODO Auto-generated method stub
return super.onCreateOptionsMenu(menu);
}
public void button1Click; {
startActivity(new Intent("android.intent.action.TESTMENUACTIVITY"));}
public void onClick(View view) {
// TODO Auto-generated method stub
switch (view.getId()) {
case R.id.button1:
button1Click: break;
}
}
public void Enter(View view) {
Intent openTestMenuActivity = (new Intent(
"trial.play.TESTMENUACTIVITY"));
startActivity(openTestMenuActivity);
}
}
I'm still new to development so be specific and show me what you mean if you can.
Thanks

[Q] Some problem with my code

Hi, I having some problem with this simple code. I don't undestrand because ADT impost fields as EditText, TextView and Button as field final. I think that there' some errore in my code, I admit that is one of my first times that i programming for andoird, so have patience with me.
I apologize if I wrong section :silly:
That's my code:
package com.example.buttoncerca;
import android.os.Bundle;
import android.app.Activity;
import android.text.Editable;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class RisponditoreInterattivoActivity extends Activity implements Lista {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_risponditore_interattivo);
final EditText text = (EditText)this.findViewById(R.id.campoNome);
Button button = (Button)this.findViewById(R.id.buttone);
final TextView tv = (TextView)this.findViewById(R.id.testoSaluto);
button.setOnClickListener(new OnClickListener(){
public void onClick(View arg0){
Editable nome = text.getText();
String s = nome.toString();
for(int c =0; c<3; c++){
if(toppings[c].equals(s)){
tv.setText(toppings[c] + " si butta nel " + cestino[c]);
} //End if
}//End for
}//End onClick
});
}
}
Click to expand...
Click to collapse
I implements inteface Lista, this is code:
package com.example.buttoncerca;
public interface Lista {
String[] toppings = {"Plastica", "Legno", "Cartone"};
String[] cestino = {"Cestino plastica", "Cestino legno", "Cestino cartone"};
}
Click to expand...
Click to collapse
And... what's the problem? Please post it "as is".
You need to instantiate the editable first, with BUFFERTYPE.EDITABLE, because in your current case you are casting a charseq to an editable so that's likely the exception you are getting (hard to know without a logcat). See here:
http://developer.android.com/reference/android/widget/EditText.html#getText()
Sent from my Amaze 4G using xda app-developers app

Media Player constructor

Hello guys, I've just start to write simple android apps and I get stuck with this problem. I have an activity with one button (startPlay) and I want to play a mp3 file (a1.mp3) from raw directory when the button is clicked. When I try to run the app on virtual device I get this error:
Error22, 25) error: constructor MediaPlayer in class MediaPlayer cannot be applied to given types;
required: no arguments
found: MainActivity,int
reason: actual and formal argument lists differ in length
Click to expand...
Click to collapse
Here is my code:
Code:
import android.media.MediaPlayer;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Button;
import android.view.View;
import android.view.View.OnClickListener;
public class MainActivity extends AppCompatActivity implements OnClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = (Button) findViewById(R.id.startPlay);
button.setOnClickListener(this);
}
public void onClick(View v) {
MediaPlayer m = new MediaPlayer(this,R.raw.a1);
m.start();
}
}

Categories

Resources