0

I'm trying to make 3 tabs with fragments from TabHost with Fragments and FragmentActivity and find problems with my intent in 3rd tab. I tried this method Android Remove arguments to match "intent()" but nothing change. it's still error.

    package com.spamcity;

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

public class postingActivity extends Fragment {

    private ImageButton infrastructure;
    private ImageButton trafficjam;
    private ImageButton others;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
            // Inflate the layout for this fragment
            View V = inflater.inflate(R.layout.posting_activity, container, false);

        setupVariables();
        addListenerOnButton();
        return V; 
    }

    public void addListenerOnButton() {
        Intent intent = new Intent(getBaseContext(), writeActivity.class); // error
        // want me to create method getBaseContext()

    // infrastructure
        infrastructure.setOnClickListener(new OnClickListener() { 
            @Override
            public void onClick(View arg0) {
                startActivity(intent); //error
                finish(); 
            } 
        });

    // traffic jam
        trafficjam.setOnClickListener(new OnClickListener() {            
            @Override
            public void onClick(View arg0) {
                startActivity(new Intent(postingActivity.this, writeActivity.class)); // error
                finish();
            } 
        });

    // others
        others.setOnClickListener(new OnClickListener() {            
            @Override
            public void onCzlick(View arg0) {
                startActivity(new Intent(postingActivity.this, writeActivity.class)); // error
                finish();
            }

        });

    }

    protected void finish() {
        // TODO Auto-generated method stub
        // they require this method to be exist
        // but I dont know what should I write here
    }

    private void setupVariables(){
        infrastructure = (ImageButton) findViewById(R.id.btnInfra);
        trafficjam = (ImageButton) findViewById(R.id.btnTrafJam);
        others = (ImageButton) findViewById(R.id.btnOther);
    }
}

I don't understand what went wrong here. thanks for trying to help.

Community
  • 1
  • 1
Hikmah Az
  • 27
  • 1
  • 1
  • 6

2 Answers2

0

Try getActivity() Rather than getBaseContext()

Abhishek
  • 122
  • 7
  • thanks! the errors in startActivity all gone now. I was wondering did you know why they ask to create it methods for finish() & findViewById(int) ? it said the method is undefined. – Hikmah Az Jun 05 '15 at 17:39
  • Then use get activity.finish() and v.findViewById() – Abhishek Jun 05 '15 at 17:40
  • activity.finish() doesn't work and I tried this -- View v = getLayoutInflater().inflate(R.layout.posting_activity, null); -- but the line's error. – Hikmah Az Jun 05 '15 at 17:54
  • sorry it was getActivity.finish(); and v=inflater.inflate(R.layout.posting_activity, container, false); use this – Abhishek Jun 05 '15 at 18:08
  • I tried, but getActivity() doesn't work either. and for the v, it's -- View v=inflater.inflate(R.layout.posting_aduan, container, false); -- , right? this line gets error for the 'inflater' and 'container' part, but the ImageButton part works with v. – Hikmah Az Jun 05 '15 at 18:17
  • getActivity requires local variable named getActivity. same goes to container in v. – Hikmah Az Jun 05 '15 at 18:39
0
   import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.widget.ImageButton;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;

public class postingActivity extends Fragment {

    private ImageButton infrastructure;
    private ImageButton trafficjam;
    private ImageButton others;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
            // Inflate the layout for this fragment
            View V = inflater.inflate(R.layout.posting_activity, container, false);
            infrastructure = (ImageButton) V.findViewById(R.id.btnInfra);
            trafficjam = (ImageButton) V.findViewById(R.id.btnTrafJam);

            others = (ImageButton) V.findViewById(R.id.btnOther);
        addListenerOnButton();
        return V; 
    }

    public void addListenerOnButton() {
        final Intent intent = new Intent(getActivity(), writeActivity.class); // error
        // want me to create method getBaseContext()

    // infrastructure
        infrastructure.setOnClickListener(new OnClickListener() { 
            @Override
            public void onClick(View arg0) {
                startActivity(intent); //error
                getActivity().finish(); 
            } 
        });

    // traffic jam
        trafficjam.setOnClickListener(new OnClickListener() {            
            @Override
            public void onClick(View arg0) {
                startActivity(new Intent(getActivity(), writeActivity.class)); // error
                getActivity().finish();
            } 
        });

    // others
        others.setOnClickListener(new OnClickListener() {            
            @Override
            public void onClick(View arg0) {
                startActivity(new Intent(getActivity(), writeActivity.class)); // error
               getActivity().finish();
            }

        });

    }




}

this works properly try this

Abhishek
  • 122
  • 7