This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 3 years ago.
I'm trying to make an Intent to start the NewEventActivityunfortunately the app crashejn when the floating action button is pressed. Here is my code:
MainActivity:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void newEventIntent(View v){
Intent intent = new Intent(this, NewEventActivity.class);
startActivity(intent);
}
}
activity_main.xml:
<android.support.design.widget.FloatingActionButton
android:onClick="newEventIntent"
android:id="#+id/add_event_fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:layout_marginRight="20dp"
android:layout_marginBottom="20dp"
android:src="#drawable/baseline_add_24"/>
LogCat:
java.lang.RuntimeException: Unable to start activity componentInfo{com.example.deadline/com.example.deadline.NewEventActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.design.widget.FloatingActionButton.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
I made sure that both activities are in the manifests.
The reason is that you are trying to set a listener to a FloatingActionButton that it does not have reference in your layout.
You should find it by id :
Add private FloatingActionButton mFloatingActionButton;
and
floatingActionButton=findViewById(R.id.add_event_fab);
public class MainActivity extends AppCompatActivity {
private FloatingActionButton mFloatingActionButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
floatingActionButton=findViewById(R.id.add_event_fab);
}
public void newEventIntent(View v){
Intent intent = new Intent(this, NewEventActivity.class);
startActivity(intent);
}
}
Related
I'm trying to start another activity by pressing on the cardview which has a friend finder id. But when I write home.java it gives me problems in the setOnClickListener. At homeActivity it tells me Cannot resolve method 'homeActivity' in 'HomeActivity'. because?
public class HomeActivity extends AppCompatActivity {
private CardView btn_home;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn_home = findViewById(androidx.appcompat.R.id.home);
btn_home.setOnClickListener(v -> homeActivity(new Intent(HomeActivity.this, TrovamicoActivity.class)));
}
btn_home.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(HomeActivity.this, TrovamicoActivity.class);
startActivity(intent);
}
});
If there is no code in the manifest, write it
<activity android:name=".TrovamicoActivity" />
This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 3 years ago.
Alright I'm trying to display data called by an api on a recyclerview and I get the error. It has nothing to do with butterknife I think. Please help.
Caused by: java.lang.NullPointerException: Attempt to invoke virtual
method 'void
androidx.recyclerview.widget.RecyclerView.setLayoutManager(androidx.recyclerview.widget.RecyclerView$LayoutManager)'
on a null object reference
at com.example.myflickrproject.PhotoDisplayView.onCreate(PhotoDisplayView.java:40)
public class PhotoDisplayView extends AppCompatActivity { //implements AdapterView.OnItemClickListener
#BindView(R.id.recView)
RecyclerView rViewDis;
#BindView(R.id.addToDatabse)
Button download;
#BindView(R.id.backButton)
Button goBack;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_photo_display_view);
System.out.println("My photolist is " + DataCollection.photoList);
rViewDis.setLayoutManager(new GridLayoutManager(this, 3));
PhotoAdapter myAdapter = new PhotoAdapter(this, DataCollection.photoList);
rViewDis.setAdapter(myAdapter);
ButterKnife.bind(this);
}
#OnClick({R.id.addToDatabse, R.id.backButton})
public void onClick(View v) {
int btns = v.getId();
switch (btns) {
case R.id.addToDatabse:
addToDatabse();
break;
case R.id.backButton:
backToMenu();
break;
}
}
public void addToDatabse() {
}
public void backToMenu() {
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
}
}
Bind your views after setContentView(R.layout.activity_photo_display_view);
So, your code should be like this
setContentView(R.layout.activity_photo_display_view);
ButterKnife.bind(this);
I am new to programing and I am trying to clean up my main class.
There is just too much going on there.
I was wondering if it is possible to set up a way that 2 classes were to control 1 xml file.
For now I started doing this, but it didnt work:
public class MainActivity extends AppCompatActivity{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
....SOME CODE....
getParsha();
}
}
public void getParsha() {
new Parsha();
}
In new Parsha I had this code:
public class Parsha extends AppCompatActivity {
Parsha() {
setContentView(R.layout.activity_main);
....SOME CODE.....
}
}
I also tried doing this in onCreate() and it didnt work.
I am not sure why, is this even allowed to do in Android.
Please Note: I didnt get any error, it simply just didnt process the code, no Log or anything.
Thank you.
When you call the Activiy shoud use Intent
Like this
intent = new Intent(MainActivity.this, Parsha.class);
startActivity(intent);
finish();//if use need to stop use first activity.
And then, In your second activity namely Parsha, why you are set content view in inside constructor
You should give this in onCreate block only
setContentView(R.layout.activity_main);
Your two activity should be in following structure
public class MainActivity extends AppCompatActivity{
#Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);
....SOME CODE....
getParsha();
}
public void getParsha() {
intent = new Intent(MainActivity.this, Parsha.class);
startActivity(intent);
}
}
Second Activity
public class Parsha extends AppCompatActivity{
#Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
....SOME CODE....
}
}
Rather than using xml file in both java files why dont you just make the common variables static and keep the code in Parsha.java, use the class wherever inside onCreate(). The following example might give you a clear idea.
MainActivity.java
public class MainActivity extends AppCompatActivity {
ListView list;
ArrayAdapter<String> adap;
static ArrayList<String> arr;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
list = (ListView) findViewById(R.id.list);
arr = new ArrayList<String>();
new Parsha();
arr.add("Hello");
arr.add("There");
adap = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,arr);
list.setAdapter(adap);
}
}
Parsha.java
public class Parsha extends AppCompatActivity{
Parsha(){
arr.add("This");
arr.add("Works");
}
}
activity_main.xml
<?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">
<ListView
android:id="#+id/list"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
I am trying to make a button on my homepage of an app that will lead to a search page, that will have a handful more buttons leading to other pages. However, I used the same code from my activity main for the button in my second page (seachpage) and now when I run the code, my first button on my main page, when clicked it just shuts the app down. I don't even know how to approach this properly because I copied the same code, just changed the "findViewById" and the "startActivity" accordingly with their new labels. Any recommendation or help would be massively appreciated!
Activity main Java code:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button yourButton = (Button) findViewById(R.id.TranslateButton);
if (yourButton == null) throw new AssertionError();
yourButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, SearchPage.class));
}
});
}
}
Activity main xml for the button:
Secondary page (searchpage) Java code:
public class SearchPage extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search_page);
Button accommodationButton = (Button) findViewById(R.id.accommodationButton);
if (accommodationButton == null) throw new AssertionError();
accommodationButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
startActivity(new Intent(SearchPage.this, Accommodation.class));
}
});
}
}
Secondary page xml for the button:
<Button
android:layout_width="match_parent"
android:layout_height="0dp"
android:text="#string/accommodation"
android:id="#+id/accommodationButton"
android:layout_below="#+id/search_bar"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_weight="1"/>
Thank you again for taking the time and consideration to read and/or respond to my question~!
This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 6 years ago.
I am trying to call intent I set putExtra in first Activity as follows.
In first activity
public void onClick(View v) {
int position = getAdapterPosition();
final Intent intent;
intent = new Intent(c, WhenCardClicked.class);
intent.putExtra("title",titles[position]);
intent.putExtra("desc",details[position]);
intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
c.startActivity(intent);
}
In WhenCardClicked.java
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_when_card_clicked);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
TextView textView = (TextView) findViewById(R.id.title);
TextView textView1 = (TextView) findViewById(R.id.desc);
String title ="";
String desc = "";
title = getIntent().getExtras().getString("title");
desc = getIntent().getExtras().getString("desc");
Log.d("Debugtext", title+" "+desc);
if(title != null)textView.setText(title);
}
In content_card_demo.xml
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/title"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/desc"
android:layout_below="#+id/title"/>
I am calling intent in first activity. But I am getting
Null pointer exceptioava.lang.NullPointerException: Attempt to invoke
virtual method 'void
android.widget.TextView.setText(java.lang.CharSequence)' on a null
object reference at
quickbook.com.recyclerlayout.WhenCardClicked.onCreate(WhenCardClicked.java:28)
For other people encoutering this:
Okay, the nullpointer is on one of your TextViews. Maybe because you setContentView to "activity_when_card_clicked" but the layouts name is "content_card_demo.xml".
You just referenced the wrong layout in setContentView().