I'm trying to configure AdMod in my first Android project. When I try to run the app, findViewByID always returns null.
Here's the code of my main activity:
import android.app.Activity;
import android.content.Context;
import android.content.res.Configuration;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.os.Bundle;
import android.os.PowerManager;
import android.os.PowerManager.WakeLock;
import android.view.Window;
import android.view.WindowManager;
public abstract class MyAndroidApp extends Activity {
AdView adView;
public static final String MY_PUBLISHER_ID = "***********";
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
AdRequest adRequest = new AdRequest();
adRequest.addTestDevice(AdRequest.TEST_EMULATOR);
adView = new AdView(this, AdSize.BANNER, MY_PUBLISHER_ID);
//R.id.main has value, I can see it in Eclipse
RelativeLayout mainLayout = (RelativeLayout) findViewById(R.id.main);
if ( mainLayout == null )
Log.d("Error","Layout is null");
else
mainLayout.addView(adView);
adView.loadAd(adRequest);
The XML File (under res\layout) has the following:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MyApp"
android:id="#+id/main">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="#string/hello_world" />
</RelativeLayout>
When I debug, I can see value for R.id.main. However, the value of mainLayout is still null.
Thanks in advance.
Problem is that you are not calling setContentView() in onCreate() method:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layoutId);
RelativeLayout mainLayout = (RelativeLayout) findViewById(R.id.main);
}
Without it you will get always NPE because UI elements don't have created instances(purpose of setContentView method) and View returned by findViewById() is assigned to NULL.
So add this one line and it should work.
The setContentView method should be called with appropriate layout before calling findViewById. It is usually called in onCreate(Bundle savedInstance) method.
Related
I'm trying to get a ListView to show the contents of a simple array but my program crashes at the setAdapeter line. As I understand this is supposed to be because the final value comes out as null but I don't see why that would be the case or how to fix it. I have already tried using ArrayAdapeter instead as well as catching the null exception with a try block. The former changes nothing and the latter simply shows a blank activity with no list to speak of. Please help.
Here's Main Activity
package ballton.fowdeckbuilder;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ListAdapter;
import android.widget.ArrayAdapter;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btnDecks = (Button) (findViewById(R.id.btnDecks));
Button btnCards = (Button) (findViewById(R.id.btnCards));
btnDecks.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, DeckCatalogue.class));
}
});
btnCards.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, CardCatalogue.class));
}
});
}
}
Here's the activity with the Problem
package ballton.fowdeckbuilder;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
public class DeckCatalogue extends AppCompatActivity {
ListView Ldecks;
String TAG = "TAG";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_deck_catalogue);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
Ldecks = (ListView) (findViewById(R.id.DeckList));
String Decks[] = new String[] {"Faria","Melgis"};
ListAdapter feed = (new ArrayAdapter<String>(this, R.layout.show_deck, Decks));
Ldecks.setAdapter(feed);
}
}
Here's the Layout for that activity
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="ballton.fowdeckbuilder.DeckCatalogue"
tools:showIn="#layout/activity_deck_catalogue">
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/DeckList"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
And Here's the layout XML I use for the list times
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Small Text"
android:id="#+id/Deck" />
</LinearLayout>
Use this code instead of yours:
ListAdapter feed = (new ArrayAdapter<String>(this, R.layout.show_deck, R.id.Deck, Decks));
This happened because you must supply the resource ID (as your xml: R.id.Deck) for the TextView you used for ListView.
I'm relatively new to android development and I'm trying to find a way to inflate a view repeatedly each time when a button is pressed, in a different location, so every inflated view has its own position:
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;
public class teamCreateScreen extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.team_locate_layout);
}
public void createTeam(View view) {
final RelativeLayout rlTeam = (RelativeLayout) findViewById(R.id.rlTeam);
View teamBox = View.inflate(this, R.layout.team_box, rlTeam);
final TextView teamBoxView = (TextView) findViewById(R.id.team_task_box);
teamBoxView.setX(0);
teamBoxView.setY(230);
}
}
The XML code of the layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/rlTeam">
<Button
android:layout_width="60dp"
android:layout_height="60dp"
android:id="#+id/teamAddBtn"
android:text="+"
android:textSize="30sp"
android:onClick="createTeam"/>
</RelativeLayout>
XML code of the view that's being inflated:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="192dp"
android:layout_height="120dp"
android:id="#+id/team_task_box"
android:text="New Team" />
</RelativeLayout>
I want to use the same view to inflate multiple boxes with different coordinates in the layout. Every time I press the button to inflate the view again it inflates the box in the same coordinates so they overlap. I need to make the second box to appear to the first one's right, the third below 1st and so on, much like a grid of boxes.
Try this code and tell me whether it works. Remove the inflating of layout
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;
public class teamCreateScreen extends Activity {
int i=0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.team_locate_layout);
}
public void createTeam(View view) {
final RelativeLayout rlTeam = (RelativeLayout) findViewById(R.id.rlTeam);
RelativeLayout.LayoutParams relativeParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
TextView tv=new TextView(getApplicationContext());
if(tv.getId()>0) {
relativeParams.addRule(RelativeLayout.BELOW, tv.getId());
}
tv.setId(i);
r1Team.addView(tv, relativeParams);
i++;
}
}
Declare int i=0; as a global variable and increment it in the createTeam() method.
I have a TextView in my app and I want to change its textSize programmatically using the setTextSize method, but everytime I ran my code, I got an error message : "java.lang.NullPointerException". I tried to print the value of the TextView and it got NULL value.
Here's my code:
LoadActivity.java
import android.app.Activity;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;
public class LoadActivity extends Activity{
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView text = (TextView) findViewById(R.id.loadTextView);
try{
System.out.println("TextView: "+text);
}catch(Exception e){
System.err.println(e);
}
text.setTextSize(30);
setContentView(R.layout.activity_load);
}
}
activity_load.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffffff">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Please Wait"
android:id="#+id/loadTextView"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:typeface="monospace"
android:textSize="35sp"
android:textStyle="bold"
android:textColor="#000000"/>
</RelativeLayout>
What do you think is the problem?
setContentView(R.layout.activity_load); should be declared before initializing your views(TextView) and System.out.println("TextView: "+text); is not correct form to print value. The correct form is System.out.println("TextView: "+text.getText().toString());
So your corrected code will be like this
import android.app.Activity;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;
public class LoadActivity extends Activity{
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_load);
TextView text = (TextView) findViewById(R.id.loadTextView);
try{
System.out.println("TextView: "+text.getText().toString());
}catch(Exception e){
System.err.println(e);
}
text.setTextSize(30);
}
}
Use this in your onCreate Method
setContentView(R.layout.activity_load);
TextView text = (TextView) findViewById(R.id.loadTextView);
text.setTextSize(30);
In order for you to find views by their id, you need to write
setContentView(R.layout.activity_load);
This is because it essentially gets the layout resource file that defines your UI, and then allows you to retrieve other views within that layout (with findViewById();)
Look here for more information on how to properly set up Activites
import android.app.Activity;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;
public class LoadActivity extends Activity{
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_load); // Retrieves layout that defines your UI
// After setContentView is called, you can find views within the layout
TextView text = (TextView) findViewById(R.id.loadTextView);
try{
System.out.println("TextView: "+text.getText().toString());
}catch(Exception e){
System.err.println(e);
}
text.setTextSize(30);
}
}
I am trying to add an ad to my application and I get the error when I write this code :
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.google.ads.AdRequest;
import com.google.android.gms.ads.AdView;
public class isPrime extends Activity {
Button mButton = null;
TextView resultText = null;
EditText mInput = null;
RelativeLayout myLayout = null;
long input ;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Look up the AdView as a resource and load a request.
AdView adView = (AdView)this.findViewById(R.id.adView);
AdRequest adRequest;
adRequest = new AdRequest.Builder().build();
adView.loadAd(adRequest);
i.e the error I get is
Builder cannot be resolved into a Symbol
I have read through all the documentation(well at least I think I have.) I am totally new to this field. I would appreciate any help.
Also my XML code is :
<com.google.android.gms.ads.AdView android:id="#+id/adView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
ads:adSize="BANNER"
ads:adUnitId="blahblah"/>
You are using the wrong import for AdRequest looks like it is looking at the old admob implementation. Use --
import com.google.android.gms.ads.AdRequest;
instead of
import com.google.ads.AdRequest;
I am working in android, i want to make a scrollview.
this is my code for that :
package com.pericent;
import android.app.Activity;
import android.app.TabActivity;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.util.Log;
import android.view.ViewGroup.LayoutParams;
import android.widget.HorizontalScrollView;
import android.widget.LinearLayout;
import android.widget.TabHost;
import android.widget.TextView;
public class HelloTabWidget extends Activity {
private String TAG="HelloTabWidget";
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.v(TAG,"i am just before everything");
HorizontalScrollView hr=new HorizontalScrollView(this);
hr.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));
LinearLayout layout=new LinearLayout(this);
LinearLayout mainlayout=new LinearLayout(this);
mainlayout=(LinearLayout)findViewById(R.id.upper1);
layout.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
Log.v(TAG,"i am just after the declarations");
for(int i=0;i<100;i++){
TextView txt=new TextView(this);
txt.setText("Text " + i );
layout.addView(txt);
}
Log.v(TAG,"i am after the for loop");
hr.addView(layout);
mainlayout.addView(hr);//<<---this is creating NullPointerException
setContentView(R.layout.cecking);
Log.v(TAG,"i am after the everything");
}
}
and this my cecking.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent" android:id="#+id/upper1">
</LinearLayout>
whenever i run the program, this error is occurred:-
Unable to start activity ComponentInfo{com.pericent/com.pericent.HelloTabWidget}: java.lang.NullPointerException
and in this line the error is occurred:- mainlayout.addView(hr);
please help me to find out the reason of error.
Thank you in advance.
You are not setting any layout for the Activity
as well as you are not Inflating checking.xml for that Activity.
That's why mainlayout cannot be found and cause NullPointerException
Try this way:
checking.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent" android:id="#+id/upper1">
</LinearLayout>
</LinearLayout>
and then set your layout inside onCreate() of the Activity
like this:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContenView(R.layout.checking);
mainlayout=(LinearLayout)findViewById(R.id.upper1);
}
This might be because you haven't set
.setLayoutParams(new Layout...
for mainlayout, the error statement line object.