Android App Null Pointer on Specific Textview - android

Normally null pointer exceptions seem to be view related - where the wrong layout is targeted.
This is different I reckon. I have four textviews in a layout and one returns a null, the rest work fine. Here is the layout:
<?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:orientation="vertical" >
<TextView
android:id="#+id/text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="hello" />
<TextView
android:id="#+id/text2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="stringello2" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="ip"
android.id="#+id/iptest"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="hostname"
android:id="#+id/hostname"
/>
</LinearLayout>
And here is the test code:
public class MainActivity extends Activity
{
/** Called when the activity is first created. */
protected TextView text;
protected TextView ip;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
text = (TextView) findViewById(R.id.text);
text.setText("goodbye");
text = (TextView) findViewById(R.id.hostname);
text.setText("hostname flibble");
// text = (TextView) findViewById(R.id.text2);
text = (TextView) findViewById(R.id.iptest);
text.setText("ip flibble");
}
}
If I switch the comment to the other textview, it works fine. If I target iptest it returns null and raises an exception.
Any ideas why? All four appear in gen and they all reappear if I delete gen and recompile.

in your TextView Tag
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="ip"
android.id="#+id/iptest"
/>
you had taken a .(dot) instead of :(colon)
android.id="#+id/iptest"
shoulb de like this
android:id="#+id/iptest"
Moreover please clean your project regularly.

your R class is not holding iptest reference . android.id="#+id/iptest" is wrong . It should be android:id

Related

Difference between Initialization of ListView and TextView in Fragment onActivityCreated() function

When I initialize ListView inside OnActivityCreated() method of a class that extends Fragment as given below :
ListView listView=(ListView)getActivity().findViewById(R.id.list);
then it works fine but when i do same thing for the TextView i.e initialize textView in same way inside the same method of class that extends Fragment
textView = (TextView)getActivity().findViewById(R.id.textView);
then my app crashes and log shows null pointer exception . I want to know the reason behind this behavior . !! Xml Code is Given Below :
<?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"
>
<ListView
android:id="#+id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="36dp"
android:text="some tect"
android:textSize="28sp" />
</RelativeLayout>
What you are doing is wrong.
Just do
#Override
public void onViewCreated(View view, #Nullable Bundle savedInstanceState) {
ListView listView=(ListView)view.findViewById(R.id.list);
textView = (TextView)view.findViewById(R.id.textView);
}
Initialize all view as above.

TextView gets overlaped(instead of Overwriting)in a Preference Screen

I am trying to build a preference screen which consists of a Layout with some TextViews and EditText Preference.
When I use textView.setText(inputString) in the Activity, it does not overwrite the existing TextView, instead it creates a new copy of TextView with inputString as text and it overlaps with the existing TextView. Is there any way that the setText overwrites the existing TextView itself.
This is the XML Layout with the TextView
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:background="#color/blue"
android:layout_height="match_parent">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#android:id/list"
></ListView>
<TextView
android:text="TextView"
android:layout_width="wrap_content"
android:textColor="#color/pseudoBlack"
android:layout_height="wrap_content"
android:layout_marginStart="59dp"
android:layout_marginTop="100dp"
android:id="#+id/textView2"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true" />
<TextView
android:text="TextView"
android:textColor="#color/pseudoBlack"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/textView5"
android:layout_alignBaseline="#+id/textView2"
android:layout_alignBottom="#+id/textView2"
android:layout_toEndOf="#+id/textView2"
android:layout_marginStart="100dp" />
<TextView
android:text="TextView"
android:textColor="#color/pseudoBlack"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="43dp"
android:id="#+id/textView11"
android:layout_below="#+id/textView2"
android:layout_toEndOf="#+id/textView2"
android:layout_marginStart="21dp" />
</RelativeLayout>
And this is the Preference Screen I am using.
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
>
<Preference android:layout="#layout/inner" android:key="innerkey"
/>
<Preference android:key="mskey"
android:title="show"
/>
<EditTextPreference android:key="editpref"
android:title="Add"
/>
</PreferenceScreen>
This is the Activity Class in which I am trying to do setText().
package com.example.asus.layoutcheck;
import android.os.Bundle;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.widget.TextView;
public class SecondaryActivity extends PreferenceActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.inner);
TextView txt1 = (TextView)findViewById(R.id.textView2);
TextView txt2 = (TextView)findViewById(R.id.textView5);
TextView txt3 = (TextView)findViewById(R.id.textView11);
txt1.setText("First Text");
txt2.setText("Second Text");
txt3.setText("Third Text");
final String message = "This is the message" ;
addPreferencesFromResource(R.xml.pref);
final Preference msg = findPreference("mskey");
msg.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
#Override
public boolean onPreferenceClick(Preference preference) {
msg.setSummary(message);
return true;
}
});
Preference edit = findPreference("editpref");
}
}
I have added the screenshot of the result.
How can I overcome this Issue?Thanks in advance
replace setContentView(R.layout.inner); with addPreferencesFromResource(//your preferenceScreen);
I've encountered this similar issue, and it happens with other view as well like a TextureView inside a FrameLayout. I resized my TextureView smaller and I can still see the previous TextureView in the back flickering.
What made it work is funny -> I just put a black background color (probably any color) to my layout (LinearLayout) and it is working fine now.
I added: android:background="#000000"
<LinearLayout 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"
android:background="#000000"
android:orientation="vertical"
android:visibility="visible"
tools:context=".CameraActivity">
It seems the background is not cleared, if it doesn't know what color to clear it with.

Android TextView not refreshing after new Text has been set

I have a TextView in a RelativeLayout together with a button. When the button is pressed, the TextView.Text (corresponds to setText(string) in Java I believe) is set to a new value. This new value is not show on the screen. When I press the button second time, the previously set value shows, not the current value. I tried running the code that sets the TextView.Text on a UI thread, I have tried to call Invalidate() after setting the value but no luck there. When I move the TextView outside of the RelativeView, the TextView gets set correctly and the change is reflected immediately. I have built a test view and activity to test this behaviour isolated from the rest of the application:
<?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">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="35dp"
android:background="#ccc"
android:layout_margin="35dp"
android:id="#+id/test_layout">
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="0"
android:id="#+id/test_text"
android:background="#android:color/transparent"
android:layout_toLeftOf="#+id/test_increase"
android:gravity="center"
android:textColor="#fff" />
<Button
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="Add"
android:textColor="#fff"
android:gravity="center"
android:layout_alignParentRight="true"
android:id="#+id/test_increase" />
</RelativeLayout>
</LinearLayout>
Activity is in C# (Xamarin) but should be straight forward to create in Java:
[Activity(Label = "TestActivity", ScreenOrientation = ScreenOrientation.Portrait)]
public class TestActivity : Activity
{
Button _testIncrease;
TextView _testText;
int _currentIndex = 0;
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.test_navigation);
_testText = FindViewById<TextView>(Resource.Id.test_text);
_testIncrease = FindViewById<Button>(Resource.Id.test_increase);
_testIncrease.Click += (sender, args) =>
{
_currentIndex++;
_testText.Text = _currentIndex.ToString();
};
}
}

Android TextView doesn't show affected text

Ok so... I've been trying to affect text to TextView (because it's meant to show text, right?)
But for some unknown to me reason it refuses to print whatever I put into it.
My application has two activities, the MainAcivity, or a welcoming screen, per se, and a GameActivity (the project is a Domino game).
I create my previously written java objects at the beginning of the GameActivity class, trying then to show player's name within a TextView.
Player player1 = new Player("Hulk");
creates a player with name Hulk, as you can imagine. I have a method within Player.java to return player's name:
player1.getName()
returns string "Hulk"
I then try to set my TextView's text to hulk, first off by creating a handle to it with:
TextView p1v = (TextView) findViewById(R.textviews.p1view);
then doing the following:
p1v.setText("Player 1 : " + player1.toString());
the p1view is defined as follows within game.xml layout:
<LinearLayout
...
<LinearLayout
...
<TextView
android:id="#+textviews/p1view"
android:text="Board"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
</LinearLayout>
I tried p1view.textAppend, tried using android:editable="true". Nothing seems to work.
Funnily enough though, when I try doing the same thing, within MainActivity, it works, as so:
public class
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
with the TextView defined within main.xml as follows:
<TextView
android:id="#+textviews/mainview_player"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
Following are the full codes of the files, just to avoid answers like: Give us the full code D:
GameActivity.java:
package domino.asd;
import LI260.*; import android.app.Activity; import android.os.Bundle;
import android.widget.TextView;
public class GameActivity extends Activity {
private Player player1 = new Player("HULK");
private Player player2 = new Player("CPU");
private Pioche bag = new Pioche();
private Plateau board = new Plateau();
private Game game1 = new Game(bag, board, player1, player2);
TextView p1name = null;
TextView p2name = null;
/**
* Called when the activity is first created.
*
* #param savedInstanceState
*/
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.game);
TextView tv = (TextView) findViewById(R.textviews.p1view);
tv.setText(player1.getName());
} }
game.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:weightSum="10"
>
<!-- Played layout -->
<LinearLayout
android:id="#+drawable/gameview_Scores"
android:layout_width="fill_parent"
android:layout_height="0px"
android:layout_gravity="top"
android:layout_weight="2"
android:layout_marginBottom="10px"
android:orientation="horizontal"
android:background="#ff0000"
android:gravity="center"
>
<Button
android:id="#+buttons/exitButton"
android:text="Exit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
/>
<Button
android:id="#+buttons/drawButton"
android:text="Draw"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<Button
android:id="#+buttons/scoresButton"
android:text="Show scores"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
<!-- Board layout -->
<LinearLayout
android:id="#+textviews/gameview_Table"
android:layout_width="fill_parent"
android:layout_height="0px"
android:layout_gravity="center"
android:layout_weight="6"
android:layout_marginBottom="10px"
android:background="#ffff00"
>
<TextView
android:id="#+textviews/p1view"
android:text="Board"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
<!-- At hand layout -->
<LinearLayout
android:id="#+drawable/gameview_Playable"
android:layout_width="fill_parent"
android:layout_height="0px"
android:layout_weight="2"
android:layout_gravity="bottom"
android:background="#ff00ff"
>
<TextView
android:id="#+textviews/gameview_AtHandText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
</LinearLayout>
MainActivity.java:
package domino.asd;
import LI260.*;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
Player p1 = new Player("Hulk");
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView p1view = (TextView) findViewById(R.textviews.mainview_player);
p1view.setText(p1.getName());
}
public void btnClick_Name(View view) {
if (view.getId() == R.buttons.B_EnterName) {
EditText playersname = (EditText) findViewById(R.string.playerNameInput);
Toast.makeText(this, "Your name: " + playersname.getText().toString(), Toast.LENGTH_SHORT).show();
}
setContentView(R.layout.game);
}
}
and lastly main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
>
<EditText
android:id="#+string/playerNameInput"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textMultiLine"
android:lines="1"
/>
<TextView
android:id="#+textviews/mainview_player"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<Button
android:id="#+buttons/B_EnterName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Save name and start game."
android:onClick="btnClick_Name"
/>
</LinearLayout>
When you are first loading the activity, you are calling this code:
setContentView(R.layout.main);
TextView p1view = (TextView) findViewById(R.textviews.mainview_player);
p1view.setText(p1.getName());
Yet when the user click's the button you are only calling this code:
setContentView(R.layout.game);
Because you have reset the main content view the child view mainview_player has been destroyed and replaced with a new one when inflated from R.layout.game.
You would need to call this code again to find the new view in the game layout and populate it with your player name:
TextView p1view = (TextView) findViewById(R.textviews.mainview_player);
p1view.setText(p1.getName());
You would be better to look into how activities relate to one another and start a new activity for R.layout.game

myView.setLayoutParams(otherView.getLayoutParams()); Not working, why?

Got my TextView in the xml:
<TextView
android:id="#+id/myTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:gravity="left"
android:text="TextView"/>
I want to create multiple TextViews but i want to look them the same as this.
So I tried:
TextView newTextView = new TextView(this);
newTextView.setLayoutParams(myTextView.getLayoutParams());
I think this should get all the layout parameters from myTextView straigthlghly(?) from the xml, and pass them to newTextView to set them.
My problem is: Nothing happens. It does not take effect, why?
here's a sample project which shows that it works.
you can see that it works by looking at the preview of the visual editor , which looks different than what is shown at runtime.
i think that your mistake was that you didn't set 0px (or 0dp, zero is still zero) for the weighted views.
main.xml (layout) :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:id="#+id/container">
<TextView android:id="#+id/textView1" android:layout_width="wrap_content"
android:layout_height="0px" android:text="TextView1"
android:layout_weight="1" android:background="#ffff0000" />
<TextView android:id="#+id/textView2" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="TextView2"
android:background="#ff00ff00" />
</LinearLayout>
TestActivity.java :
public class TestActivity extends Activity
{
/** Called when the activity is first created. */
#Override
public void onCreate(final Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final TextView tv1=(TextView)findViewById(R.id.textView1);
final TextView tv2=(TextView)findViewById(R.id.textView2);
final LayoutParams layoutParams=tv1.getLayoutParams();
tv2.setLayoutParams(layoutParams);
// adding textView programatically:
final TextView tv3=new TextView(this);
tv3.setText("textView3");
tv3.setBackgroundColor(0xff0000ff);
tv3.setLayoutParams(layoutParams);
final ViewGroup root=(ViewGroup)findViewById(R.id.container);
root.addView(tv3);
}
}

Categories

Resources