Java 对话+回调?
我已经创建了一个CustomTypeDialog类,我想要的是使用活动布局中不存在的EditText。当我尝试单击其中一个按钮时,会出现空指针异常,我认为这是因为它们不在活动布局中。你能帮我解决这个问题吗?在另一个类的活动中调用该对话框Java 对话+回调?,java,android,layout,dialog,Java,Android,Layout,Dialog,我已经创建了一个CustomTypeDialog类,我想要的是使用活动布局中不存在的EditText。当我尝试单击其中一个按钮时,会出现空指针异常,我认为这是因为它们不在活动布局中。你能帮我解决这个问题吗?在另一个类的活动中调用该对话框 package dk.droidrun.droidrunapp; import android.app.Dialog; import android.content.Context; import android.content.DialogInterface
package dk.droidrun.droidrunapp;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
public class CustomTypeDialog extends Dialog {
ImageButton routeType;
EditText txtType;
Button imageRun, imageBike, imageWalk;
public CustomTypeDialog(final Context context) {
super(context);
this.setContentView(R.layout.customtype_dialog);
routeType = (ImageButton)findViewById(R.id.saveRoute_activityType);
txtType = (EditText)findViewById(R.id.saveRoute_typeTxt);
imageRun = (Button)findViewById(R.id.dialog_btn1);
imageBike = (Button)findViewById(R.id.dialog_btn2);
imageWalk = (Button)findViewById(R.id.dialog_btn3);
setTitle("Select activity type");
show();
imageRun.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
txtType.setText("Run");
routeType.setBackgroundResource(R.drawable.track_run);
}
});
imageBike.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
txtType.setText("Bike");
routeType.setBackgroundResource(R.drawable.track_bike);
}
});
imageWalk.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
txtType.setText("Walk");
routeType.setBackgroundResource(R.drawable.track_walk);
}
});
}
}
这是我的customtype_dialog.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".AutoMode"
android:background="@color/black" >
<RelativeLayout
android:id="@+id/dialog_relativeLayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="3dp"
android:layout_above="@+id/dialog_relativeLayout2"
android:layout_centerHorizontal="true" >
<Button
android:id="@+id/dialog_btn1"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_margin="3dp"
android:background="@drawable/track_run"
android:layout_alignRight="@+id/dialog_relativeLayout1"
android:layout_alignTop="@+id/dialog_relativeLayout1"
/>
<Button
android:id="@+id/dialog_btn2"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_margin="3dp"
android:background="@drawable/track_bike"
android:layout_alignTop="@+id/dialog_relativeLayout1"
android:layout_toRightOf="@+id/dialog_btn1"
/>
<Button
android:id="@+id/dialog_btn3"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_margin="3dp"
android:layout_toRightOf="@+id/dialog_btn2"
android:background="@drawable/track_walk"
/>
</RelativeLayout>
</RelativeLayout>
saveroutes.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@color/black"
tools:context=".SaveRouteActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="20dp"
android:text="@string/saveRoute"
android:textColor="@color/white"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:layout_marginTop="10dip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#7a0100"
android:text="Enter a name for the route" />
<EditText
android:id="@+id/saveRoute_nameRoute"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:ems="10"
android:hint="@string/saveRoute_name"
android:textColor="@color/white"
android:background="#4e4751"
android:inputType="textPersonName" >
</EditText>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#7a0100"
android:text="Describe your route" />
<EditText
android:id="@+id/saveRoute_desciptionTxt"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:ems="10"
android:hint="@string/saveRoute_description"
android:textColor="@color/white"
android:background="#4e4751"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#7a0100"
android:text="Activity type (e.g. running)"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center_horizontal" >
<EditText
android:id="@+id/saveRoute_typeTxt"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_marginTop="17dip"
android:ems="10"
android:layout_marginLeft="30dp"
android:hint="@string/saveRoute_type"
android:textColor="@color/white"
android:background="#4e4751" >
<requestFocus />
</EditText>
<ImageButton
android:id="@+id/saveRoute_activityType"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/track_walk" />
</LinearLayout>
<Button
android:id="@+id/saveRoute_saveBtn"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="30dp"
android:background="@color/white"
android:text="@string/saveRoute_savebutton" />
<Button
android:id="@+id/saveRoute_cancelBtn"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="30dp"
android:background="@color/white"
android:text="@string/saveRoute_cancel" />
</LinearLayout>
您不能在对话框视图中使用findViewById从活动的另一个布局访问视图 单击对话框上的按钮时,需要添加回调侦听器:
public interface OnDialogClickListener {
void onDialogImageRunClick();
}
public class CustomTypeDialog extends Dialog {
private final OnDialogClickListener listener;
public CustomTypeDialog(final Context context, OnDialogClickListener listener) {
this.listener = listener;
}
....
imageRun.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
listener.onDialogImageRunClick();
}
);
}
然后,当您在“活动”中创建有权访问视图的对话框时:
new CustomTypeDialog(context, new CustomTypeDialog.OnDialogClickListener() {
@Override
public void onDialogImageRunClick() {
txtType.setText("Run");
routeType.setBackgroundResource(R.drawable.track_run);
}
});
您不能在对话框视图中使用findViewById从活动的另一个布局访问视图 单击对话框上的按钮时,需要添加回调侦听器:
public interface OnDialogClickListener {
void onDialogImageRunClick();
}
public class CustomTypeDialog extends Dialog {
private final OnDialogClickListener listener;
public CustomTypeDialog(final Context context, OnDialogClickListener listener) {
this.listener = listener;
}
....
imageRun.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
listener.onDialogImageRunClick();
}
);
}
然后,当您在“活动”中创建有权访问视图的对话框时:
new CustomTypeDialog(context, new CustomTypeDialog.OnDialogClickListener() {
@Override
public void onDialogImageRunClick() {
txtType.setText("Run");
routeType.setBackgroundResource(R.drawable.track_run);
}
});
调用setContentView后,无法从“主/非活动”布局中找到视图。要么在构造函数中传递这两个组件,要么使用访问器方法从对话框中操作它们。请记住,大部分内容都必须用RunonuithRead包装。我对Android有点陌生,希望在这个问题上能得到更多帮助调用setContentView后,无法从“主/非活动”布局中找到视图。要么在构造函数中传递这两个组件,要么使用访问器方法从对话框中操作它们。请记住,大部分内容都必须用RunonuithRead包装。我对Android有点陌生,希望在这个问题上能得到更多帮助