简体   繁体   English

活动尚未开始

[英]Activity is not starting

Please have a look at the following code 请看下面的代码

Form.java Form.java

My main activity is Form.java. 我的主要活动是Form.java。 Inside that, I have an AlertDialog . 在里面,我有一个AlertDialog When the user clicks on the "yes" button, the following class will be called 当用户单击“是”按钮时,将调用以下类

private class PositiveDialogBtnAction implements DialogInterface.OnClickListener
    {

        public PositiveDialogBtnAction()
        {
        }  
        @Override
        public void onClick(DialogInterface arg0, int arg1) {
            // TODO Auto-generated method stub
            //Toast.makeText(getApplicationContext(), databaseConnector.getStreetAddress(selectedBranch), Toast.LENGTH_LONG).show();

            Dialog dialog = new Dialog(context);
            dialog.setContentView(R.layout.activity_call_dialog);
            dialog.setTitle("Select a Phone Number");

            dialog.show();
        }

    }

Following are the XML file and the Java class, which is being set to a Dialog inside PositiveDialogBtnAction class mentioned above. 以下是XML文件和Java类,它们被设置为上述的PositiveDialogBtnAction类中的Dialog

activity_call_dialog activity_call_dialog

<TextView
    android:id="@+id/callNumber1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:text="" />

<TextView
    android:id="@+id/callNumber2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:text="" />

CallDialog.java 调用对话框

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.TextView;
import android.widget.Toast;

public class CallDialog extends Activity {

    private DatabaseConnector database = DatabaseHandler.getInstance();
    private TextView ph1,ph2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_call_dialog);

        Toast.makeText(this, "Activity Running", Toast.LENGTH_LONG).show();
        //String selectedBranch = Form.selectedBranch;

        //ph1 = (TextView)findViewById(R.id.callNumber1);
        //ph2 = (TextView)findViewById(R.id.callNumber2);

        //ph1.setText(database.getPhoneNumber1(selectedBranch));
        //ph2.setText(database.getPhoneNumber2(selectedBranch));

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_call_dialog, menu);
        return true;
    }


}

But, when the PositiveDialogBtnAction is fired, The Toast mentioned in CallDialog.java never get fired. 但是,当PositiveDialogBtnAction被触发时, CallDialog.java提到的Toast永远不会被触发。 It seems like it is not dealing with CallDIalog.java. 似乎它没有处理CallDIalog.java. Because of that, I am unable to set the values to the text fields in activity_call_dialog.xml as well. 因此,我也无法将值设置为activity_call_dialog.xml中的文本字段。 I have commented out those lines. 我已经注释掉了这些台词。

Why this Java file bundled with this activity is not getting called? 为什么与此活动捆绑在一起的Java文件没有被调用? Please help! 请帮忙!

For creating the alert dialog you should use the AlertDialog.Builder. 为了创建警报对话框,您应该使用AlertDialog.Builder。 It has a beautiful fluent interface that makes it really easy to handle dialogs. 它具有漂亮而流畅的界面,使处理对话框非常容易。 Example: 例:

new AlertDialog.Builder(this)
    .setMessage("Someone is calling you")
    .setPositiveButton("Positive", 
            new OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    startActivity(new Intent("com.example.CallDialogActivity"));
            }})
    .create()
    .show();

In order to start a new activity from inside the click listener, simply call startActivity() with the action registered in the AndroidManifest.xml for you CallDialog (I suggest you rename it to CallDialogActivity ). 为了从点击侦听器内部启动新活动,只需使用您在AndroidManifest.xml为您的CallDialog注册的动作调用startActivity() (建议您将其重命名为CallDialogActivity )。 But anyway, you should determine where you use dialogs or activities. 但是无论如何,您应该确定在哪里使用对话框或活动。 They have to be handled accordingly. 它们必须相应地处理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM