I have this onClickListener in onCreate method of my PreferenceActivity, but it gives me error.
Here is the PrefereceActivity:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.settings);
Button button = (Button) findViewById(R.id.button2);
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// do something.
}
});
}
it gives me this error:
unable to start activity componentinfo java.lang.nullpointerexception
any idea what am I doing wrong?
EDIT: My SettingsPreference opens Dialog that holds that "button2".
The Button is causing your NullPointerException because you haven't set a layout and therefore it is null. You shouldn't need to use buttons in a PreferenceActivity anyway.
There's a perfectly good example of using PreferenceActivity over at the Android developer site: http://developer.android.com/reference/android/preference/PreferenceActivity.html
Related
I've added my acivity class in this link
Click MeI am fairly new to Android and am having difficulty trying to add a button on my second activity. I am able to place a button in my main activity and then I use it to navigate to my secondary activity (using setContentView(R.layout.)) and then I use the same 'onClick' method or even 'OnClickListener' method but the button on my second activity just wont work on another activity. Maybe i am missing something
]3
just try to do this:
public class FirstActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.first_activity);
findViewById(R.id.about_us).setOnClickListener(new
View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(FirstActivity.this, SecondActivity.class));
}
});
}
}
and in second activity again find your button in second activity xml by id and write onClickListener for it
You need to implement two separate methods for two different buttons. I would suggest do these things in the Java code instead of XML.
You can do some thing like this:
Button button = findViewById(R.something.something);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//perform your operation(s) here.
}
});
As I understand you try to use one layout.xml for both activities.
You need to declare method click1 in both activities, not only in first.
It means that your first activity has to have method public void click1() and the second activity has to duplicate method public void click1()
I know it's old however,
#Meikiem idea is great. When you use setContentView(View View) you are just setting the activity's
content to another view (xml), and thus not really using the other .java file which has another
onClick method defined for the second button.
Activity's setContentView(view)
You need to create an Intent and pass it along the startActivity method.
Intent Definition
I'm trying to make a button for my app, which will bring the screen to another page. However, I'm not successful in doing so.
I've tried many things, without a reliefing answer.
My project doesn't accept "Intent" in my program.
My button I need to open a new layout is called "OptionButton"
Here's what I've got:
in MainActivity.java
In the beginning I got this
public class MainActivity extends Activity {
private Button startButton;
private Button pauseButton;
private Button resetButton;
public Button OptionButton;
/** further I got this**/
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
/** (I'm just mentioning this because I use savedInstanceState here too)**/
/**MainActivity.java And my code for my button is this **/
OptionButton = (Button) findViewById(R.id.Button1);
OptionButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
myClick(v); /* my method to call new intent or activity */
}
public void myClick(View v) {
Intent intent = new Intent(this, Background2.class);
startActivity(intent);// for calling the activity
}
});
}
}
}
I added this in AndroidManifest:
<activity android:name=".Background2"></activity>
and this in the 2nd class (java file in src map)
(package & imports, then this:
public class Background2 extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
}
}
_
I got 2 classes in src map:
-Background2.java
-MainActivity.java
Also 2 layout xml's:
activity_main.xml
activity main2.xml
In Activity_main, I got this for the button:
<Button
android:id="#+id/Button1"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="35dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:text="#string/OptionButtonLabel"/>
Still it's not working. What am I missing?
Ty all so much!
I've tried changing this:
button1 = (Button) findViewById(R.id.button1); button1.setOnClickListener(this);
But it didn't work.
Inside of the listener you are calling this (which refers to the listener itself), while what you want to refer to is the MainActivity.
Simply change to
Intent intent = new Intent(MainActivity.this, Background2.class);
You Main Activity has to implement OnClickListner
public class MainActivity extends Activity implements OnClickListener{
From Eclipse IDE press Ctrl+Shift+O it will automatically implement and import necessary functions
Thank you for the answers! :)
I've tried installing it on my real device.
Now, it opens, but it's the 2nd xml that opens, and when I click the button, it re-opens the same xml.
:-/
I don't get any error messages though when I changed your suggested solutions (from both of you)
I am new in Android.I get "Unfortunately app has stopped" when i run following code.
public class MainActivity extends Activity {
Button btn;
EditText edit;
#Override
protected void onCreate(Bundle savedInstanceState) {
btn=(Button)findViewById(R.id.button);
edit=(EditText)findViewById(R.id.text);
super.onCreate(savedInstanceState);
setContentView(R.layout.test_click);
btn.setOnClickListener(onClickList);
}
private OnClickListener onClickList= new OnClickListener() {
#Override
public void onClick(View v) {
btn.setText(edit.getText());
}
};
Wrong:
btn=(Button)findViewById(R.id.button);
edit=(EditText)findViewById(R.id.text);
super.onCreate(savedInstanceState);
setContentView(R.layout.test_click);
Issue:
You are trying to find views before setting layout to Activity. So call setContentView() first and then you can find whichever views you want.
Correct:
super.onCreate(savedInstanceState);
setContentView(R.layout.test_click);
btn=(Button)findViewById(R.id.button);
edit=(EditText)findViewById(R.id.text);
Currently you are accessing views from current Activity before setting layout for Activity .Call setContentView before accessing views from xml as:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test_click); // set layout here
btn=(Button)findViewById(R.id.button);
edit=(EditText)findViewById(R.id.text);
btn.setOnClickListener(onClickList);
}
You should call findViewById() after you call setContentView().
button.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
setContentView(R.layout.activity_chart);
}
});
Hi i have the above code wherein upon clicking a button i am trying to display them activity activity_chart. In that activity i want to display a graph. Here i am calling a method createIntent(). But my problem is that the graph is not getting plotted. Please help i am new to android.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
createIntent();
}
public Intent createIntent()
{
...
}
Am i calling the method right.
A new Activity is called with:
startActivity(new Intent(currentActivity.this, nextActivity.class));
Then in your new Activities onCreate(Bundle savedInstance) method you can call setContentView(Layout layout); to set the new Layout.
So if you want to change the Activity when clicking on a Button you have to do the following:
button.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
startActivity(new Intent(currentActivity.this, nextActivity.class));
}
});
You are currently only changing the layout of the current Activity when clicking the button and not changing to another Activity.
I hope I understood you correctly. If not then provide me with some more code so I can try to understand what you want to do.
I have two layouts, what is the best way to switch between them programatically (not using an xml file).
And how can I do it?
Basically there are two ways.
1.) you got one XML containing all components you want to use. The ones which are not available at the moment, should bis hidden. When the user should have the possibility to use them, just make them visible
2.) this is definitly the better solution because Android is concipated for this method.
You have 2 Activities and 2 layout XML files. When you want to display another layout, start the second Activity.
in your first Activity:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mainlayout);
Button btn = (Button) findViewById(R.id.button1);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(StackOverflowActivity.this, Login.class);
startActivityForResult(i, LOGIN_REQUEST);
}
});
}
your second Activity;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
}