My app is unable to start activity? - java

When i try to run the app on my phone,it force closes when i try to do activity 3.
Logcat says:
01-13 17:53:25.368: E/AndroidRuntime(3235): Caused by: java.lang.NullPointerException
01-13 17:53:25.368: E/AndroidRuntime(3235): at android.app.activity3.onCreate(activity3.java:18)
where line 18 is
Button wg = (Button) findViewById(R.id.Back);
heres my full code for activity3.java:
package android.app;
import android.app.R;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class activity3 extends Activity{
/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main3);
Button wg = (Button) findViewById(R.id.Back);
wg.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent intent = new Intent();
setResult(RESULT_OK, intent);
finish();
}
});
}
}
Thanks in advance

You need to modify your 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"
>
<!-- your textview -->
<TextView
android:id="#+id/text1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/text1"
android:textAppearance="?android:attr/textAppearanceMedium"
/>
<!-- your back button -->
<Button
android:id="#+id/back"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/Back"
/>
</RelativeLayout>
What I did:
Removed text and orientation from your root RelativeLayout, these do nothing.
Modified your height and width, feel free to change these as you please.
Added a Button with id of R.id.back and text of #string/Back.
You will now be able to reference your button using findViewById(R.id.back) and set your click listener.

I don't see a button in the xml. And once you have that, it needs to have:
android:id="#+id/Back"

This is happening because you don't have a button declared in your view. Only a Textview. You must create the button in the view. You're referencing nothing hence the null.
You can find a nice example of how to make a button here.

You don't have button with id "Back" in xml posted, that is why you are getting null there. Add button entry in your xml.

Related

Android Button throws IllegalStateException

So I'm trying to build the base of a simple app where I can switch activities with a button click. I have this working fine for my main activity to my second activity; however, I can't get it to work going back to the main activity.
Currently I am getting this error when I click the button: java.lang.IllegalStateException: Could not find method NewBack(View) in a parent or ancestor Context for android:onClick attribute defined on view class com.google.android.material.button.MaterialButton with id 'back'
Heres the code in the second activity:
package com.example.homework4;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class CreateQuestion extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_create_question);
}
public void NewBack(View v){
setContentView(R.layout.activity_main);
}
}
Here is the code for the button in my xml file:
<Button
android:id="#+id/back"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:layout_marginEnd="153dp"
android:onClick="NewBack"
android:text="GO BACK"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="#+id/submit" />
I have also tried setting an onClickListener with the same results. I'm sure this is an easy fix, I just can't seem to figure it out!
If You want to navigate between activities you need to replace
public void NewBack(View v){
setContentView(R.layout.activity_main);
}
with
public void newBack(View v){
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
}
P.S. don't use Upercase in function name. So if the function is called newBack
change it also in XML file like:
<Button
android:id="#+id/back"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:layout_marginEnd="153dp"
android:onClick="newBack"
android:text="GO BACK"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="#+id/submit" />

How do i create a button to another page for my app

