我有自定义警告对话框,里面有两个按钮。但是每个按钮都有错误的空对象引用,我想让我的relativelayout可以点击,这里是我的java代码:
RelativeLayout relaCamera;
RelativeLayout relaGallery;
relaCamera = findViewById(R.id.relaCameraIntent);
relaGallery = findViewById(R.id.relaGalleryIntent);
......
......
private void pickFromGallery() {
LayoutInflater layoutInflater = LayoutInflater.from(this);
View view = layoutInflater.inflate(R.layout.upload_video_options, null);
final AlertDialog alertD = new AlertDialog.Builder(this).create();
relaCamera.setOnClickListener(new View.OnClickListener() { // error here
public void onClick(View v) {
Intent intent = new Intent();
intent.setTypeAndNormalize("video/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
startActivityForResult(Intent.createChooser(intent, "Select Video"), REQUEST_GET_VIDEO);
}
});
relaGallery.setOnClickListener(new View.OnClickListener() {// error here
public void onClick(View v) {
Intent takeVideoIntent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
if (takeVideoIntent.resolveActivity(getPackageManager()) != null) {
startActivityForResult(takeVideoIntent, REQUEST_VIDEO_CAPTURE);
}
}
});
alertD.setView(view);
alertD.show();
}
这是我的upload_video_options.xml,用于自定义警告对话框:
<RelativeLayout
android:id="@+id/relaCameraIntent"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:layout_marginLeft="10dp"
android:layout_marginTop="20dp"
android:clickable="true"
android:gravity="center_horizontal"
android:orientation="horizontal">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:id="@+id/relaGalleryIntent"
android:clickable="true"
android:layout_marginBottom="10dp"
android:layout_marginTop="10dp"
android:orientation="horizontal">
答案 0 :(得分:2)
尝试以下代码,您从view
获得upload_video_options.xml
,因此您需要这样做。
LayoutInflater layoutInflater = LayoutInflater.from(this);
View view = layoutInflater.inflate(R.layout.upload_video_options,null);
relaCamera = view.findViewById(R.id.relaCameraIntent);
relaGallery = view.findViewById(R.id.relaGalleryIntent);
完整代码:
private void pickFromGallery() {
LayoutInflater layoutInflater = LayoutInflater.from(this);
View view = layoutInflater.inflate(R.layout.upload_video_options, null);
relaCamera = view.findViewById(R.id.relaCameraIntent);
relaGallery = view.findViewById(R.id.relaGalleryIntent);
final AlertDialog alertD = new AlertDialog.Builder(this).create();
relaCamera.setOnClickListener(new View.OnClickListener() { // error here
public void onClick(View v) {
Intent intent = new Intent();
intent.setTypeAndNormalize("video/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
startActivityForResult(Intent.createChooser(intent, "Select Video"), REQUEST_GET_VIDEO);
}
});
relaGallery.setOnClickListener(new View.OnClickListener() {// error here
public void onClick(View v) {
Intent takeVideoIntent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
if (takeVideoIntent.resolveActivity(getPackageManager()) != null) {
startActivityForResult(takeVideoIntent, REQUEST_VIDEO_CAPTURE);
}
}
});
alertD.setView(view);
alertD.show();
}
答案 1 :(得分:1)
试试这个。
RelativeLayout relaCamera;
RelativeLayout relaGallery;
private void pickFromGallery() {
LayoutInflater layoutInflater = LayoutInflater.from(this);
View view = layoutInflater.inflate(R.layout.upload_video_options, null);
relaCamera = view.findViewById(R.id.relaCameraIntent);
relaGallery = view.findViewById(R.id.relaGalleryIntent);
final AlertDialog alertD = new AlertDialog.Builder(this).create();
relaCamera.setOnClickListener(new View.OnClickListener() { // error here
public void onClick(View v) {
Intent intent = new Intent();
intent.setTypeAndNormalize("video/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
startActivityForResult(Intent.createChooser(intent, "Select Video"), REQUEST_GET_VIDEO);
}
});
relaGallery.setOnClickListener(new View.OnClickListener() {// error here
public void onClick(View v) {
Intent takeVideoIntent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
if (takeVideoIntent.resolveActivity(getPackageManager()) != null) {
startActivityForResult(takeVideoIntent, REQUEST_VIDEO_CAPTURE);
}
});
alertD.setView(view);
alertD.show();
}
问题是你在主要版面而不是alerviews的布局中搜索视图。
希望这会对你有所帮助。
答案 2 :(得分:0)
relaCamera = view.findViewById(R.id.relaCameraIntent);
relaGallery = view.findViewById(R.id.relaGalleryIntent);
尝试以上代码。
答案 3 :(得分:0)
试试这个:
relaCamera = view.findViewById(R.id.relaCameraIntent);
relaGallery = view.findViewById(R.id.relaGalleryIntent);