Calling a new Activity through a Fragment

Asked

Viewed 1,454 times

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 cancels 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)

  • That one Button is even inside the xml of Fragment?

2 answers

1

Try the following:

In the statement of his xml 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" 
....
>

So that your xml know what the context is!

  • Ja esta certo:&#xA;<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"&#xA; xmlns:tools="http://schemas.android.com/tools"&#xA; android:layout_width="match_parent"&#xA; android:layout_height="match_parent"&#xA; tools:context="br.net.alexandrelima.postoonline.Mainfragment">

1

I usually make the call the following way:

getActivity.startActivity(intent);

that there will make this call by Activity who has your Fragment...

Browser other questions tagged

You are not signed in. Login or sign up in order to post.