Android:Intent要求删除参数& getBaseContext()请求创建它的方法

时间:2015-06-05 17:07:45

标签: android android-intent android-fragments

我正在尝试使用TabHost with Fragments and FragmentActivity中的片段创建3个标签,并在第3个标签中发现我的意图问题。我尝试了这种方法Android Remove arguments to match "intent()",但没有任何改变。它仍然是错误的。

    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);
    }
}

我不明白这里出了什么问题。谢谢你的帮助。

2 个答案:

答案 0 :(得分:0)

尝试     getActivity() 而不是     getBaseContext()

答案 1 :(得分: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();
            }

        });

    }




}

这很正常 试试这个