我的代码如下
带有4个Edittext的警告对话框的布局
alert_edit_information1.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Name"
android:id="@+id/editTextInformationName" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Address"
android:id="@+id/editTextInformationAddress" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Phone"
android:id="@+id/editTextInformationPhones"
android:layout_gravity="center_horizontal" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Company Name"
android:id="@+id/editTextInformationCompanyName" />
单击相对布局以弹出警告对话框
relativeLayout1.setOnClickListener(new AdapterView.OnClickListener() {
@Override
public void onClick(View v) {
AlertDialog.Builder alert = new AlertDialog.Builder(getContext());
alert.setTitle("Alert Dialog With EditText"); //Set Alert dialog title
alert.setView(R.layout.alert_edit_information1);//Set 4 edittext to alert dialog
editTextName = (EditText) v.findViewById(R.id.editTextInformationName); //error posible here
editTextName.setText("Name here",TextView.BufferType.EDITABLE); //error here
alert.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) { }
}); //End of alert.setPositiveButton
alert.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
// Canceled.
dialog.cancel();
}
});//End of alert.setNegativeButton
AlertDialog alertDialog = alert.create();
alertDialog.show();
}
});
错误日志
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.EditText.setText(java.lang.CharSequence, android.widget.TextView$BufferType)' on a null object reference
我是否正确初始化EditText? 这条线路好吗?
editTextName =(EditText)v.findViewById(R.id.editTextInformationName);
答案 0 :(得分:1)
<强>唐&#39;吨强>
editTextName = (EditText) v.findViewById(R.id.editTextInformationName);
editTextName.setText("Name here",TextView.BufferType.EDITABLE);
<强>不要强>
您应该传递View的对象。
editTextName = (EditText) alert.findViewById(R.id.editTextInformationName);
editTextName.setText("Name here");
答案 1 :(得分:0)
AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);
// ...Irrelevant code for customizing the buttons and title
LayoutInflater inflater = this.getLayoutInflater();
View dialogView = inflater.inflate(R.layout. alert_edit_information1, null);
dialogBuilder.setView(dialogView);
EditText editText = (EditText) dialogView.findViewById(R.id.editTextInformationName);
editText.setText("test label");
AlertDialog alertDialog = dialogBuilder.create();
alertDialog.show();
答案 2 :(得分:0)
不要直接设置AlertDialog的视图,如
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2\video\tracking.hpp>
#include <cmath>
#include <string>
#include <iostream>
#include <sstream>
#include <filesystem>
#include <cstring>
int main(int argc, char *argv[])
{
//Tracking of points of interest:
cv::Mat img;
cv::UMat prevgray;
std::vector<cv::Point2f> featuresPrevious;
std::vector<cv::Point2f> featuresCurrent;
//int count = 0;
for (auto& p : std::experimental::filesystem::directory_iterator("C:\\Billeder"))
{
img = cv::imread(p.path().string(), CV_LOAD_IMAGE_GRAYSCALE);
//printf("Image number: %d\n", count);
//count++;
if (prevgray.empty() == false)
{
std::vector<cv::Point2f> featuresNextPos;
std::vector<uchar> featuresFound;
cv::Mat err;
featuresPrevious = std::move(featuresCurrent);
// create points of interest in current image:
goodFeaturesToTrack(img, featuresCurrent, 50, 0.0005, 16); //calculate the features for use in next iteration
// Track points of interest in current image:
calcOpticalFlowPyrLK(prevgray, img, featuresPrevious, featuresNextPos, featuresFound, err);
err.release();
//printf("featuresNextPos length: %d \n", (int)featuresNextPos.size());
//printf("featuresfound length: %d \n", (int)featuresFound.size());
//Draw lines connecting previous position and current position
for (size_t i = 0; i<featuresNextPos.size(); i++) {
if (featuresFound[i]) {
float dist = sqrt(pow(std::abs(featuresPrevious[i].x - featuresNextPos[i].x), 2) + pow(std::abs(featuresPrevious[i].y - featuresNextPos[i].y), 2));
if (dist > 15) {
// printf("The distance is too great to make sense \n");
}
else {
//Draw line from previous features to next features:
line(img, featuresPrevious[i], featuresNextPos[i], cv::Scalar(255, 0, 0));
if (std::abs(featuresPrevious[i].y - featuresNextPos[i].y)>9)
{
printf("Satellite might be found? Distance is: %f \n", dist);
//printf("Path of image containing satellite: %s \n", p.path().string());
}
}
//Draw the features:
circle(img, featuresNextPos[i], 3, cv::Scalar(255, 0, 0),2);
}
}
}
else {
printf("Go here First\n");
goodFeaturesToTrack(img, featuresCurrent, 50, 0.005, 16); //calculate the features for use in next iteration
//Move current image into "previous" image:
img.copyTo(prevgray);
}
// Print image to show
cv::namedWindow("Tracking Current", cv::WINDOW_AUTOSIZE);
imshow("Tracking Current", img);
img.copyTo(prevgray);
// wait for image to be printed
cv::waitKey(10);
//free memory again
img.release();
}
cv::waitKey(0);
return 0;
}
使用像
这样的本地视图变量alert.setView(R.layout.alert_edit_information1);
然后使用
View view = inflater.inflate(R.layout. alert_edit_information1, null);
现在你可以用v - &gt;初始化一个edittext图。
alert.setView(view);