I am new to Android programming. When my app starts it crashed.maybe this is a loop problem.app works fine when i remove code which get source from page
is it possible to get page source inside loop, for example every minute?
This is one of the most frequent errors observed here
With this line
TextView mTextView = (TextView) findViewById(R.id.textView3);
you are trying to access a view before the content view has been set. So you'll have to move this line like
private MediaPlayer mediaPlayer;
TextView mTextView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_security_system);
mTextView = (TextView) findViewById(R.id.textView3);
Related
lately, I was working on a very small training project.
But there is one error in my code:
public class MainActivity extends AppCompatActivity {
TextView textView = (TextView) findViewById(R.id.textview);
#override
protected void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView.setText("Welcome");
}
}
but when I tried to cut the textview global variable and paste it inside the onCreate method, the error has gone. Why is this error occur although I already have inflated the textView in a global variable ?!
You need to call findViewById(...) after calling setContentView(...).
Fetching the view before inflating the layout isn’t possible since there is no view.
findViewById() should only be used after setContentView() in onCreate.
That's where your entire layout get inflated. Only then you can access views using findViewById().
This is what you want:
public class MainActivity extends AppCompatActivity {
TextView textView;
#override
protected void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = (TextView) findViewById(R.id.textview);
textView.setText("Welcome");
}
}
So I have two objects on my activity, a TextView, and a Button. I did change the font of both of these using a font style from my assets folder. The project would load fine no problems. However now all of a sudden changing the TextView font makes the game crash and not load. I can't understand what could have caused this and how to solve it other than maybe using a button to display text, which is in practical.
My java code:
public class Main_Menu extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main__menu);
Button n=(Button) findViewById(R.id.start_button);
Typeface typeface = Typeface.createFromAsset(getAssets(), "Stencil WW.ttf");
n.setText("Start");
n.setTypeface(typeface);
Typeface typeface2 = Typeface.createFromAsset(getAssets(), "Stencil WW.ttf");
TextView title = (TextView) findViewById(R.id.title);
title.setTypeface(typeface2);
//sets screen orientation on created
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
MediaPlayer mMediaPlayer = new MediaPlayer();
mMediaPlayer = MediaPlayer.create(this, R.raw.sound1);
mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mMediaPlayer.setLooping(true);
mMediaPlayer.start();
}
}
I did use the typeface to change both texts fonts but I tried just making it again with typeface2 and it still crashes. Not sure if I need to show any other part of my project but will if you wish to see it. Thank you for the help.
It seems like you forgot "setText" to the title
This question already has answers here:
How do I declare a TextView variable so that I can use it anywhere?
(2 answers)
Closed 6 years ago.
I am still a Java beginner and I have the problem that I have a onClick function and of course the onCreate in which i define the TextView to a value. But when I want to edit the TextView from the onClick i can't do that. Now I would like to know how I can make the TextView value "global".
final TextView result = (TextView) findViewById(R.id.result);
You have to instantiate the TextView after the setcontentView() method :
public class MainActivity extends Activity {
TextView textView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = (TextView)findViewById(R.id.textview);
}
}
You can define your TextView above method onCreate.
TextView result;
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
setContentView(R.layout.yourLayout);
TextView result = (TextView) findViewById(R.id.result);
}
In your class you create an attribute:
private Textview tv;
then in onCreate method you initialise this variable like this :
tv = (TextView) findViewById(R.id.result);
Then you can use it wherever you want in your class by calling "tv".
I am learning how to develop apps for android.
I followed the tutorial by Google Building Your First App.
The idea is basically to write a message in a fragment, submit it with a button, the message is passed through the intent and displayed in the second fragment.
My question is about the part where we display the message. This is how it was done in the tutorial:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Get the message from the intent
Intent intent = getIntent();
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
// Create the text view
TextView textView = new TextView(this);
textView.setTextSize(40);
textView.setText("You submitted: "+message);
// Set the text view as the activity layout
setContentView(textView);
}
I learned that the UI components can be implemented using XML, or Java, in this case obviously the tutorial uses Java to make a new TextView with the data received through the intent. I was trying to redo that part, but this time without relying that much on Java when it comes to the view. So I replaced the part under // Create the text view by:
TextView textView = (TextView) findViewById(R.id.display_message);
textView.setText(message);
And in the fragment_display_message.xml I had the following element:
<TextView
android:id="#+id/display_message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello_world" />
It throws a NullPointerException at textView.setText(message);
I wonder why this happens, why can't it be done this way?
i think you should call
setContentView(R.layout.fragment_display_message);
before you call
TextView textView = (TextView) findViewById(R.id.display_message);
textView.setText(message);
but it's a fragment?
I have some problems passing and displaying values from the MainActivityClass to another MyActivityClass. So I learned here how to pass values from one class to another. Here's the method of MainActivity class which contains the intent to the other activtiy:
Intent intent = new Intent(this, AddAptActivity.class);
Bundle extras = new Bundle();
EditText editText = (EditText) findViewById(R.id.edit_address);
String message = editText.getText().toString();
EditText editText2 = (EditText) findViewById(R.id.edit_name);
String message2 = editText2.getText().toString();
extras.putString(EXTRA_MESSAGE, message);
extras.putString(EXTRA_MESSAGE1, message2);
intent.putExtras(extras);
startActivity(intent);
and then in MyActivityClass receiving the values:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = getIntent();
Bundle extras = intent.getExtras();
String message = extras.getString(MainActivity.EXTRA_MESSAGE);
String message2 = extras.getString(MainActivity.EXTRA_MESSAGE2);
TextView textView = new TextView(this);
textView.setTextSize(40);
textView.setText(message);
TextView textView2 = new TextView(this);
textView2.setTextSize(40);
textView2.setText(message2);
setContentView(textView, textView2);
}
but the setContentView() method doesn't want to accept more than one value. How can I display the other value in another textview and then display it???
I think I don't understand how to display values with setContentView... Please help me, as I am new in Android programming (just finished the 1st tutorial one week ago).
You can use a ViewGroup such as a LinearLayout or RelativeLayout then add TextViews to the same and then setContenView(linearlayout);
LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.VERTICAL);
TextView textView = new TextView(this);
TextView textView2 = new TextView(this);
ll.addView(textView);
ll.addView(textView2);
setContenView(ll);
setContentView() takes one Layout, either a View or a layout resource ID. You can pass in a LinearLayout, RelativeLayout, etc. to setContentView() which can contain any number of views. Probably a better approach would be to build a layout XML file that contains your view higherarchy then set it using setContentView(R.layout.someLayout);
see more info here:
http://developer.android.com/guide/topics/ui/declaring-layout.html
You can only add a view to your setContentView(...). Best create a new View that contains both TextView and then setContentView to this new View.
RelativeLayout layout= new RelativeLayout(this);
layout.addView(textView1);
layout.addView(textView1);
setContentView(layout);
Several things wrong here so let's start from the beginning...
but the setContentView() method doesn't want to accept more than one value. There is a method of it which takes a second param but that is for layout params.
Nope, if you look at the docs that's all it takes and you can't force it to take more (without rewriting the activity class).
I think I don't understand how to display values with setContentView.
I don't think so either but you will.
You just set a layout (typically) with this method. You are creating Views dynamically which is ok but you don't need to. If you do then you need to add them to your inflated layout (the one you set in setContentView()). You can just add them to your layout (someLayout.xml) then get a reference to them
So it may look something like
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/*
* call this first and it will save you some grief
*/
where someLayoutFile is the xml file without the .xml extension
setContentView(R.layout.someLayoutFile);
Intent intent = getIntent();
Bundle extras = intent.getExtras();
String message = extras.getString(MainActivity.EXTRA_MESSAGE);
String message2 = extras.getString(MainActivity.EXTRA_MESSAGE2);
TextView textView = (TextView) findViewById(R.id.someID); // where someId is the id you give a TextVIew in your xml file
textView.setTextSize(40);
textView.setText(message);
// create another TextView in your xml and do the same with that using the appropriate layout
See this page in the docs for more information