I am getting nullpointer exception on Textview's settext(charSeq) method? - android

I have given my code below.I am just want to create a custom dialog in android.whenever i click on button it will show a custom dialog box.i have created a xml "alert.xml".after click on button i will show the content from string.xml
public class TriangleActivity extends Activity implements View.OnClickListener{
/** Called when the activity is first created. */
private Button bt;
private Dialog dialog;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
bt=(Button) findViewById(R.id.button1);
bt.setOnClickListener(this);
//Context mContext = getApplicationContext();
dialog = new Dialog(TriangleActivity.this);
dialog.setContentView(R.layout.alert);
dialog.setTitle("This is my custom dialog box");
TextView t=(TextView) findViewById(R.id.tv1);
t.setText(getString(R.string.h));
}
public void onClick(View v)
{
dialog.show();
}
}
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"
>
<Button
android:id="#+id/button1"
android:layout_width="60dp"
android:layout_height="60dp"
/>
</LinearLayout>
alert.xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scrollbars="vertical" >
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scrollbarAlwaysDrawVerticalTrack="true" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/tv1" >
</TextView>
</LinearLayout>
</ScrollView>
string.xml
<?xml version="1.0" encoding="utf-8"?>I am getting
<resources>
<string name="hello">Hello World, TriangleActivity!</string>
<string name="app_name">Triangle</string>
<string name="h">The Royal Society of Chemistry’s interactive periodic table which </string>
</resources>

use this to find the textview:
TextView t=(TextView)dialog.findViewById(R.id.tv1);

TextView t=(TextView)dialog.findViewById(R.id.tv1);
This is ur solution dude

Modify your textView initialization.. because its unable to find the dialog view..so do this--
TextView t=(TextView)dialog.findViewById(R.id.tv1);
Hope this will help you..

if t.setText(getString(R.string.h)); is throwing exception NullPointerException, it means, either t or getString(R.string.h) is null.
t might be null, if main.xml does not have a TextView with id, #+id/tv1.

TextView text = (TextView)findviewByid(R.id.text1);

Related

Android Studio Activity Layout Changing

I need help in changing layout of Activity in Android Studio when I click on button.
But I don't want to open new activity I want to change layout and design completely on the current activity by clicking button. Is it possible ?
you can inflate another layout during button click:
public class LayoutInflateDemo extends Activity {
private LinearLayout layoutToAdd;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_existed);
layoutToAdd = (LinearLayout) findViewById(R.id.existedlayout);
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
LayoutInflater inflater = LayoutInflater
.from(getApplicationContext());
View view = inflater.inflate(R.layout.layout_toinfliate, null);
//Add the inflated layout to your existing view
layoutToAdd.addView(view);
}
});
}
}
layout to inflate 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">
<TextView
android:id="#+id/mytext"
android:text="Inflated layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
And this is your existing 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/existedlayout"
>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Clicck to inflate view"
android:id="#+id/button"
/>
</LinearLayout>
Yes it is possible by using setContentView() method. But you have to make sure that you didn't use any view (e.g. view by id) that is not available on current layout.
Best way is Fragments and FragmentActivity https://developer.android.com/guide/components/fragments.html

Button - Textview click event issue - Android studio

I have a list of buttons in my activity_main, Once i clicked on Button_A it is taking me to the next list of buttons layout for eg. Button_A1.
And once i clicked Button_A1 it should show me the text which i have written in text_view. But here its getting failed. Once i Clicked in Button_A1, it showing like "your app is stopped". Can you guys help me on this, as i am new on this platform.
**Button_A1.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">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Small Text"
android:id="#+id/textView2"
android:layout_gravity="center_vertical"
android:onClick="Button_A1" />
</LinearLayout>
**button_A1.java:**
public class Ov1 extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.ov1);
}
}
Do i have to give activity entry in manifest.xml. Please help me on this..
Button_A ----> Button_A1 ----> Textview
Clicked in Button_A1, it showing like "your app is stopped"
Because Button_A1 method is not available in Ov1 class. add method as:
public void Button_A1(View view) {
////
}
NOTE: To make TextView click-able need to add android:clickable="true" attribute in TextView xml

Declare EditText as TextView in Android

i have a xml layout main.xml like this
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:gravity="center" >
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="text"
android:textColor="#000"
android:textSize="16dp"
android:id="#+id/edtxt"
android:gravity="center|top|left"/>
</RelativeLayout>
and i declare this in my activity like this
public class MyActivity extends Activity {
private TextView txt = null;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
txt= (TextView)findViewById(R.id.edtxt);
}
}
but i have no problem in my activity. why?
see this link http://developer.android.com/reference/android/widget/EditText.html
and look here
you will find your answer
That would be because EditText is a subclass of TextView. You're casting an EditText to a TextView, which works because an EditText IS a TextView.
Check out the EditText's docs: http://developer.android.com/reference/android/widget/EditText.html
And here's a little something so that you can learn about inheritance, which is what is happening here: http://docs.oracle.com/javase/tutorial/java/IandI/subclasses.html
Answer is simple, it is so because EditText extends TextView.

Dynamic Strings and Xml

I'm new to Android (and surely I'm not an expert Java developer), so I'm sorry for the dumb question
I would like to "capture" the id of a textView and to set its text value dynamically,
here is how I'm trying to do this:
public class AndStringTestActivity extends Activity {
/** Called when the activity is first created. */
String abc = "abcabc";
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView tv = (TextView)findViewById(R.id.test);
tv.setText(abc);
setContentView(R.layout.main);
}
}
and here is the Xml from which i'm reading the id of the textview i would like to write inside
// 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"
>
<TextView
android:id="#+id/test"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</LinearLayout>
the error i receive is the usual Application stopped unexpectedly
Thanks.
Call setContentView before trying to find the TextView. The TextView does not exist before you have called setContentView (which will inflate your XML layout)
Call setContentView(R.layout.main) before you call findViewById().

Activity of android

I am working with the xml file ,through which i am accessing a string through a variable passkey.later i am assigning it to strPassword. i am getting error for setListAdapter.Here is my code..
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="passkey">
<item>1234</item>
</string-array>
</resources>
and
public class AdminLogin extends Activity
{
public void onCreate(Bundle bdlActivity)
{
String[] strPassword = getResources().getStringArray(R.array.passkey);
setListAdapter(new ArrayAdapter<String>(this,R.layout.adminlogin,
R.id.Editpassword, strPassword))
//button events
}
You need to extend your class with ListActivty. You cant instantiate ListView in simple activity.
You can do it from a simple Activity too:
say you have a main layout, like:
mylayout.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent">
<Button android:id="#+id/btn_ok" android:layout_height="wrap_content"
android:layout_width="wrap_content" android:text="OK" android:layout_alignParentBottom="true" />
<ListView android:id="#+id/listview" android:layout_width="fill_parent" android:layout_height="fill_parent"
android:layout_above="#id/btn_ok" />
</RelativeLayout>
This would be the layout of your activity, with a list view inside of it (#id/listview)
Your Activity's onCreate method should look like this:
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.mylayout);
final ListView listView = (ListView) findViewById(R.id.listview);
listView.setListAdapter(new ArrayAdapter<String>(this, R.layout.adminlogin,
R.id.Editpassword, strPassword))
}

Categories

Resources