我需要从课堂内拨打require
,但我没有startActivityForResult
。
文件:SignUpFragment.java
java.lang.NullPointerException
P.S:public class SignUpFragment extends Fragment {
ImageView imageView;
private OnFragmentInteractionListener mListener;
public SignUpFragment() {
// Required empty public constructor
Test test = new Test();
test.getImageData();
}
}
不是Test.java
,其类和我扩展Activity
以访问Activity
文件Test.java
startActivityForResult
更新:这是错误日志:
public class Test extends Activity {
public void getImageData() {
Intent pickIntent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
pickIntent.setType("image/*");
startActivityForResult(pickIntent, 1);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Log.d(“Reps”, ”Called”);
}
}
答案 0 :(得分:0)
public class Test extends Activity {
public void getImageData() {
Intent pickIntent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
pickIntent.setType("image/*");
if (pickIntent == 1) {
startActivity(pickIntent);
}
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Log.d(“Reps”,”Called”);
}
}
答案 1 :(得分:0)
我倾向于将getImageData()和onActivityResult放到包含Fragment的Activity类中(例如,通过侦听器在Activity和Fragment之间进行通信)。
但下面是另一种选择
public class SignUpFragment extends Fragment {
private final int PICK_ID = 1;
public SignUpFragment() {
// Required empty public constructor
}
public static SignUpFragment newInstance() {
SignUpFragment fragment = new SignUpFragment();
Bundle args = new Bundle();
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getImageData();
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PICK_ID && resultCode == Activity.RESULT_OK) {
Log.d("TAG", "onActivityResult");
if (data != null) {
try {
InputStream inputStream = getActivity().getContentResolver().openInputStream(data.getData());
//...
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
}
public void getImageData(){
Intent pickIntent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
pickIntent.setType("image/*");
startActivityForResult(pickIntent, PICK_ID);
}
}
在您的活动中,只需输入类似的内容
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getSupportFragmentManager()
.beginTransaction()
.add(android.R.id.content, SignUpFragment.newInstance())
.commit();
}
}
答案 2 :(得分:0)
您将Activity视为普通Java类,而不是像其他Activity一样。因此,当您调用startActivityForResult()时,您会得到一个nullpointerexception,因为尚未调用Activity的onCreate(),这意味着它的当前实例对于调用startActivityForResult()无效。