This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 6 years ago.
I'm trying to make an app with different intents. I've got two of the ready. I want that a button in the 1st intent makes the 2nd intent to appear.
The issue is that the button is always set to null and throws a NullPointerException when the app is run
My .java classes are:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(vocabulario);
//....
Button buttonVocabulary = (Button) findViewById(R.id.buttonVocabulary);
Button holaPlayerBtn = (Button) findViewById(R.id.holaPlayerBtn);
//...
buttonVocabulary.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View view)
{
//Se abre un nuevo Intent con el primer botón
try
{
{
Intent myIntent = new Intent(view.getContext(), Vocabulario.class);
startActivityForResult(myIntent, 0);}
} catch (Exception e) {
e.printStackTrace();
}
}});
My main.xml file is:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:id="#+id/adViewContainer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true"
/>
<Button
android:id="#+id/buttonVocabulary"
android:text="#string/vocabulario"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:nestedScrollingEnabled="true" />
</LinearLayout>
</LinearLayout>
In my AndroidManifest.xml there is an element that says:
<activity android:name=".Vocabulario"
android:label="#string/vocabulario"/>
There's something I'm doing wrong. Could you help me with this issue?
Thanks in advance.
change
setContentView(vocabulario);
To
setContentView(R.layout.main);
In your onCreate method you should set the view to the layout:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
...
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main); //set layout id
//...
findViewById(R.id.buttonVocabulary).setOnClickListener(new OnClickListener(){
#Override
public void onClick(View view){
try {
Intent i = new Intent(view.getContext(), Vocabulario.class);
startActivityForResult(i, 0);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
Related
i'm building an app for my project in which it contains a layout which opens the camera and takes photo while other converts text to speech, whenever I click on the button which navigates to text to speech layout , the app is closing and I cannot reach that layout
text to speech code works fine when separately tested in a new project but when linked as a navigating page by button it closes
//code of java //
public class Text extends AppCompatActivity {
Button ak,bk;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.text);
ak=(Button)findViewById(R.id.cam);
ak.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i=new Intent(Text.this,Camera.class);
startActivity(i);
}
});
bk=(Button)findViewById(R.id.spe);
bk.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent k=new Intent(Text.this,Speech.class);
startActivity(k);
}
});
}
//code of xml//
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".major.Text">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<Button
android:id="#+id/spe"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="text to speech"
android:textColor="#2976d5"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="#+id/cam"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="190dp"
android:text="camera"
android:textColor="#2976d5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</LinearLayout>
</RelativeLayout>
//code of text to speech java class
public class Speech extends AppCompatActivity {
Button b1;
EditText ed1;
TextToSpeech t1;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ed1 = (EditText) findViewById(R.id.editText);
b1 = (Button) findViewById(R.id.button);
t1 = new TextToSpeech(getApplicationContext(), new TextToSpeech.OnInitListener() {
#Override
public void onInit(int status) {
if (status != TextToSpeech.ERROR) {
t1.setLanguage(Locale.UK);
}
}
});
b1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String toSpeak = ed1.getText().toString();
Toast.makeText(getApplicationContext(), toSpeak, Toast.LENGTH_SHORT).show();
t1.speak(toSpeak, TextToSpeech.QUEUE_FLUSH,null);
}
});
}
public void onPause() {
if (t1 != null) {
t1.stop();
t1.shutdown();
}
super.onPause();
}
}
//logcat:
2019-10-06 20:15:53.294 20959-21006/com.example.omen.loginmajor
E/eglCodecCommon: glUtilsParamSize: unknow param 0x000082da
2019-10-06 20:15:53.294 20959-21006/com.example.omen.loginmajor
E/eglCodecCommon: glUtilsParamSize: unknow param 0x000082da
the app keeps closing instead of going to another page
Maybe you can change
Intent k=new Intent(Text.this,Speech.class);
to
Intent k=new Intent(getApplicationContext(),Speech.class);
I have my code for a simple activity to return to the main activity once the user clicks on the home button.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_greetings);
// Set up onclicklistener for homeIcon imageview to go to back one activity
ImageView homeIcon = (ImageView) findViewById(R.id.home_icon);
homeIcon.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
finish();
}
});
}
This is a java file for a single activity, greetings. I want this home button + functionality for many other activities, but I do not want to copy and paste. Should I make another java class, and implement the function inside of it? I tried, but it can not findviewbyid.
Without your code really hard understand why you can not find by id, but I can offer this way:
HomeButtonActivity.java
public class HomeButtonActivity extends AppCompatActivity
{
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
Button home_button = (Button) findViewById(R.id.home_button);
home_button.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View view)
{
Toast.makeText(HomeButtonActivity.this, "Replace with your own action", Toast.LENGTH_SHORT).show();
}
});
}
}
Activity1.java
public class Activity1 extends HomeButtonActivity
{
#Override
protected void onCreate(Bundle savedInstanceState)
{
setContentView(R.layout.activity1);
super.onCreate(savedInstanceState);
}
}
activity1.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.myapplication.HomeButtonActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="#style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<include layout="#layout/content_main" />
<include layout="#layout/home_button" />
</android.support.design.widget.CoordinatorLayout>
home_button.xml
<?xml version="1.0" encoding="utf-8"?>
<Button xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/home_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|center"
android:text="home button"/>
create class e.g ReturnClass and create constructor for it, then pass Context as parameter e.g ReturnClass(Context context) then write
Intent intent = new Intent(context , MainActivity.class);
context.startActivity(intent);
from each activity you can call this class and pass activity to it , and ReturnClass return it to main activity
I'm trying to create a menu using clickable images, but they don't seem to be working? I was following a tutorial and it uses something like
Intent biodata = new Intent(mainActivity.this, profile.class);
but it's not working so I tried to look for something else and someone said to use v.getContext() instad of ....this
it works on my other page but it doesn't work on this page?
mainActivity.java
package skripsi.garden;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
public class mainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public ImageButton buttonBio;
public void init(){
buttonBio= (ImageButton)findViewById(R.id.buttonBio);
buttonBio.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent biodata = new Intent(v.getContext(), profile.class);
startActivity(biodata);
}
}
);
}
public ImageButton buttonList;
public void tombollist(){
buttonList=(ImageButton)findViewById(R.id.buttonList);
buttonList.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent listTaman = new Intent(v.getContext(), gardenlist.class);
startActivity(listTaman);
}
}
);
}
public ImageButton buttonWeather;
public void tombolcuaca(){
buttonWeather=(ImageButton)findViewById(R.id.buttonWeather);
buttonWeather.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent cuaca = new Intent(v.getContext(), weather.class);
startActivity(cuaca);
}
}
);
}
}
and this is the xml
activity_main.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"
android:background="#drawable/foliagemain"
tools:context="skripsi.garden.mainActivity">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageView3"
android:src="#drawable/selamatdatang"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/buttonBio"
android:src="#drawable/buttonbiodata"
android:layout_above="#+id/buttonList"
android:layout_alignRight="#+id/buttonWeather"
android:layout_alignEnd="#+id/buttonWeather" />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/buttonList"
android:src="#drawable/buttondaftartaman"
android:layout_above="#+id/buttonHelp"
android:layout_alignLeft="#+id/buttonHelp"
android:layout_alignStart="#+id/buttonHelp" />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/buttonHelp"
android:src="#drawable/buttonhelp"
android:layout_above="#+id/buttonWeather"
android:layout_alignLeft="#+id/buttonBio"
android:layout_alignStart="#+id/buttonBio" />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/buttonWeather"
android:src="#drawable/buttoncuaca"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
Put your buttons in your onCreate method. They do not need to be in their own method.
public class mainActivity extends AppCompatActivity {
public ImageButton buttonBio;
public ImageButton buttonList;
public ImageButton buttonWeather;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
buttonBio= (ImageButton) findViewById(R.id.buttonBio);
buttonBio.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent biodata = new Intent(mainActivity.this, profile.class);
startActivity(biodata);
}
});
buttonList=(ImageButton)findViewById(R.id.buttonList);
buttonList.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent listTaman = new Intent(mainActivity.this, gardenlist.class);
startActivity(listTaman);
}
});
buttonWeather=(ImageButton)findViewById(R.id.buttonWeather);
buttonWeather.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent cuaca = new Intent(mainActivity.this, weather.class);
startActivity(cuaca);
}
});
}
}
You could just call your methods init(), tombollist() and tombolcuaca() in onCreate method of your activity after setting the layout.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
tombollist();
tombolcuaca()
}
This would initialise the button to do the action written inside the onClick.
If the methods are not called, the button would not be initialised and hence the click will not work.
You could also use android:clickable="true" in your imageButtons, and then assign the click function init() to the button in your layout xml.
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/buttonBio"
android:src="#drawable/buttonbiodata"
android:layout_above="#+id/buttonList"
android:layout_alignRight="#+id/buttonWeather"
android:layout_alignEnd="#+id/buttonWeather"
android:clickable="true"
android:onClick="init"/>
Then you wouldn't need to initialise the buttons in your activity. On click of the button, the method would be called.
here is my code:
public class MainActivity extends Activity implements View.OnClickListener {
Button b;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b = (Button) findViewById(R.id.button1);
}
#Override
public void onClick(View v) {
switch (v.getId()){
case R.id.button1:
Toast.makeText(getApplicationContext(), "onClick method was called", Toast.LENGTH_LONG).show();
break;
}
}
}
For some reason nothing happens and no toast shows up. Did I miss anything ?
Thanks
write:
b.setOnClickListener(this);
in onCreate:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b = (Button) findViewById(R.id.button1);
b.setOnClickListener(this);
}
you must set a listener to the button for onClick to work
As an alternative of Vipul answer you can also add the attribute onClick in the Button tag in the XML layout file.
Example:
<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 id="#+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="myMethod" />
</RelativeLayout>
And, in your MainActivity.java, remove the View.OnClickListener implemented interface (thanks #donfuxx) add the following method:
public void myMethod(View v) {
//do something
}
I am making tabs in android. Currently this is how my tabs are working. When I click on any tab it will take me to appropriate tab's view which is fine. But lets say when I click on Profile tab then inside this profile view there is a button Add New. When I click on Add New button then it takes me to it's view but it also removes tabs from the view which I don't want. So how can I make tabs always available even when user clicks any link or button inside any view?
Here is my current code
tabs.xml
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<RelativeLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="5dp">
<TabWidget
android:id="#android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true" />
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="5dp" />
</RelativeLayout>
</TabHost>
Tabs.java
public class Tabs extends TabActivity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tabs);
Resources resources = getResources();
TabHost tabHost = getTabHost();
Intent startUseSaldo = new Intent().setClass(this, Usesaldo.class);
TabSpec useSaldo = tabHost
.newTabSpec("UseSaldo")
.setIndicator("Use Saldo")
.setContent(startUseSaldo);
Intent startHistory = new Intent().setClass(this, History.class);
TabSpec history = tabHost
.newTabSpec("History")
.setIndicator("History")
.setContent(startHistory);
Intent startProfile = new Intent().setClass(this, Profile.class);
TabSpec profile = tabHost
.newTabSpec("Profile")
.setIndicator("Profile")
.setContent(startProfile);
// add all tabs
tabHost.addTab(useSaldo);
tabHost.addTab(history);
tabHost.addTab(profile);
tabHost.setCurrentTab(1);
}
}
Profile.java
public class Profile extends Activity {
Button bAddNew;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.profile);
bAddNew = (Button)findViewById(R.id.bAddNew);
bAddNew.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
Intent AddFacility = new Intent(getApplicationContext(), AddFacility.class);
startActivity(AddFacility);
}
});
}
}
AddFacility.java
public class AddFacility extends Activity {
#Override
public void setContentView(int layoutResID) {
// TODO Auto-generated method stub
super.setContentView(R.layout.add_facility);
}
}
Try this example - http://gamma-point.com/content/android-how-have-multiple-activities-under-single-tab-tabactivity
You have to use ActivityGroup with Tabhost
http://richipal.com/post/2624844577
But as ActivityGroups are depericated you should use Fragments with Tabhost