i was just wondering how i would make a button on my page so that it would go to another page for my app. I am a beginner so if you could explain how it all works and where it goes it would help a lot.
PS i am using android studio if that makes a difference and this is the code i have so far in my fragment_main.xml. I have not entered any code into the .java
<TextView android:text="#string/hello_world"
android:id="#id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ImageButton
android:id="#+id/firstbutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/homebutton"
android:layout_below="#+id/text"/>
You can declare views and objects dynamically in Java, and then pass the button from fragment to fragment (or Activity to Activity, depending on your app).
To declare a Relative Layout with a Button, for example:
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.RelativeLayout;
public class JavaLayoutActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Button myButton = new Button(this);
RelativeLayout myLayout = new RelativeLayout(this);
myLayout.addView(myButton);
setContentView(myLayout);
}
This doesn't set the properties or anything, I am just using this as proof of concept.
XML makes things easy with regards to user interface design because you don't have to manage it in code, but this is one case where it is an exception. If you want dynamic interface objects, you need to use Java.
Instead of creating view dynamically you should obtain your view in activity
ImageButton button = (ImageButton) findViewById(R.id.firstButton)
and assign an onClick listener to id
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// start new Activity here
}
});
You can also do it in xml:
<ImageButton
android:id="#+id/firstbutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/homebutton"
android:onClick="sendMessage"
android:layout_below="#+id/text"/>
with such configuration, you should add method in activity:
public void sendMessage(View view) {
// start another activity here
}
There are two methods available in android using which you can go from one Activity to another.
1. Use button.setOnClickListener()
Create a button in xml file.
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
Now set event listener for the button in your .class file
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//set the event you want to perform when button is clicked
//you can go to another activity in your app by creating Intent
Intent intent = new Intent(getApplicationContext, Activity2.class);
startActivity(intent);
}
});
2. Use <android:onClick="goNext">
Put the onClick as the attribute of the button you have created in xml file.
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
android:onClick="goNext" />
Now in your .class file define an event for that button as,
goNext() {
Intent intent = new Intent(getApplicationContext, Activity2.class);
startActivity(intent);
}

Programmatically close dialog opened by theme.dialog

I have two xml files, and two java files. In the first xml I have few buttons, and one of them is EXIT. In the java file i write in the onCreate:
Button exitButton = (Button) this.findViewById(R.id.button_exit);
exitButton.setOnClickListener(this);
Then further down the code I write:
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button_exit:
Intent switchtoExit = new Intent(StartActivity.this, ExitActivity.class);
startActivityForResult(switchtoExit, MESSAGE_REQUEST);
break;
}
}
the second java files is called ExitActivity.java. In the manifest file I wrote:
<activity android:name=".ExitActivity"
android:label="#string/exit_title"
android:theme="#android:style/Theme.Dialog"/>
In order to make the second xml file popup like a dialog.
My second java file is(the one that popup like a dialog):
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class ExitActivity extends Activity implements OnClickListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
this.setContentView(R.layout.activity_exit);
Button noExitButton = (Button) this.findViewById(R.id.exit_no_button);
noExitButton.setOnClickListener(this);
Button yesExitButton = (Button) this.findViewById(R.id.exit_yes_button);
yesExitButton.setOnClickListener(this);
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.exit_no_button:
Toast.makeText(this, "Good Choice :-D", Toast.LENGTH_SHORT).show();
break;
case R.id.exit_yes_button:
Toast.makeText(this, "So sad... \nnice playing with you...", Toast.LENGTH_SHORT).show();
break;
//break;
}
}
}
and my second java file has:
<?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"
android:background="#color/red">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="#+id/exitTV"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/exit_body"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:id="#+id/exit_no_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/exit_no_button"
android:layout_weight="1.0"
/>
<Button
android:id="#+id/exit_yes_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/exit_yes_button"
android:layout_weight="1.0"
/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
What I wanted to do is to have one button close the dialog, and the other button to stop the application. I tried finish(), and it worked just fine. but then when I added the onDestroy() method one button would restart the application, and the other just close it. Also, when I pressed the Back button, it would close the application.
Can anyone explain me how to get the following to happen:
When the dialog pops up, and I press the back button, it just closes the dialog?
When I press on exit button, it closes the application.
When I press on the Stay button it closes the dialog.
Thanks
You're doing this the wrong way. Using a second activity is overkill. Use an AlertDialog, which you can easily make with AlertDialogBuilder. Set it to have a positive and a negative button. Set an OnClickListener for the positive button that calls finish on the only activity. There you go- 1 activity, and the dialog class itself will take care of popping up and closing itself.

Using onClick in button to a view

