在onActivityCreated中未从intent调用的活动

时间:2015-10-17 15:47:37

标签: android android-fragments android-intent android-fragmentactivity

我的Tab1.java中有按钮,通过单击按钮,它会调用另一个类说Custom.java,但我面临错误:

"Intent cannot be resolved to a type"

我搜索了很多,但没有找到任何有用的帖子。 请帮帮我!

Tab1.java

public class Tab1 extends Fragment
{
    ListView lv;
    int FIRST_OPTION = 1;
    int SECOND_OPTION = 2;

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onActivityCreated(savedInstanceState);
    Button b = (Button) getView().findViewById(R.id.button4);
    registerForContextMenu(b);

    Button bi = (Button) getView().findViewById(R.id.button5);
    registerForContextMenu(bi);

    Button setting = (Button) getView().findViewById(R.id.setting);
    setting.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            startActivity(new Intent(getActivity(), Custom.class));             
        }
    });

    b.setOnCreateContextMenuListener(new OnCreateContextMenuListener() {

        @Override
        public void onCreateContextMenu(ContextMenu menu, View v,
                ContextMenuInfo menuInfo) {
            // TODO Auto-generated method stub
            menu.add(0, FIRST_OPTION, 0, "Activate");

                }
    });
bi.setOnCreateContextMenuListener(new OnCreateContextMenuListener() {

        @Override
        public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
            // TODO Auto-generated method stub

            menu.add(0, SECOND_OPTION, 0, "Activate");
        }
    });

}

@Override
public boolean onContextItemSelected(MenuItem item) {

    int itemId = item.getItemId();
    if(itemId == FIRST_OPTION){
        //Normal_Mode nm = new Normal_Mode();
        //nm.activateNormal(getApplicationContext());
    Toast.makeText(this.getActivity(), "Normal Mode Activated", Toast.LENGTH_SHORT).show();

    return true;

    }
    else if(itemId == SECOND_OPTION){
        //Silent_Mode sm = new Silent_Mode();
        //sm.activateSilent(getApplicationContext());
        Toast.makeText(this.getActivity(), "Silent Mode Activated", Toast.LENGTH_SHORT).show();

        return true;            
    }
    return super.onContextItemSelected(item);
 }
    @Override
    public View onCreateView(LayoutInflater inflater,@Nullable ViewGroup container,
        @Nullable Bundle savedInstanceState) {
        View view =inflater.inflate(R.layout.profile,container,false);
        return view;
    }
}

Custom.java

public class Custom extends Activity{

    private SeekBar mediaVlmSeekBar = null;
    private AudioManager audioManager = null;
    private boolean mphoneisVib;
    Ringtone ringtone;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        this.setVolumeControlStream(AudioManager.STREAM_MUSIC);  
        setContentView(R.layout.custom_sett);
        initControls();

        audioManager= (AudioManager) getSystemService(AUDIO_SERVICE);

        setButtonClickListener();

        Button b = (Button) findViewById(R.id.button6);
        b.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

                Intent inte = new Intent(RingtoneManager.ACTION_RINGTONE_PICKER);
                inte.putExtra(RingtoneManager.EXTRA_RINGTONE_TYPE, 
                        RingtoneManager.TYPE_RINGTONE);
                //String title= ringtone.getTitle(this);
            inte.putExtra(RingtoneManager.EXTRA_RINGTONE_TITLE, "Sle");

                startActivityForResult(inte, 5);
            }           
        });     
    }

   /********************* Code For VIbration *******************************/

    private void setButtonClickListener() {
        // TODO Auto-generated method stub
        ToggleButton tb = (ToggleButton) findViewById(R.id.toggleButton1);
        tb.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

                if(mphoneisVib){
                    audioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
                mphoneisVib=false;
            Toast toast=    Toast.makeText(Custom.this, "Vibration Off !",
                    Toast.LENGTH_SHORT);
                toast.show();
                }
                else{

                    audioManager.setRingerMode(AudioManager.RINGER_MODE_VIBRATE);

                    mphoneisVib=true;

                    Toast toast=Toast.makeText(Custom.this, "Vibration On !",
                    Toast.LENGTH_SHORT);
                    toast.show();                   
                }                   
            }
        });
    }
    /********************          End        *************************/

    /******************** Code For Volume Seekbar *********************/
    private void initControls() {
        // TODO Auto-generated method stub

        audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE); 
        mediaVlmSeekBar = (SeekBar) findViewById(R.id.seekBar1);
        mediaVlmSeekBar.setMax(audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC)); 
        //Set the progress with current Media Volume 
        mediaVlmSeekBar.setProgress(audioManager.getStreamVolume(AudioManager.STREAM_MUSIC));

        try { mediaVlmSeekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {    
            public void onStopTrackingTouch(SeekBar arg0) {         }

            @Override
            public void onProgressChanged(SeekBar seekBar, int progress,
                    boolean fromUser) {
                // TODO Auto-generated method stub

                audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, progress, 0);                   
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {                 
            } 
        });

        }
        catch (Exception e) 
        {e.printStackTrace(); } 
    }
             /***********************  End  ***************************/

2 个答案:

答案 0 :(得分:1)

Hye,请尝试这种方式。

setting.setOnClickListener(new OnClickListener() {

 @Override
 public void onClick(View arg0) {
   /startActivity(new Intent(getActivity(), Custom.class));   
    Intent intent = new Intent((Tab1)getActivity(), Custom.class);
    (Tab1)getActivity().startActivity(intent);    

    }
});

答案 1 :(得分:1)

有一些可以解决问题的快速提示:

1)您是否确保在Android清单中注册了您的自定义活动?

2)不要直接在你的片段中调用0,使用一个界面与持有你的片段的活动进行通信并在那里进行startActivity,这种方式更干净

3)不要使用startActivityCustom使用Tab1CustomActivity等类名称,请遵循Android代码风格指南。

Code guidelines

Communication Fragment-Activity

请记住,如果有一天,你的老板或某​​人告诉你"那么,现在我们要为平板电脑设备制作一个版本"这段代码需要进行大量更改,这就是为什么您要将片段与您的活动进行沟通,并在那里执行逻辑。

告诉我您是否有任何问题,或者问题是否得到解决。