I created a menu and when I click on register I want to open a new class with just one line of text in it.
The app opens and runs but when I click on the "Register" Button in my menu the app crashes. I have no code erros.
Can you see my issue?
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.register:
Intent intent = new Intent(MeorNot.this, AddMember.class);
startActivity(intent);
return true;
}
}
This is my new class i try to open
package com.meornotFinal;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class AddMember extends Activity {
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
TextView textview = new TextView(this);
textview.setText("Add member");
this.setContentView(textview);
}
}
check your activity
<activity android:name="AddMember.class"> in AndroidManifest.xml file.
you are forgetting to add this line . so add this line also in androidmanifest.xml file.
Your current code block for the Activity you call after clicking on the Register button:
public class AddMember extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView textview = new TextView(this);
textview.setText("Add member");
this.setContentView(textview);
}
}
You will need to change the this.setContentView(textview); to something like this:
public class AddMember extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.a_layout_in_your_layout_folder);
// CAST YOUR TEXTVIEW HERE
}
}
In the a_layout_in_your_layout_folder, you can keep a single LinearLayout and add your TextView at runtime. I don't quite see the point of doing such a thing as declaring a TextView in the layout XML would be a far simpler option.
But to each his own I suppose.
You are trying to set a View in setContentView() instead of a layout
this.setContentView(textview);
If you are going to do it this way then you need to create a layout programmatically with something like
RelativeLayout relativeLayout = new RelativeLayout(this);
then you can add your TextView to this and set your layout in setContentView()
Unless you need to do it this way, a simpler way is probably to create the xml layout then get your TextView from there with findViewById(R.id.textView1) after calling setContentView(R.layout.your_layout_file);
Logcat
To turn on the logcat, if not already in Eclipse, Goto Window-> Show View -> Other -> Android-> Logcat.
This will give you a console where it logs errors and other debug information. When your app crashes it will give you the reason and usually a java file with line number where it crashed. You may have to do further digging if the error happens before that line but it will give you a good starting point.
An example might be
Caused by: java.lang.NullPointerException
08-07 08:24:02.516: ERROR/AndroidRuntime(334): at your_app.org.ThisActivity.onCreate(ThisActivity.java:26)
Then you know to start looking at line 26 of ThisActivity for something that is null
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 to android. Can I get to know Onclick text how can I redirect to another XML page in Android ?
You can redirect to another xml through a button click, But here I think you have asked how to do it clicking on some text, for a example if you want to click on this text "click to sign up" and redirect you to the signup page.
So here is how I did it.
1)for text click
Inside Main Activity class
you write the following code.
//Text redirect. signup textview should be created within mainActivity.xml
final TextView signup=(TextView)findViewById(R.id.signup);
signup.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v1) {
Intent launchActivity1= new Intent(MainActivity.this,SignUp.class);
startActivity(launchActivity1);
}
});
In the AndroidManifest file, You create a New Activity
<!-- Sign Up activity -->
<activity
android:name=".SignUp"
android:label="#string/app_name">
<intent-filter>
<action android:name="com.example.xxxx.SignUp"/>
<categeory android:name="android.intent.categeory.LAUNCHER"/>
</intent-filter>
</activity>
Sign Up Java class. You make signup.xml appear inside this class by accessing it through the R.id.layout
package com.example.xxxx;
import android.app.Activity;
import android.os.Bundle;
public class SignUp extends Activity{
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.sign_up);
}
}
.
2) for button click
You will do the same as for Text click.
In MainActivity.java class
final Button button=(Button)findViewById(R.id.loginbtn);
button.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
Intent launchactivity= new Intent(MainActivity.this,Details.class);
startActivity(launchactivity);
Then you will create A new Activity in AndroidManifest.
You Have to have a Details.java class. Since you have mentioned to redirect to Details.class, according to this example.
Inside Details class like in previous access the relevant xml file.
"click here to signup" in the following image will redirect you to the signup.xml .
I'm guessing here that with 'new XML page' you refer to another view?
Most basic way to accomplish this is to make 2 activities with each a different layout (that XML file). On activity 1 add a button. In onclicklistener for that button start the other activity.
Use the visual tools in your IDE to create the activities. Use below code to switch to other activity:
public void startActivity2(View view) {
Intent intent = new Intent(this, Activity2.class);
startActivity(intent);
}
More info at http://developer.android.com/training/basics/firstapp/starting-activity.html
There so many ways to do it i can't even describe them all :)
Possibly you thing about starting new activity. So you need to create new activity class with proper layout (in xml) and then start it with intent.
Heres an example
I'm using an android panel widget in my application to create a sliding panel effect from the top of my app (similar to the notification panel). When clicked the panel opens to fill about 30% of the screen. By default the panel is closed and has a handle to "show". I'm trying to modify it to be in the shown state by default but am unsure how to do it. I assume that I'll have to modify the Panel.java file but I suppose it might also be done in my layout file.. Any tips?
Assuming you're defining your own class which extends Panel...in your onCreate() override, you could try calling setOpen(true, false); ?
EDIT As you're not extending it yourself, you could simply call the setOpen() method as soon as your activity is created instead. Example...
public class MyActivity extends Activity {
protected Panel topPanel = null;
#Override
public void onCreate(Bundle savedInstance) {
super.onCreate();
setContentView(R.layout.main);
topPanel = (Panel) findViewById(R.id.top_panel);
topPanel.setOpen(true, false);
}
}
I know this is the basic stuff but I couldn't figure it out. Here is my code:
public class test extends Activity{
private static final String TAG = "test";
private Button Test;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Log.v(TAG,"onCreate is called.");
this.setContentView(R.layout.main);
Test= (Button)this.findViewById(R.id.Test);
Test.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Log.v(TAG, "onClick Entered.");
// Perform action on click
}
});
setContentView(R.layout.main);
}
}
I can see the first log "OnCreate" but the button click event listener doesn't seem
to work because I can't see "OnClick Entered". Where did I do wrong?
Thanks
You are calling setContentView twice, the second time after you have set up your on click listener. This means that the view that you add the listener to is no longer visible, it is replaced with a different instance of the view. Remove the second setContentView.
A few comments / things to try:
You have your naming conventions backwards. You should be naming your activity in title case, your variables in camel case (e.g. your activity "test.java" should be Test.java, your Button Test should be Button test). That's not your problem, but just something to keep in mind.
You're calling super.onCreate() twice. I really don't know what effect that has, but it shouldn't be there. You're also calling setContentView() twice. One call to onCreate, one call to setContentView is all you should have. EDIT: Apparently three times, per Jems's comment.
In main.xml, do you have a Button with an id of test? (i.e. android:id="#+id/test")
Where is showTrafficButton defined? Are you sure that shouldn't be:
Button showTrafficButton = (Button)this.findViewById(R.id.Test);