Just started learning Android this morning, need help on this. I have few buttons in my App, I want when a user clicks a button a image will be shown and back button to load main.xml .
Code:
In main.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"
android:orientation="vertical" >
<Button
android:layout_marginTop="40dp"
android:layout_width="100dp"
android:layout_height="80dp"
android:id="#+id/b1"
android:text="xyz"
android:background="#ff3375"
android:layout_marginLeft="20dp"
/>
<Button
android:layout_marginTop="40dp"
android:layout_width="100dp"
android:layout_height="80dp"
android:id="#+id/b2"
android:layout_toRightOf="#id/b1"
android:text="abc"
android:background="#ff3375"
android:layout_marginLeft="80dp"
/></RelativeLayout>
And in Activity.java
package com.sam;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class A2Activity extends Activity {
/** Called when the activity is first created. */
Button a,b;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
a= (Button) findViewById(R.id.b1);
b= (Button) findViewById(R.id.b2);
a.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
b.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
}
});}}
Now, what do I need to add inside the onCLick method to open a image or a XML file and also a back button to return to main.xml
please explain your question a little further . what do you mean open an image or xml file? do you mean to create a bitmap ? do you mean to show it? if so , where? also , what do you mean that the back button will return to main.xml? main.xml is a layout file , not an activity .
in any case, maybe you meant that you wish to open an image in full screen upon pressing on the button ,and when clicking on the back button of the device go back to the activity you've created?
if so , you can create a new class that extends Activity , update the manifest so that it would be reachable , and start it (using startActivity) . in the new activity class , set the content view to be of an ImageView that shows an image , or of a layout file that has an imageView that shows an image .
for starting a new activity , you need to call :
startActivity(new Intent(CurrentActivity.this, NewActivity.class);
where "CurrentActivity" is the current activity that you are at (called "A2Activity" in your sample) and "NewActivity" is the one that shows the image.
as written before , do not forget to update the manifest.

Can I use setContentView outside the oncreate method?

I have seen many people telling that you can set setContentView outside the oncreate method, but I didn't find anywhere an example. Now when I try to use setContentView, my app just crashes. Here is my source code:
AlarmActivity.java:
package com.alarm.example;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.RelativeLayout;
public class AlarmActivity extends Activity{
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Button buton = new Button(this);
buton.setId(101);
buton.setText("New alarm");
buton.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
RelativeLayout layout1 = new RelativeLayout(this);
layout1.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
layout1.addView(buton);
setContentView(layout1);
Button nou =(Button)findViewById(101);
nou.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v){
New_ala nou1=new New_ala();
nou1.nou_alarma();
}
});
}
}
New_ala.java:
package com.alarm.example;
public class New_ala extends AlarmActivity{
public void nou_alarma() {
setContentView(R.layout.timepicker);
}
}
TimePicker.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" >
<TimePicker
android:id="#+id/timePicker"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_alignParentBottom="true">
<Button
android:id="#+id/picker_save"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Save">
</Button>
<Button
android:id="#+id/picker_cancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Cancel">
</Button>
</LinearLayout>
</LinearLayout>
On more detail, I can use setContentView(R.layout.timepicker) inside the oncreate method without any problems so the problem is that setContentView isn't working properly inside the New_ala.java class. Can anyone help me?
The code after setting the intent:
AlarmActivity:
package com.alarm.example;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
public class AlarmActivity extends Activity{
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
startActivity(new Intent(this, NewAlarmActivity.class));
}
}
NewAlarmActivity:
package com.alarm.example;
import android.os.Bundle;
public class NewAlarmActivity extends AlarmActivity{
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.timepicker);
}
}
You can call setContentView any time you are running on the event (UI) thread. Be aware that when you do, any fields you initialized by calling findViewById will need to be reset.
The best way to do that is to have multiple activities: instead of nou1.nou_alarma();, create an intent and start a new activity with the new layout.
startActivity(new Intent(this, NewAlarmActivity.class));
And in the onCreate method of NewAlarmActivity, set the content view to R.layout.timepicker
The setContentView() method can be called only once per activity. If you want to completely change the layout at some point you should either go for ViewFlipper or have 2 layouts in the activity and show only one of them at given time by calling view.setVisibility(View.GONE); and view.setVisibility(View.VISIBLE); respectively.

Categories