I copied the following code from a book. And when I run the app, it fails to launch.
The offending line is aboutButton.setOnClickListener(this);. If I knock it out, the app works.
Any clues?
Thanks.
G.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
View aboutButton = findViewById(R.id.main_about_button);
aboutButton.setOnClickListener(this);
setContentView(R.layout.activity_main);
}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:gravity="center"
tools:context=".MainMenu"
>
<TextView
android:id="#+id/welcome_title"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:textStyle="bold"
android:textSize="25sp"
android:text="#string/welcome_title"
/>
<Button
android:id="#+id/search_button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/search_button"
/>
<Button
android:id="#+id/new_button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/new_button"
/>
<Button
android:id="#+id/help_button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/help_button"
/>
<Button
android:id="#+id/exit_button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/exit_button"
/>
<Button
android:id="#+id/main_about_button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/exit_button"
android:layout_alignLeft="#+id/exit_button"
android:text="#string/main_about_button"
android:clickable="true"
/>
</LinearLayout>
aboutButton is null. You can't initialize it until you have inflated the layout. Change it to
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); // Switch these two lines
View aboutButton = findViewById(R.id.main_about_button);
aboutButton.setOnClickListener(this);
}
Your Views exist within your layout which means they will return null if you try to initialize them before inflating your layout, using a layout inflater or by calling setContentView(), which will give you a NPE when trying to call a function on them or setting a listener
had to put the listener statements AFTER the seContentView command. that fixed it.
my guess is, that the setContentView imports all the variables & ids from the layout. and the findViewById just returned a null pointer or something.
if anyone can confirm my guess, i'd appreciate it.
I recommend you to declare the aboutButton as a Button. Try this:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button aboutButton = (Button) findViewById(R.id.main_about_button);
aboutButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// Do something
}
});
}
I hope it helps!
Related
Upon clicking each TextView they should lead to another layout file to help the user learn about human trafficking. In the onCreate method I have setOnClickListener to my text view. Inflating it is an issue though. Is this called inflating a view? I've seen people recommending the use of fragments for this, using setContentView (from what I've found this shouldn't be used), and using the layout inflater while passing in the layout I want and null. However that doesn't work. How should this code look?
The XML:
<?xml version="1.0" encoding="utf-8"?>
<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"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.example.piatt.worksafe.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Work Safe!"
android:textSize="36sp"
android:layout_centerHorizontal="true"
android:paddingBottom="32dp"
android:id="#+id/title"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="What is Human Trafficing?"
android:layout_below="#+id/title"
android:layout_centerHorizontal="true"
android:textSize="24sp"
android:padding="16dp"
android:id="#+id/whatIsHumanTrafficing"
android:clickable="true"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="How do I get safe labor?"
android:layout_below="#+id/whatIsHumanTrafficing"
android:layout_centerHorizontal="true"
android:textSize="24sp"
android:padding="16dp"
android:id="#+id/howDoIGetSafeLabor"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="How do I check that my job / job offer is legal?"
android:layout_below="#+id/howDoIGetSafeLabor"
android:layout_centerHorizontal="true"
android:textSize="24sp"
android:padding="16dp"
android:gravity="center"
android:id="#+id/checkLegality"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="How can I get help?"
android:layout_below="#+id/checkLegality"
android:layout_centerHorizontal="true"
android:textSize="24sp"
android:padding="16dp"
android:id="#+id/getHelp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="About us"
android:layout_below="#+id/getHelp"
android:layout_centerHorizontal="true"
android:textSize="16sp"
android:layout_alignParentBottom="true"
android:gravity="bottom"
android:id="#+id/aboutUs"
/>
</RelativeLayout>
The java class:
#Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView whatIsHumanTrafficing = (TextView) findViewById(R.id.whatIsHumanTrafficing);
whatIsHumanTrafficing.setOnClickListener(new View.OnClickListener(){
public void onClick(View view){
//What is this context, why do I need it and where does it come from?
//What is the ViewGroup, why do I need it and where does it come from?
view.inflate(Context context, R.layout.what_is_human_trafficing, ViewGroup root);
}
});
}
Aside: You could read the documentation for View.inflate
What is this context, why do I need it and where does it come from?
What: YourActivity.this (replace with your actual class name)
Why: You need it as a parameter to inflate a View.
What is the ViewGroup, why do I need it and where does it come from?
You probably don't need it; it can be null. It is the ViewGroup to inflate the View into. Like for a ListView, you would load each row into the list as the "group".
Anyways, if you want to dynamically show different layout file based upon what is clicked, yes Fragment's are one way to do that, and so are ViewStub's.
And you aren't actually calling inflate on the TextView because it is a static method. It is equivalent to doing this with a LayoutInflater
View v = LayoutInflater.from(YourActivity.this).inflate(R.layout.your_layout, null);
// TODO: Do something with v
The final code should look like:
#Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final RelativeLayout rl = (RelativeLayout) findViewById(R.id.splashScreenId);
TextView whatIsHumanTrafficing = (TextView) findViewById(R.id.whatIsHumanTrafficing);
whatIsHumanTrafficing.setOnClickListener(new View.OnClickListener(){
public void onClick(View view){
MainActivity.this.setContentView(R.layout.what_is_human_trafficing);
}
});
}
Set an on click listener to the text view then within it on MainActivity.this set the content view to the layout you want.
I am developing an android app in which when I am setting a background image to that app, any thing like button or edittext also includes that background, making them difficult to see.
Following is my code
MainActivity.java
public class MainActivity extends Activity {
Button b1,b2,b3;
ImageView i1;
int count=0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b1=(Button) findViewById(R.id.button1);
View background = findViewById(R.id.background);
background.setBackgroundResource(R.drawable.img2);
}
}
activity_main.xml
<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"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:id="#+id/background"
android:background="#drawable/img2"
tools:context=".MainActivity" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/imageView1"
android:layout_alignTop="#+id/imageView1"
android:layout_marginLeft="65dp"
android:layout_marginTop="50dp"
android:color="#ffffff"
android:text="HELLO" />
<EditText
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/button1"
android:layout_centerHorizontal="true"
android:layout_marginTop="19dp"
android:ems="10" >
<requestFocus />
</EditText>
</RelativeLayout>
I want the color of the button and the edit-text to be same as it should be when having a plain white background.
Try this:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b1=(Button) findViewById(R.id.button1);
Drawable buttonBackground = b1.getBackground();
View background = findViewById(R.id.background);
background.setBackgroundResource(R.drawable.img2);
b1.setBackgroundResource(buttonBackground);
}
This will work.
I am trying to show textview items from entries in edittext on button click to listview in another layout with scrollview.. Code is not running.. application error so help me.
this is my xml..
<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"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<Button
android:id="#+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/spinner1"
android:layout_toRightOf="#+id/editText5"
android:text="#string/add" />
<EditText
android:id="#+id/editText5"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView5"
android:layout_alignTop="#+id/textView5"
android:layout_marginTop="8dp"
android:ems="10"
android:inputType="number" />
<RelativeLayout
android:layout_width="70dp"
android:layout_height="140dp"
android:layout_alignParentLeft="true"
android:layout_alignTop="#+id/spinner1"
android:layout_marginTop="17dp" >
<ScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scrollbars="vertical">
</ScrollView>
<ListView
android:layout_width="wrap_content"
android:layout_height="125dp">
</ListView>
this is my java code.
private Button mbtn_currencyadd;
private EditText medit_currency;
private TextView mtv1=null;
private ListView mlv;
public String s1;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mtv1=new TextView(this);
mlv.addView(mtv1);
//mll2.addView(mtv2);
mbtn_currencyadd=(Button)findViewById(R.id.button3);
medit_currency=(EditText)findViewById(R.id.editText5);
mbtn_currencyadd.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
s1=medit_currency.getText().toString();
s1=mtv1.getText().toString()+"\n"+s1;
mtv1.setText(s1);
mtv1.setTextSize(17);
medit_currency.setText("");
}
});
Have a look at this example.
It is very similar to what you are looking for.Items are added dynamically from Edit text to list view on button click
You can ask if you have any further queries :)
Of course it won't work. Here's why: -
You list view doesn't have an id
You are not getting a reference to your listview (m1v) in your activity
In order to add items to a listview, you need to create an adapter
See this example http://developer.android.com/guide/topics/ui/layout/listview.html
I have been trying for a while to work out how to dynamically create a RelativeLayout with multiple views inside (e.g. TextView, ProgressBar) a LinearLayout to create a RelativeLayout beneath the previous one after every button click. Please can anyone look at my code and see if there is anything that I can do to solve this issue.
Here is the code:
activity_test_container.xml
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/frag1ScrollView"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:id="#+id/testLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".TestContainerActivity" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/testContainerTextView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/testContainerTextView1"
android:layout_marginBottom="16dp"
android:text="TextView2" />
<TextView
android:id="#+id/testContainerTextView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:text="TextView1" />
<Button
android:id="#+id/testContainerButton1"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toRightOf="#+id/testContainerTextView2"
android:text="Button" />
</RelativeLayout>
</LinearLayout>
</ScrollView>
container.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/containerLayout"
android:layout_width="match_parent"
android:layout_height="80dp"
android:paddingBottom="10dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:layout_marginBottom="16dp"
android:background="#color/display_panels" >
<ProgressBar
android:id="#+id/containerProgressBar1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_toLeftOf="#+id/containerImageButton2"
android:max="100"
android:progress="40" />
<TextView
android:id="#+id/containerTextView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/containerTextView6"
android:layout_alignLeft="#+id/containerProgressBar1"
android:text=""
android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView
android:id="#+id/containerTextView6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/containerProgressBar1"
android:layout_centerHorizontal="true"
android:text=""
android:textAppearance="?android:attr/textAppearanceSmall" />
<ImageButton
android:id="#+id/containerImageButton2"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="#+id/containerTextView6"
android:background="#color/display_panels"
android:contentDescription="Okay icon"
android:src="#drawable/ic_green_ok" />
</RelativeLayout>
TestContainerActivity.java
public class TestContainerActivity extends Activity implements OnClickListener {
LinearLayout containerLayout;
Button testButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test_container);
testButton = (Button)findViewById(R.id.testContainerButton1);
containerLayout = (LinearLayout)findViewById(R.id.testLayout);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.test_container, menu);
return true;
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(v==testButton){
createNewLayout();
}
}
public void createNewLayout(){
LayoutInflater layoutInflater = (LayoutInflater) getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View addView = layoutInflater.inflate(R.layout.container, null);
containerLayout.addView(addView);
}
}
I'm not entirely sure what you're problem is, but I suspect it's that the rows are not showing up at all because I don't see where you attach the listener to the Button. To handle a click event, an OnClickListener needs to be set on your View. Though this is commonly done with Buttons, OnClickListeners can be set on any view, so any size/shape widget can be made clickable. This is done with the setOnClickListener method of a View. There are multiple ways to do this, try modifying your onCreate like this:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test_container);
testButton = (Button)findViewById(R.id.testContainerButton1);
containerLayout = (LinearLayout)findViewById(R.id.testLayout);
testButton.setOnClickListener(this);
}
An alternative method to setting your listener would be to create the listener in onCreate rather than using the Activity:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test_container);
testButton = (Button)findViewById(R.id.testContainerButton1);
containerLayout = (LinearLayout)findViewById(R.id.testLayout);
testButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
createNewLayout();
}
});
}
In this case, you wouldn't need to have your Activity implement OnClickListener. I usually only will do something like that if I have many buttons with similar functionality, where creating listeners for each will cause a performance hit. For more isolated cases like this, I prefer to set individual Listeners since the performance difference will be negligible, but that's just my personal preference.
Hope this helps! If your problem was actually based somewhere else, please modify your question and I'll try my best to assist! Also, keep in mind that you can use the Log class to post information about execution in your LogCat output. It really helps with debugging! I suspect that if you put some logging in your listener and createNewLayout() right now, you'd see that the logging never happens because those methods are never called.
I have an app, which shows toast below the actionbar. I'm using Crouton library for displaying toast. Here I'm placing custom layout for toast, that layout contains one textview and one button (for close option). So Toast appears with button. If I click that it should close the toast but nothing happens.. Below Code for this example. Any Help Appreciated
MainActivity.java
public class MainActivity extends SherlockActivity implements OnClickListener {
private View customToastView;
private TextView customToastMessageView;
private Button customToastCloseButton;
private Button doneButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
doneButton = (Button) findViewById(R.id.done_button);
customToastView = getLayoutInflater().inflate(R.layout.toast_custom_layout, null);
customToastMessageView = (TextView) customToastView.findViewById(R.id.messages);
customToastCloseButton = (Button) customToastView.findViewById(R.id.close_button);
customToastCloseButton.setOnClickListener(this);
doneButton.setOnClickListener(this);
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.done_button:
Crouton.cancelAllCroutons();
customToastMessageView.setText("Message");
Crouton.show(this, customToastView);
break;
case R.id.close_button:
Crouton.cancelAllCroutons();
break;
}
}
}
toast_custom_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TextView
android:id="#+id/messages"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="26dp"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
<Button
android:id="#+id/close_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginRight="23dp"
android:text="Button" />
</RelativeLayout>
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<Button
android:id="#+id/done_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="146dp"
android:text="Button" />
</RelativeLayout>