Problema setoncliklistener en un fragment
Publicado por Juan Jose (14 intervenciones) el 31/05/2016 13:56:14
Hola a todos, llevo poco en esto de android y me acabo de encontrar un problema, que creo que sera de facil solucion, pero no se porque me esta dando quebraderos de cabeza.
he creado una activity en la que hay un boton que al pulsarlo abre un fragment en el que hay otro boton
la cuestion es que al pulsar en el segundo boton debera de aparecer una tostada diciendo hola mundo.
Pues buen me da este error en el oncliklistener y no se por donde metele mano, la cuestion es que ni no implemento el metodo setonclick funciona:
El error es este:
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at com.example.cursodam.myapplication.Fragment2.onCreateView(Fragment2.java:36)
El codigo de la activiy:
EL codigo del fragment:
y los Layout de MainActivity:
y del fragment:
espero vuestra ayuda porque estoy atascado y no se como seguir
he creado una activity en la que hay un boton que al pulsarlo abre un fragment en el que hay otro boton
la cuestion es que al pulsar en el segundo boton debera de aparecer una tostada diciendo hola mundo.
Pues buen me da este error en el oncliklistener y no se por donde metele mano, la cuestion es que ni no implemento el metodo setonclick funciona:
El error es este:
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at com.example.cursodam.myapplication.Fragment2.onCreateView(Fragment2.java:36)
El codigo de la activiy:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
package com.example.cursodam.myapplication;
import android.support.v4.app.FragmentManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
private Button btfragment;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btfragment=(Button)findViewById(R.id.buttonfrag2);
btfragment.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Fragment2 fragment2=new Fragment2();
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction().replace(R.id.main, fragment2).commit();
}
});
}
}
EL codigo del fragment:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package com.example.cursodam.myapplication;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.Toast;
/**
* A simple {@link Fragment} subclass.
*/
public class Fragment2 extends Fragment{
private Button bt2;
private LinearLayout rootView;
public Fragment2() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
//referencia a la vista
rootView = (LinearLayout) inflater.inflate(R.layout.fragment_fragment2, container, false);
bt2=(Button)rootView.findViewById(R.id.buttonhola);
bt2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getContext(),"Hola Mundo",Toast.LENGTH_LONG);
}
});
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_fragment2, container, false);
}
}
y los Layout de MainActivity:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<?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:id="@+id/main"
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="com.example.cursodam.myapplication.MainActivity">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Fragment2"
android:id="@+id/buttonfrag2"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
y del fragment:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.cursodam.myapplication.Fragment2"
android:background="#4e50d0">
<Button
android:layout_width="231dp"
android:layout_height="64dp"
android:text="Hola"
android:id="@+id/buttonhola"
android:layout_gravity="center" />
</LinearLayout>
espero vuestra ayuda porque estoy atascado y no se como seguir
Valora esta pregunta


0