Carlos Dayan
Carlos Dayan

Reputation: 3

Open an activity from a fragment (crashes)

I've been trying to open an activity from a fragment using the OnClick element in a TableRow but when I run the application it closes when trying to open the activity.

This is the Java code file:

package com.hello.turidf;

import com.hello.turidf.R;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class TabsIndexM001Help extends Fragment {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.m_001_help, container, false);

    return rootView;
}

public void open_m001_map(View view) {
    Intent openmap = new Intent(getActivity(),M001MapActivity.class);
    startActivity(openmap); 
    }

}

This is the XML code file (fragment):

<?xml version="1.0" encoding="utf-8"?>
<ScrollView
.......>

    <TableLayout
    .......>

        <TabelRow
        android:onClick="open_m001_map"
        ....>
             ........
        </TableRow>

    </TableLayout>
</ScrollBiew>

In 'M001MapActivity' activity has not yet been modified, has the code created by default.

Upvotes: 0

Views: 101

Answers (1)

Mou
Mou

Reputation: 2137

android:onClick execute the method passed like parameter in Activity and not in Fragment. If you want execute this method in fragment, remove this line and set OnClickListener programatically on fragment.

Upvotes: 3

Related Questions