Android工作室 - 按钮onclick片段错误

时间:2016-09-29 03:14:57

标签: java android android-fragments android-intent

SecondFragment.java

package com.hci.acer.heroesunite;

import android.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;


public class SecondFragment extends Fragment {



@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.trivia, container, false);
    Button button = (Button) view.findViewById(R.id.button1);
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent i;
            i = new Intent(SecondFragment.this, jrizal_trivia.class);
            startActivity(i);
        }
    });
    return view;


}



}

jrizal_trivia.java

package com.hci.acer.heroesunite;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class jrizal_trivia extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_jrizal_trivia);
}
}

MainActivity.java

package com.hci.acer.heroesunite;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v4.app.FragmentManager;
import android.view.View;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;

public class MainActivity extends AppCompatActivity
    implements NavigationView.OnNavigationItemSelectedListener {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);


    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
            this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
    drawer.setDrawerListener(toggle);
    toggle.syncState();

    NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(this);
}

@Override
public void onBackPressed() {
    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    if (drawer.isDrawerOpen(GravityCompat.START)) {
        drawer.closeDrawer(GravityCompat.START);
    } else {
        super.onBackPressed();
    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
    // Handle navigation view item clicks here.
    int id = item.getItemId();
    android.app.FragmentManager fragmentManager = getFragmentManager();
    if (id == R.id.nav_home) {
        fragmentManager.beginTransaction()
                .replace(R.id.content_frame
                        ,new FirstFragment())
                .commit();
    } else if (id == R.id.nav_dyk) {
        fragmentManager.beginTransaction()
                .replace(R.id.content_frame
                        ,new SecondFragment())
                .commit();
    } else if (id == R.id.nav_about) {
        fragmentManager.beginTransaction()
                .replace(R.id.content_frame
                        ,new ThirdFragment())
                .commit();
    } else if (id == R.id.nav_hero1) {
        startActivity(new Intent(this, jose_rizal.class));
    } else if (id == R.id.nav_hero2) {
        startActivity(new Intent(this, andres_bonifacio.class));
    } else if (id == R.id.nav_hero3) {
        startActivity(new Intent(this, emilio_aguinaldo.class));
    } else if (id == R.id.nav_hero4) {
        startActivity(new Intent(this, apolinario_mabini.class));
    } else if (id == R.id.nav_hero5) {

    } else if (id == R.id.nav_hero6) {
        startActivity(new Intent(this, juan_luna.class));
    } else if (id == R.id.nav_hero7) {

    } else if (id == R.id.nav_hero8) {
    }

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    drawer.closeDrawer(GravityCompat.START);
    return true;
}
}

这就是错误

  

错误:(28,31)错误:找不到合适的构造函数   Intent(,Class)构造函数   Intent.Intent(String,Uri)不适用(参数不匹配;    无法转换为String)构造函数   Intent.Intent(Context,Class)不适用(参数不匹配;    无法转换为上下文)

下(SecondFragment.this,jrizal_trivia.class); 错误说。 "无法解析构造函数' intent(com.hci.acer.heroesunite.SecondFragment,java.lang.Class)'

帮助任何人?我想在SecondFragment中调用我的按钮中的另一个活动。感谢

1 个答案:

答案 0 :(得分:2)

i = new Intent(SecondFragment.this, jrizal_trivia.class);

第一个参数需要Context,因此您可以通过Context从片段中获取getActivity()/getContext()

所以应该是这样的:

i = new Intent(getActivity(), jrizal_trivia.class);