I am trying to code an action bar for my android app and i defined my object to set the logo of the action bar to the default andorid icon before i update. When i run just that code, i get an error on the marked line.
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
public class myabDemo extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_myab_demo);
android.support.v7.app.ActionBar actionBar = getSupportActionBar();
actionBar.setLogo(R.drawable.ic_launcher); // ***Error appears here***
actionBar.setDisplayUseLogoEnabled(true);
actionBar.setDisplayShowHomeEnabled(true);
}
}
It's not R.drawable.icon_launcher but R.drawable.ic_launcher (At least i'm pretty sure you followed the tutorial that puts ic_launcher and not icon_launcher)
I guess you received a nullpointer exception, since actionBar is null.
Add the code below:
requestWindowFeature(Window.FEATURE_NO_TITLE);
Related
the monitor of Android Studio do not show Logs.
I have the code:
package com.nv.threadpassdata;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;
import android.content.Context;
import com.google.android.material.snackbar.Snackbar;
public class MainActivity extends AppCompatActivity {
private static final String TAG = "START";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.i(TAG, "Start");
}
}
And the monitor shows NOTHING:
In settings the Log ticks are ON.
I do not use emulator. Instead of emulator I connect my Phone (HUAWEI MYA-L41)
What is the problem? Can anybody find?
Thanks,
Nickolas
Try to do on of the following:
File>Invalidate Caches and Restart
Build>Clean Project then Build>Rebuild Project
I'm just trying to screw around in Android Studio and at the moment, I'm trying to figure out how to get the selected text of a spinner.
Here is my relevant code
package dgameman1.com.emojifixer;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
Spinner emojiSpinner = (Spinner)findViewById(R.id.emojiSpinner);
final String selectedSpinnerText = emojiSpinner.getSelectedItem().toString();
Button installNewEMojis = (Button) findViewById(R.id.installEmojisButton);
installNewEMojis.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Snackbar.make(view, selectedSpinnerText, Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
}
}
The issue I have right now is with this
Spinner emojiSpinner = (Spinner)findViewById(R.id.emojiSpinner);
String selectedSpinnerText = emojiSpinner.getSelectedItem().toString();
'Spinner' is a red color and when I over my mouse over it, I get the error
Cannot resolve symbol 'Spinner'
Cannot resolve symbol 'Spinner'
This means that the class has not been imported properly .
At First import R
import dgameman1.com.emojifixer.R
Then add this in your import section
import android.widget.Spinner;
Then Clean-Rebuild-Restart Your Project .
Let me know if that works. If it doesn't, there are other possible solutions I can add.
you need to import the android.widget.Spinner
change the code if you don't want to import anything.
android.widget.Spinner emojiSpinner = (android.widget.Spinner)findViewById(R.id.emojiSpinner);
I got a very odd problem. I need to create a three tabs in my activiy. Eveything seems fine, but I got the only ine mistake on the same line everytime no matter is it a part if the code either a comment. Here is the code
package com.example.i.actionbar4;
import android.app.ActionBar;
import android.graphics.drawable.Drawable;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTabHost;
public class MainActivity extends FragmentActivity {
// start
private FragmentTabHost mTabHost;
// end
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// start
mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
// Error always there mTabHost.setup(this, getSupportFragmentManager(), android.R.id.tabcontent);
mTabHost.addTab(
mTabHost.newTabSpec("tab1").setIndicator("Tab 1", null),
FragmentTab.class, null);
mTabHost.addTab(
mTabHost.newTabSpec("tab2").setIndicator("Tab 2", null),
FragmentTab.class, null);
mTabHost.addTab(
mTabHost.newTabSpec("tab3").setIndicator("Tab 3", null),
FragmentTab.class, null);
}
//end
So I'm fairly new to Java coding and I'm trying to create a simple code to have an edit text value act as a url for an intent internet command. I'm using this in Eclipse and ADT. The following is my java code.
import android.widget.EditText;
import android.widget.Button;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View.OnClickListener;
import android.view.View;
import android.content.Intent;
import android.net.Uri;
public class MainActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button2 = (Button) findViewById(R.id.button2);
button2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
EditText UR_L=(EditText) findViewById(R.id.ur_l);
String sUR_L= new String(UR_L.getText().toString());
Intent brwsrintnt = new Intent(Intent.ACTION_VIEW, Uri.parse(sUR_L));
startActivity(brwsrintnt);
}
});
In my layout I have a 2 buttons and an edit box but the edit box reflects a missing input type error. I have no idea how to fix this and I've looked many places.
Thanks,
David
I think your problem might be that the EditText is never intialized until you have already hit the button, so when you go to get text from it, it doesn't know the user has already entered something. Try moving
EditText UR_L=(EditText) findViewById(R.id.ur_l);
to just before you set the OnClickListener and see if anything changes
Hoping to get into android app development so I'm doing some basic tutorials just now.
Just trying to get comfortable with the basics at the moment, one of which is using the Typeface class.
package org.me.myandroidstuff;
import android.os.Bundle;
import android.app.Activity;
import android.graphics.Typeface;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class HelloWorldActivity extends Activity implements OnClickListener
{
private View mainView;
private TextView tbox1;
private Button exitButton;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mainView=(View)findViewById(R.id.mainView);
mainView.setBackgroundColor(getResources().getColor(R.color.silver));
tbox1 = (TextView)findViewById(R.id.textBox1);
tbox1.setTypeface(Typeface.MONOSPACE);
}
}
The line
tbox1 = (TextView)findViewById(R.id.textBox1);
Has a red cross beside it (I'm using eclipse) with the error
tbox1 cannot be resolved
Its been a while since i have used java, but as i aware the following code
create a new TextView object called tbox1
Assigns the tbox1 object the id specified in the xml for the TextView tag in an external main.xml
Then tbox1 executes the setTypeFace() method on itself?
Obviously I'm going wrong somewhere, any ideas? Something really simple no doubt...
You can't inform us about one error and neglect the others. Look at your code.
Besides what user370305 said, you have other problems. Namely, your Activity, according to the contract, implements OnClickListener but does not override the necessary onClick(View v) method. You must add it for the contract to be met.
So your code should look like:
package org.me.myandroidstuff;
import android.app.Activity;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class HelloWorldActivity extends Activity implements OnClickListener {
private View mainView;
private TextView tbox1;
private Button exitButton;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mainView=(View)findViewById(R.id.mainView);
mainView.setBackgroundColor(getResources().getColor(R.color.silver));
tbox1 = (TextView)findViewById(R.id.textBox1);
tbox1.setTypeface(Typeface.MONOSPACE);
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
}
Remember, you can't talk about errors until you fix every other that might cause other errors to be falsely reported.
First try to set setContentView(R.layout.yourlayoutfilename); in onCreate().
1.) Delete line super.onCreate(savedInstanceState);
2.) Retype super.onCreate(savedInstanceState);
3.) Clean the Project
4.) Build the Project