Android TextView and null pointer exception - android

In my activity I do
TextView bTitle = (TextView)findViewById(R.id.TextView);
bTitle.setText(Html.fromHtml(title));
where title is a string
but i get a null pointer exception at second line ie. bTitle.setText(Html.fromHtml(title));
can anyone help???
Activity
public class NewsDetails extends ListActivity{
public static final String LOG_TAG = "Infra";
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listplaceholder);
super.onStart();
Intent myIntent = getIntent();
String id = myIntent.getStringExtra("content_id");
String title = myIntent.getStringExtra("title");
//Spanned newTitle = Html.fromHtml(title);
//tv.setText(Html.fromHtml(title));
TextView bTitle = (TextView)findViewById(R.id.TextView);
Log.d(LOG_TAG, "What is the value: " + bTitle);
bTitle.setText(Html.fromHtml(title));
ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
String xml = XMLfunctions.getBodyXML(id);
String result = xml.replaceAll("<p>", "<p><div align=\"justify\">");
String nXml = result.replaceAll("</p>", "</div></p>");
TextView body = (TextView)findViewById(R.id.TextView1);
body.setText(Html.fromHtml(nXml));
//Spanned body = Html.fromHtml(nXml);
/*HashMap<String, String> map = new HashMap<String, String>();
map.put("title", tv.toString());
map.put("news", tv1.toString());
mylist.add(map);*/
/*ListAdapter adapter = new SimpleAdapter(this, mylist , R.layout.list_item, new String[] { "title", "news" }, new int[] { R.id.item_title, R.id.item_subtitle });
setListAdapter(adapter);
final ListView lv = getListView();
lv.setTextFilterEnabled(true);*/
}
Layout - listplaceholder.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ListView
android:id="#id/android:list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:drawSelectorOnTop="false" />
<TextView
android:id="#id/android:empty"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="No data"
android:focusable="false"
android:clickable="true"/>
</LinearLayout>
text_view.xml
<LinearLayout android:id="#+id/LinearLayout01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<TextView
android:id="#+id/TextView"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:textSize="16px"
android:textStyle="bold">
</TextView>
<TextView
android:id="#+id/TextView1"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:textSize="16px"
android:textStyle="bold">
</TextView>
</LinearLayout>

You should load the correct xml for your layout
setContentView(R.layout.text_view)
TextView bTitle = (TextView)findViewById(R.id.TextView);
bTitle.setText(Html.fromHtml(title));

i found two issue in your code
1)first thing is your TextView is not in listplaceholder.xml ..
so you it will be null pointer...
2)and you need add this code before setContentView();
Intent myIntent = getIntent();
String id = myIntent.getStringExtra("content_id");
String title = myIntent.getStringExtra("title");
and let me know what u want in list and where you call setAdapter for list , where is your adapter...

Make sure, that you actually have the
<TextView android:id="#+id/TextView [...]
view in your activity's layout xml.
Update
In your listplaceholder.xml there is no such TextView!
If you use the text_view.xml layout as your item renderer, then the code that tries to access the TextView should be placed inside your ListAdapter implementation's getView method!

