Call a new activity through a fragment

2

I have the following code:

Fragment:

public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment


    View i = inflater.inflate(R.layout.fragment_meus_veiculos, container, false);

    return i;

}

public void ChamaCadastroVeiculo(View v)
{
    Intent intent = new Intent(getActivity(), CadastroVeiculo.class);
    startActivity(intent);
}

XML Layout:

    <Button
          android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="NOVO VEICULO"
    android:id="@+id/btNovoVeiculo"
    android:layout_marginTop="57dp"
    android:layout_alignParentTop="true"
    android:layout_alignParentStart="true"
    android:onClick="ChamaCadastroVeiculo" />

Manifest:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="br.net.alexandrelima.postoonline">

<!-- To auto-complete the email text field in the login form with the user's emails -->
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.READ_PROFILE" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.INTERNET" />

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity
        android:name=".LoginActivity"
        android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name=".Inicio" />
    <activity
        android:name=".MainActivity"
        android:label="@string/title_activity_main"
        android:theme="@style/AppTheme.NoActionBar" />
    <activity android:name=".CadastroVeiculo" />
    <activity android:name=".CadastroAbastecimento" />
    <activity android:name=".CadastroActivity"></activity>

</application>

But when I click the button it does not call the new activity and it throws an error:

  

03-18 15: 01: 27,177 19877-19877 / br.net.alexandrelima.postoonline   E / AndroidRuntime: FATAL EXCEPTION: main                                                                                     Process: br.net.alexandrelima.postoonline, PID: 19877                                                                                     java.lang.IllegalStateException: Could not find method   ChamaCadastroVeiculo (View) in a parent or ancestor Context for   android: onClick attribute defined on view class   android.support.v7.widget.AppCompatButton with id 'btNovoVeiculo'                                                                                         at   android.support.v7.app.AppCompatViewInflater $ DeclaredOnClickListener.resolveMethod (AppCompatViewInflater.java:325)                                                                                         at   android.support.v7.app.AppCompatViewInflater $ DeclaredOnClickListener.onClick (AppCompatViewInflater.java:284)                                                                                         at android.view.View.performClick (View.java:4438)                                                                                         at android.view.View $ PerformClick.run (View.java:18422)                                                                                         at android.os.Handler.handleCallback (Handler.java:733)                                                                                         at android.os.Handler.dispatchMessage (Handler.java:95)                                                                                         at android.os.Looper.loop (Looper.java:136)                                                                                         at android.app.ActivityThread.main (ActivityThread.java:5001)                                                                                         at java.lang.reflect.Method.invokeNative (Native Method)                                                                                         at java.lang.reflect.Method.invoke (Method.java:515)                                                                                         at   com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run (ZygoteInit.java:785)                                                                                         at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:601)                                                                                         at dalvik.system.NativeStart.main (Native Method)

    
asked by anonymous 18.03.2016 / 20:02

2 answers

1

Try the following:

In your xml statement, put the following:

<SeuLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
tools:context="br.net.alexandrelima.postoonline.SuaFragment" 
....
>

In order for your xml to know what the context is!

    
18.03.2016 / 20:11
1

I usually make the call as follows:

getActivity.startActivity(intent);

that will call the activity that has its fragment ...

    
18.03.2016 / 21:51