I am trying to use an alertdialog to allow the user to press the ok button and open a new activity. Below is my code:
我正在尝试使用alertdialog来允许用户按下确定按钮并打开一个新活动。以下是我的代码:
My first activity
我的第一个活动
AlertDialog.Builder dialog= new AlertDialog.Builder(context);
dialog.setTitle("Welcome");
dialog.setMessage("Please click ok");
dialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Intent intent = new Intent("com.thewildOnz.myResult");
startActivity(intent);
}
});
dialog.show();
the new activity
新的活动
public class myResult extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.result);
Manifest:
</activity>
<activity
android:name=".myResult"
android:label="@string/app_name" >
<intent-filter>
<action android:name="com.thewildOnz.myResult" />
<category android:name="android.intent.category.DEFUALT" />
</intent-filter>
</activity>
However I get the error:
但是我收到错误:
java.lang.NullPointerException
What am I missing?
我错过了什么?
2 个解决方案
#1
2
You need to call .create()
on your AlertDialog
.
您需要在AlertDialog上调用.create()。
AlertDialog.Builder builder= new AlertDialog.Builder(context);
builder.setTitle("Welcome");
builder.setMessage("Please click ok");
builder.setPositiveButton("OK", new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which)
{
Intent intent = new Intent("com.thewildOnz.myResult");
startActivity(intent);
}
});
AlertDialog dialog = builder.create();
dialog.show();
You also may be using new Intent("")
incorrectly. Unless you have "com.thewildOnz.myResult"
defined as an action, this will fail.
您也可能错误地使用了新的Intent(“”)。除非你将“com.thewildOnz.myResult”定义为一个动作,否则这将失败。
You should be using this:
你应该使用这个:
Intent intent = new Intent(this, com.thewildOnz.myResult.class);
#2
0
Maybe you got a typo it's DEFAULT
:
也许你得到一个错字它是DEFAULT:
<category android:name="android.intent.category.DEFUALT" />
#1
2
You need to call .create()
on your AlertDialog
.
您需要在AlertDialog上调用.create()。
AlertDialog.Builder builder= new AlertDialog.Builder(context);
builder.setTitle("Welcome");
builder.setMessage("Please click ok");
builder.setPositiveButton("OK", new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which)
{
Intent intent = new Intent("com.thewildOnz.myResult");
startActivity(intent);
}
});
AlertDialog dialog = builder.create();
dialog.show();
You also may be using new Intent("")
incorrectly. Unless you have "com.thewildOnz.myResult"
defined as an action, this will fail.
您也可能错误地使用了新的Intent(“”)。除非你将“com.thewildOnz.myResult”定义为一个动作,否则这将失败。
You should be using this:
你应该使用这个:
Intent intent = new Intent(this, com.thewildOnz.myResult.class);
#2
0
Maybe you got a typo it's DEFAULT
:
也许你得到一个错字它是DEFAULT:
<category android:name="android.intent.category.DEFUALT" />