从main的不同活动开始Intent

时间:2015-06-07 17:26:10

标签: android android-intent

Android: Call Method from another Class which starts a new Intent

我的问题与此问题相似,并希望实现相机意图,但意图方法与主文件不同,当我开始nullpointerexception时,它会给我startActivityforResult

我已阅读但无法解决这个问题。

请耐心等待我,因为我是Android的新手

主要

   public class MainFragment extends Fragment{
public MainFragment() {
}
private TextView karan;
private CallbackManager mcallbackManager;
AccessTokenTracker tracker;
ProfileTracker profileTracker;
Button btnCapturePicture;
private FacebookCallback<LoginResult> mcallback=new FacebookCallback<LoginResult>() {
    @Override
    public void onSuccess(LoginResult loginResult) {
        Profile profile=Profile.getCurrentProfile();
        try{
            karan.setText(profile.getFirstName());
            System.out.print(profile.getFirstName());
            SystemClock.sleep(10000);
        }
        catch (NullPointerException e){

        }
    }

    @Override
    public void onCancel() {

    }

    @Override
    public void onError(FacebookException e) {

    }
};

public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    FacebookSdk.sdkInitialize(getActivity().getApplicationContext());
    mcallbackManager=CallbackManager.Factory.create();

    tracker=new AccessTokenTracker() {
        @Override
        protected void onCurrentAccessTokenChanged(AccessToken oldaccessToken, AccessToken newaccessToken) {

        }
    };
        ;
   profileTracker=new ProfileTracker() {
        @Override
        protected void onCurrentProfileChanged(Profile profile, Profile newprofile) {
            try{
                karan.setText(newprofile.getFirstName());
                System.out.print(newprofile.getFirstName());
           }
            catch (NullPointerException e){

            }
        }
    };
    tracker.startTracking();
    profileTracker.startTracking();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    return inflater.inflate(R.layout.fragment_main, container, false);
}
public void onViewCreated(View view,Bundle savedInstanceState){
    super.onViewCreated(view, savedInstanceState);
    LoginButton loginButton=(LoginButton) view.findViewById(R.id.login_button);
    btnCapturePicture=(Button) view.findViewById(R.id.button);
    btnCapturePicture.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            camera t=new camera();
            t.dispatchTakePictureIntent();
        }

    });
    loginButton.setReadPermissions("user_friends");
    loginButton.setFragment(this);
    loginButton.registerCallback(mcallbackManager, mcallback);
    setupTextDetails(view);
}



@Override
public void onResume() {
    super.onResume();
    Profile profile=Profile.getCurrentProfile();
    try{
        karan.setText(profile.getFirstName());
        System.out.print(profile.getFirstName());
    }
    catch (NullPointerException e){

    }
}
private void setupTextDetails(View view) {
    karan = (TextView) view.findViewById(R.id.textView);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    mcallbackManager.onActivityResult(requestCode, resultCode, data);
}
public void onStop(){
    super.onStop();
    tracker.stopTracking();
    profileTracker.stopTracking();
}


}

camera.java

    public class camera extends Activity {

static final int REQUEST_IMAGE_CAPTURE = 1;
public void dispatchTakePictureIntent() {
    Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

        startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE); ///Null pointer here

}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
        Bundle extras = data.getExtras();
        Bitmap imageBitmap = (Bitmap) extras.get("data");
        ImageView mImageView=(ImageView) findViewById(R.id.imageView);
        mImageView.setImageBitmap(imageBitmap);
    }
}
 }

日志:

        java.lang.NullPointerException
        at android.app.Activity.startActivityForResult(Activity.java:3456)
        at android.app.Activity.startActivityForResult(Activity.java:3417)
        at tkdstudios.talesandfeather.camera.dispatchTakePictureIntent(camera.java:20)
        at tkdstudios.talesandfeather.MainFragment$4.onClick(MainFragment.java:112)
        at android.view.View.performClick(View.java:4240)
        at android.view.View$PerformClick.run(View.java:17742)
        at android.os.Handler.handleCallback(Handler.java:730)
        at android.os.Handler.dispatchMessage(Handler.java:92)
        at android.os.Looper.loop(Looper.java:137)
        at android.app.ActivityThread.main(ActivityThread.java:5116)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:525)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:787)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
        at dalvik.system.NativeStart.main(Native Method)

其中camera.java:20

     startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE); 

和mainfragment:112

      t.dispatchTakePictureIntent();

这里显示权限           

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

<uses-feature
    android:name="android.hardware.camera"
    android:required="true" />

0 个答案:

没有答案