0

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.

Pedro Santangelo
  • 143
  • 2
  • 13

3 Answers3

1

change

setContentView(vocabulario);

To

setContentView(R.layout.main);
mdDroid
  • 3,135
  • 2
  • 22
  • 34
  • Changed it, but still getting NullPointerException – Pedro Santangelo May 31 '16 at 10:52
  • What error you are getting now – mdDroid May 31 '16 at 10:52
  • I am getting same error in a different line: – Pedro Santangelo May 31 '16 at 11:00
  • In Which line you are getting error , can you please post the logcat – mdDroid May 31 '16 at 11:03
  • 05-31 12:11:03.067 15036-15036/com.visit.slovenia.hd E/AndroidRuntime: FATAL EXCEPTION: main Process: com.visit.slovenia.hd, PID: 15036 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.visit.slovenia.hd/com.visit.slovenia.hd.VisitSloveniaHD}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' o – Pedro Santangelo May 31 '16 at 12:46
0

In your onCreate method you should set the view to the layout:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    ...
}
Chol
  • 2,097
  • 2
  • 16
  • 26
0
@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();
            }
        }
    });

}
Ozgur
  • 3,738
  • 17
  • 34
  • Changed it, but still getting NullPointerException – Pedro Santangelo May 31 '16 at 10:52
  • 05-31 12:11:03.067 15036-15036/com.visit.slovenia.hd E/AndroidRuntime: FATAL EXCEPTION: main Process: com.visit.slovenia.hd, PID: 15036 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.visit.slovenia.hd/com.visit.slovenia.hd.VisitSloveniaHD}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference – Pedro Santangelo May 31 '16 at 11:03