I'm trying to set a drawable resource as background for my main relative layout via java, but whenever I do it, my app crashes.
Here is the part of the code which doesn't work fine:
public class GameActivity extends Activity {
RelativeLayout layout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
layout = (RelativeLayout) findViewById(R.layout.activity_game);
setContentView(R.layout.activity_game);
layout.setBackgroundResource(R.drawable.image);
}
}
Any idea?
Thanks in advance
Call findViewById() only after setContentView() and supply an identifier in R.id (and not R.layout) so that it can return something other than null.
Remember,you are trying to find a view by id but what you are doing is actually trying to inflate a layout the wrong way.Change the R.layout.activity_game to R.id.activity_game and make sure the relative layout is givent the
android:id = "#+id/activity_game"
The full code should be
public class GameActivity extends Activity {
RelativeLayout layout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game);
layout = (RelativeLayout) findViewById(R.id.activity_game);
layout.setBackgroundResource(R.drawable.image);
}
}
Hope it helps.Happy coding.
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");
}
}
I want to use a view in multiple activities. In my case it's a FloatingActionButton. The moment I am searching for the view via findViewById the program throws a NullPointerException. How do I get access to the FloatingActionButton?
As the button should always be the same, I created a XML Layout only including the FloatingActionButton inside a mergeroot element to reduce overhead when using ìnclude.
floating_button.xml
<merge xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/include_merge" >
<android.support.design.widget.FloatingActionButton
android:id="#+id/addBloodDonation"/>
</merge>
To use this in my other XML Layouts I use the include tag
XML Activitiy Layouts that should include the FloatingActionButton
<include
android:id="#+id/include_merge"
layout="#layout/floating_button" />
That works so far.
To have the same functionality in all my Activities, I created a BaseActivity which my other classes inherit from, e.g. my MainActivity.
BaseActivity
public abstract class BaseActivity extends AppCompatActivity {
public FloatingActionButton fab;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
View include_merge = findViewById(R.id.include_merge);
fab = include_merge.findViewById(R.id.addBloodDonation);
}
}
MainActivity (exemplarily)
public class MainActivity extends BaseActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
When starting the application I receive a NullPointerException in BaseActivity for trying to find the inner element as the View variable include_merge is null.
As I read here the include tag and the root element should have the same android:id. Is there a difference when using a merge as root element? And is it even possible to convert a merge tag into a View.
Do I need to make use of setContentView in the BaseActivity as its onCreate method is called before the one of MainActivity?
Edit:
Added setContentView(R.layout.activity_main); to BaseActivity as mentioned in the comments which still does not fix it.
You don't have to set an id to the <include> or <merge> tag. Just remove it as well as findViewById(R.id.include_merge). The <merge> tag indicates that all its views are added to the container of the <include> tag. Thus there's no View with the id you've set to it. But you can directly find the FAB.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
fab = include_merge.findViewById(R.id.addBloodDonation);
}
I am trying to display a button on my android app but everytime i run the app it crashes. i realise this is because i use setContentView multiple times? I dont understand how it works, and dont understand how i can fix this problem so my button will display. my code is below.
public class MainActivity extends Activity {
Draw draw;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
draw = new Draw(this);
draw.setBackgroundColor(Color.BLUE);
setContentView(draw);
LinearLayout l = new LinearLayout(this);
l.setOrientation(LinearLayout.VERTICAL);
setContentView(l);
l.addView(new Draw(this));
//setContentView(R.layout.activity_main);
setUpBlockBtn();
}
private void setUpBlockBtn(){
setContentView(R.layout.activity_main);
Button addBlockButton = (Button)findViewById(R.id.btnBlock);
addBlockButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.i("DemoButtonApp", "you clicked the button");
//finish();
}
});
}
You try to access Button from android xml layout but you do not set this layout in Activity.
Put you button activity_main.xml and use this button in your activity.
Thanks
You can create one more layout and add Draw and Linear layout to that layout.
Something like this.
LinearLayout l1=new LinearLayout(this);
l1.setOrientation(LinearLayout.VERTICAL);
l1.addView(draw);
l1.addView(l2) // your linearLayout.
setContentView(l1)
Remember you can't use setContentView more than one time.
There should be top layout which includes subview and other layouts and then you can add that layout to your activity.
Problem:
Place the status TextView and the TicTacToe5x5View2D into a vertical LinearLayout and set the layout as the content view of the activity.
I tried to use setGravity; however, it throws me an error. Is there another way to make it a linearlayout?
private TicTacToe5x5Game game;
private TicTacToe5x5View2D gameView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
status = new TextView(this);
status.setGravity(Gravity.CENTER_VERTICAL);
status.setTypeface(Typeface.MONOSPACE);
startGame();
setContentView(status);
gameView = new TicTacToe5x5View2D(this);
}
Any help would be greatly appreciated.
Can someone tell me where I am going wrong?
I'm trying to change my textview called MyText to something else, but when I try I get the error:
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.Window.findViewById(int)' on a null object reference
at android.app.Activity.findViewById(Activity.java:2071)
public class MainScreen extends Activity {
TextView texting = (TextView) findViewById(R.id.MyText);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_screen);
texting.setText("Test");
};
}
This is how your code should look like:
public class MainScreen extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_screen);
// If you're not using this view anywhere, you don't have to declare it globally. Use only a local variables
TextView texting = (TextView) findViewById(R.id.MyText);
texting.setText("Test");
};
}
You should ALWAYS initialize your views after you have your root layout. In this case, right after you did setContentView(R.layout.activity_main_screen); This scenario applies to Activites.
In case you use a fragment you should do this right after you inflated your root layout in the onCreateView method. Something like:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = (LinearLayout) inflater.inflate(R.layout.my_fragment, container, false);
// THIS is where you start initializing your views
return view;
}
The TextView is not available until after setContentView is called. So, findViewById will return null when MainScreen is instantiated. The solution is to move the findViewById call to the onCreate method as follows:
public class MainScreen extends Activity {
TextView texting;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_screen);
texting = (TextView) findViewById(R.id.MyText);
texting.setText("Test");
}
}
Do the following:
public class MainScreen extends Activity {
TextView texting;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_screen);
texting = = (TextView) findViewById(R.id.MyText);
texting.setText("Test");
};
}
You have to initialize your TextView object inside your onCreate method.