1) Make sure R is imported from your project (and not from android.R namespace) in your import(s) section on the top
2) After the
TextView bTitle = (TextView)findViewById(R.id.TextView);
perform a log:
Log.d("debug", "bTitle: " + (bTitle != null));
and check out from terminal (if you're using Linux, if Windows, just run the logcat)
adb logcat | grep bTitle
This should tell you if you properly got the TextView from your layout (if it is present in the layout, like #rekaszeru mentioned).
3) Make sure the title variable is not null as well

Thank you all for your help. I have replaced textview with webview as i needed a lot of html formatting in my application and now my text appears in proper format and also my appliction is running fine.
Thanks

Related

Add components in Fragment at runtime in android studio

I've been struggling with finding an appropiate answer on how to add components to a layout in a fragment at runtime in android studio.
Specifically:
I have Class A which downloads and parses an XML file. Fragment B instantiates this Class A and should display those downloaded items. (just a textview for now)
This is the XML file in which the textView should be displayed. The items should be displayed in two columns. I know how to create the layout in the XML File but I have no idea on how to do it programatically. I've also read something about inflaters but I dont know if it fits this purpose.
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center">
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TableRow
android:layout_width="match_parent"
android:layout_height="0dp"
android:paddingTop="10dp">
<TextView
android:id="#+id/columnItem"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="5dp"
android:layout_marginStart="5dp"
android:layout_weight=".5"
android:background="#c5c5c5"
android:gravity="center"
android:text="#string/CategoryLeft" />
</TableRow>
</ScrollView>
</TableLayout>
So here is the Code in Fragment B which currently just changes the text of the two existing textviews, that works just fine.
public void onStart() {
super.onStart();
ArrayList<String> categories = new ArrayList<>();
XMLHandler getXML = new XMLHandler();
getXML.execute();
categories = getXML.getCategories();
Iterator<String> it = categories.iterator();
while (it.hasNext()) {
System.out.println("Data is " + it.next());
columnItem.setText(it.next());
}
}
The goal is to add a new TextView for each iteration through the while loop to the parent layout. This TextView should display the content of it.next().
Thanks in advance and let me know if u need any further information.
If you want to add a TextView to TableRow.
First, add an id to TableRow
<TableRow
android:id="#+id/table1"
android:layout_width="match_parent"
android:layout_height="0dp"
android:paddingTop="10dp">
Then, in your onCreate
tableRow = findViewById(R.id.table1); // tableRow is a global variable
add a void in your fragment
private void addTextView(String atext) {
TextView txt = new TextView(getActivity());
txt.setText(atext);
tableRow.addView(txt);
// here you can add other properties to the new TextView (txt)
}
and then
public void onStart() {
super.onStart();
ArrayList<String> categories = new ArrayList<>();
XMLHandler getXML = new XMLHandler();
getXML.execute();
categories = getXML.getCategories();
Iterator<String> it = categories.iterator();
while (it.hasNext()) {
String atxt = it.next();
System.out.println("Data is " + atxt);
addTextView(atxt);
}
}

New Game activity in Android: what UI choices?

My Android game project needs a "New Game" activity. The game has a couple of options that should be asked each time a new game is started: the board size and the game seed. I would like to do this in a form resembling the standard "Settings" activity, but with an added "Start" button at the bottom. Given that these things will be chosen every new game, they don't want to be hidden away in the app's Settings activity. So what's the best way to go about this?
Things I've investigated:
Using a custom PreferenceActivity, since the behaviour is almost the same. Since the PreferenceActivity doesn't use layout XML files I can't see a way of adding a button to it.
A Linear layout that includes a ListView and a Button. I've started implementing this, but I can't even find a way to add items to the ListView - the ListView guide in the documentation certainly isn't aimed at a beginner like me! And then the list items need to be interactive to allow options to be selected: this looks like a lot of work to duplicate what PreferenceActivity almost does on its own.
Is there an option that I'm missing? I've spent a couple of hours trying to find an answer in the documentation and in previous questions, but with no progress. If anyone can point me in the right direction I'd be grateful.
Here is some code for you. Hope it will be useful.
You should go by the second way and use LinearLayout (this is main.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:orientation="vertical">
<ListView
android:id="#+id/lvSimple"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</ListView>
<Button // describe your start button here/>
</LinearLayout>
Layout for ListView item:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<ImageView
android:id="#+id/ivImg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher">
</ImageView>
<LinearLayout
android:id="#+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical">
<CheckBox
android:id="#+id/cbChecked"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="CheckBox">
</CheckBox>
<TextView
android:id="#+id/tvText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_marginRight="20dp"
android:text="TextView">
</TextView>
</LinearLayout>
</LinearLayout>
Your MainActivity.java:
public class MainActivity extends Activity {
// имена атрибутов для Map
final String ATTRIBUTE_NAME_TEXT = "text";
final String ATTRIBUTE_NAME_CHECKED = "checked";
final String ATTRIBUTE_NAME_IMAGE = "image";
ListView lvSimple;
/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// data array
String[] texts = { "sometext 1", "sometext 2", "sometext 3",
"sometext 4", "sometext 5" };
boolean[] checked = { true, false, false, true, false };
int img = R.drawable.ic_launcher;
// our data source
ArrayList<Map<String, Object>> data = new ArrayList<Map<String, Object>>(
texts.length);
Map<String, Object> m;
for (int i = 0; i < texts.length; i++) {
m = new HashMap<String, Object>();
m.put(ATTRIBUTE_NAME_TEXT, texts[i]);
m.put(ATTRIBUTE_NAME_CHECKED, checked[i]);
m.put(ATTRIBUTE_NAME_IMAGE, img);
data.add(m);
}
String[] from = { ATTRIBUTE_NAME_TEXT, ATTRIBUTE_NAME_CHECKED,
ATTRIBUTE_NAME_IMAGE };
// array of IDs of Views
int[] to = { R.id.tvText, R.id.cbChecked, R.id.ivImg };
// create adapter
SimpleAdapter sAdapter = new SimpleAdapter(this, data, R.layout.item,
from, to);
// create ListView and set the adapter
lvSimple = (ListView) findViewById(R.id.lvSimple);
lvSimple.setAdapter(sAdapter);
}
}

displaying user input from edit text to list view

I have problem with adding 2 numbers in list view. This is my row with 2 text view (for each numer). textView2 ignore
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="0"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/textView3"
android:gravity="center_horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" - "
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0"
android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>
When user add number it will display in list view. That second number needs to be 11 - that number that user inserted. It's all working but I dont know how to display that second number.
public class MainActivity extends Activity {
ArrayAdapter<String> m_adapter;
ArrayList<String> m_listItems = new ArrayList<String>();
Button bt;
EditText et;
TextView tv;
ListView lv;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Inflate the menu; this adds items to the action bar if it is present.
bt = (Button) findViewById(R.id.button1);
et = (EditText) findViewById(R.id.editText1);
tv = (TextView) findViewById(R.id.textView1);
lv = (ListView) findViewById(R.id.lista);
m_adapter = new ArrayAdapter<String>(this,R.layout.row, R.id.textView1);
lv.setAdapter(m_adapter);
//final String input = et.getText().toString();
bt.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
String input = et.getText().toString();
if(null!=input&&input.length()>0){
String maxpunti = "11";
int a = Integer.parseInt(maxpunti);
int b = Integer.parseInt(input);
int c = a-b;
String input2 = String.valueOf(c);
m_adapter.add(input);
m_adapter.add(input2);
m_adapter.notifyDataSetChanged();
}
}
});
}
}
The default implementation of ArrayAdapter<String> binds a single Stringvalue to a a single textfield. Since you have several views for each listitem or "row" in your listview ( you need to implement/extend your own ArrayAdapter and override the getView method.
Them you can set the values for any number of custom views that you may have in your list item layout.
Look at a simple example and also it might be a good idea to look som docs on how listviews/adpters work in android since they are very central.

Adding TextView to a LinearLayout with text taken from SharedPreferences key

I'm trying to create a list of keys from a SharedPreferences object. Since the user can add new pairs key-value, I need to do it dynamically.
Here is the onCreate function of my activity:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LayoutInflater inflater = this.getLayoutInflater();
LinearLayout layout = (LinearLayout)inflater.inflate(R.layout.activity_show_saved, null);
this.add_here = (LinearLayout)layout.findViewById(R.id.saved_list);
// Loading shared preferences
SharedPreferences sp = this.getSharedPreferences("saved_sequences", MODE_PRIVATE);
Map<String, ?> kv = sp.getAll();
int i = 0;
// Iterating through shared preferences
for(Map.Entry<String, ?> entry : kv.entrySet()) {
Toast.makeText(this, entry.getKey(), Toast.LENGTH_SHORT).show();
TextView to_add = new TextView(this);
to_add.setText(entry.getKey());
to_add.setId(i++);
this.add_here.addView(to_add, LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
}
this.setContentView(layout);
}
Where this.add_here is a class variable declared as private LinearLayout. And here is the xml file which is being inflated:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#color/background"
tools:context=".ShowSaved" >
<com.google.ads.AdView
android:id="#+id/adViewTop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
ads:adUnitId="*****************"
ads:adSize="SMART_BANNER"
ads:testDevices="TEST_EMULATOR"
ads:loadAdOnCreate="true" />
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:id="#+id/saved_list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
</LinearLayout>
</ScrollView>
</LinearLayout>
Unfortunately I get a blank screen. The problem is in adding the TextViews, since the Toast inside the while correctly shows saved keys as it's expected to do.
I've just done similar dynamic list, but my TextView is a part of a stand-alone layout.
Perhaps this will do the trick for you.
This code works for me:
final ScrollView ll = (ScrollView) getLayoutInflater().inflate(
R.layout.dropdown_list, null);
final LinearLayout container = ((LinearLayout) ll
.findViewById(R.id.dropdown_list_container));
if (myObjectsCount > 0) {
for (final MyObject ci : myObjects()) {
LinearLayout item = ((LinearLayout) getLayoutInflater()
.inflate(R.layout.list_item, null));
TextView tv = ((TextView) item
.findViewById(R.id.list_item_text));
tv.setText("your text here");
container.addView(item);
}
}
And the layout for the text view:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/list_item"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/buttons"
android:clickable="true"
android:gravity="center_vertical"
android:orientation="horizontal" >
<TextView
android:id="#+id/list_item_text"
style="#android:style/Widget.TextView"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center_vertical" />
</LinearLayout>
As someone have already stated in the comments, that was not the right way of doing it. Here is the result achieved using a ListView. Have a look.
public class ShowSaved extends Activity implements OnItemClickListener {
// Layout
private ListView add_here;
private TextView empty_view;
// String Array
private ArrayList<String> string = new ArrayList<String>();
// SharedPreferences object and editor
private SharedPreferences sp;
// Array adapter for ListView add_here
private ArrayAdapter<String> saved;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.activity_show_saved);
this.add_here = (ListView)findViewById(R.id.saved_list);
this.empty_view = (TextView)findViewById(R.id.empty_text);
// Loading shared preferences and editor
this.sp = this.getSharedPreferences("saved_sequences", MODE_PRIVATE);
// Creating the Adapter with all strings in an array
this.saved = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, android.R.id.text1, string);
this.saved.setNotifyOnChange(true);
// Retrieving all pairs in SharedPreferences sp
Map<String, ?> kv = sp.getAll();
// Iterating through shared preferences
for(Map.Entry<String, ?> entry : kv.entrySet()) {
saved.add(entry.getKey());
}
// Setting Adapter to ListView and empty View
this.add_here.setEmptyView(empty_view);
this.add_here.setAdapter(this.saved);
// Setting Listener for ListView add_here
this.add_here.setOnItemClickListener(this);
}
Note: this code won't compile on its own since it lacks the onItemClick function required for the OnItemClickListener interface.

difficulty displaying data in my listview android

I have problem with displaying data in my listview. it's not displaying vertically.
here's my code:
private ListView lv;
private ArrayList<String> results = new ArrayList<String>();
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.meal_result);
BreakFastLog info = new BreakFastLog(this);
info.open();
String data1 = info.getMealNameData();
//String data2 = info.getServingData();
results.add(data1);
lv = (ListView) findViewById (R.id.lvBF);
lv.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, results));
lv.setTextFilterEnabled(true);
info.close();
}
and my xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:id="#+id/lvBF"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
What's wrong with this? please help me figure out what makes it displayed horizontally.
Here's the screenshot:
After gone through your code,
You have a only one String value String data1 = info.getMealNameData();
in your ArrayList<String> results. And results is added to your List Adapter.
So only one row displayed in your ListView.
So You have to either make a String[] for all user Inputs instead of your one String data1 and pass that String[] to your List Adapter.
Update:
String data1 = info.getMealNameData();
String[] inputData = data1.split(" ");
lv.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, inputData ));
Try <RelativeLayout> in your xml instead of <LinearLayout>. And add this to your listview
<ListView
android:id="#id+/yourListViewid"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dip"
android:layout_marginRight="5dip"
android:layout_alignParentTop="true" >
</ListView>

Categories

Resources