Android - 我第二次调用Activity时按钮不起作用

时间:2015-06-15 18:40:09

标签: android android-activity

我有一个有2个活动的应用程序,主要活动(HomeActivity)有一个包含不同对象信息的List,我可以点击这个对象,然后会出现一个警告对话框,可以启动我的第二个活动(ServerConfig)将显示所选对象的信息以及其他一些功能,现在这是问题,当我只是编译项目并调用此活动时,它首先像魅力一样工作,但是如果我回去然后选择相同或者列表中的另一个对象并调用此Activity ServerConfig,它将加载并显示新信息,但我放在底部的按钮不再工作,它们什么都不做,这个按钮是单独的,但是另一个LinearLayout上的按钮工作得很好。这个应用程序的服务告诉我,当我点击按钮但是活动没有任何变化时,作业就被发送了。

这是文件:

ServerConfig活动的XML。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1" >

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >

            <TextView
                android:id="@+id/serConfig_Title"
                style="@style/textHeading"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerHorizontal="true"
                android:text="@string/serConfig_title" />

            <TableLayout
                android:id="@+id/serConfig_serDetailsTable"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@id/serConfig_Title"
                android:layout_centerHorizontal="true"
                android:padding="10dp" >

                <TableRow>

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"
                        android:text="@string/serConfig_server_name" />

                    <TextView
                        android:id="@+id/serConfig_serverName"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"
                        android:text="@string/server_name_dummy" />
                </TableRow>

                <TableRow>

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"
                        android:text="@string/serConfig_server_address" />

                    <TextView
                        android:id="@+id/serConfig_serverAddress"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"
                        android:text="@string/server_address_dummy" />
                </TableRow>

                <TableRow>

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"
                        android:text="@string/port_title" />

                    <TextView
                        android:id="@+id/serConfig_port"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"
                        android:text="@string/port_dummy" />
                </TableRow>

                <TableRow>

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"
                        android:text="@string/protocol_title" />

                    <TextView
                        android:id="@+id/serConfig_protocol"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"
                        android:text="@string/port_dummy" />
                </TableRow>

                <TableRow>

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"
                        android:text="@string/notification_title" />

                    <TextView
                        android:id="@+id/serNotif_email"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"
                        android:text="@string/port_dummy" />

                    <TextView
                        android:id="@+id/serNotif_sms"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"
                        android:text="@string/port_dummy" />
                </TableRow>

                <Button
                    android:id="@+id/serConfig_historyButton"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_horizontal"
                    android:layout_marginTop="5dp"
                    android:text="@string/serConfig_hist_Butt" />
            </TableLayout>

            <LinearLayout
                android:id="@+id/serConfig_welcomeLayout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@id/serConfig_serDetailsTable"
                android:layout_marginTop="5dp"
                android:orientation="vertical" >

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="@string/serConfig_welcomeMsgOld"
                    android:textStyle="bold" />

                <TextView
                    android:id="@+id/serConfig_welcomeMsgOld"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:padding="10dp"
                    android:textIsSelectable="true" />

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="@string/serConfig_welcomeMsg"
                    android:textStyle="bold" />

                <TextView
                    android:id="@+id/serConfig_welcomeMsg"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:padding="10dp"
                    android:textIsSelectable="true" />
            </LinearLayout>

            <ProgressBar
                android:id="@+id/serConfig_progBar"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@id/serConfig_welcomeLayout"
                android:layout_centerHorizontal="true"
                android:visibility="gone" />
        </RelativeLayout>
    </ScrollView>

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0" >

        <Button
            android:id="@+id/serConfig_connButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:text="@string/serConfig_conn_Butt" />

        <LinearLayout
            android:id="@+id/serConfig_accpLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:visibility="gone" >

            <Button
                android:id="@+id/serConfig_accpButton"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:clickable="true"
                android:layout_weight="1"
                android:text="@string/serConfig_accept_Butt" />

            <Button
                android:id="@+id/serConfig_rejButton"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:clickable="true"
                android:layout_weight="1"
                android:text="@string/serConfig_reject_Butt" />
        </LinearLayout>
    </FrameLayout>

</LinearLayout>

部分ServerConfig活动

public class ServerConfigActivityP extends Activity {

    /**
     * View/Helper for this Activity.
     */
    private ServerConfigView m_ServerView;
    /**
     * Selected Server that will be display in this Activity.
     */
    private ServerDetails m_SelectedServer;
    /**
     * Selected servers position in the DatabaseManager.
     */
    private int m_ServerPosition;

    private boolean m_serviceIsBound = false;
    private ServerService m_service = null;

    private static final String TAG_DEBUG ="SERVER_CONFIG_ACTIVITY";

    private Button AcceptButton,RejectButton,ConnectButton,HistoryButton;
    /**
     * Activity life cycle onCreate.
     * 
     *  Gets the ServerDetails that we are going to use for this Activity.
     *  Display the information of the selected ServerDetails.
     * 
     * @see Activity
     */
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_server_config_activity_p);


        this.AcceptButton = (Button) findViewById(R.id.serConfig_accpButton);
        this.RejectButton =(Button) findViewById(R.id.serConfig_rejButton);
        this.ConnectButton= (Button) findViewById(R.id.serConfig_connButton);
        this.HistoryButton =(Button) findViewById(R.id.serConfig_historyButton);

        AcceptButton.setOnClickListener(myListener);
        RejectButton.setOnClickListener(myListener);
        ConnectButton.setOnClickListener(myListener);
        HistoryButton.setOnClickListener(myListener);



        // Create the ServerConfigView for this Activity
        m_ServerView = new ServerConfigView(this);

        // Get the selected ServerDetails from the Intent.
        Bundle recBundle = new Bundle();
        recBundle = getIntent().getBundleExtra(ServerDetails.TAG_SELECTED_SERVER);

        // Get the position in the List of the ServerDetaisl that we are going to use for this activity.
        m_ServerPosition = recBundle.getInt(ServerDBM.TAG_SERVER_POSITION);

        // Get the ServerDetails Object that we are going to use for this activity We Make a deep Copy.
        m_SelectedServer = new ServerDetails(ServerDBM.getServerAtPosition(m_ServerPosition));

        // Show the information of the Selected ServerDetails on this Activity.
        m_ServerView.ShowOnScreen(m_SelectedServer);

        m_SelectedServer.setSerName("JobConfigActivity");
        //Bind This Activity with the Service
        doBindService();

    }

    private OnClickListener myListener = new View.OnClickListener(){

        @Override
        public void onClick(View v) {

            switch (v.getId()) {

            case R.id.serConfig_connButton:
                executeJobOnService();
                m_ServerView.WaitForResponce();
                Log.i(TAG_DEBUG,"Connect Button Pressed");
                break;
            // Show History Button: Displays the History of the selected ServerDetails Initialize a new Activity.
            case R.id.serConfig_historyButton:
                m_ServerView.showHistory(m_SelectedServer.getIpAdd());
                break;

            case R.id.serConfig_accpButton:
                storeNewMessage();
                storeMonResult();
                HomeView.refreshListView();
                finish();
                break;

            case R.id.serConfig_rejButton:
                storeMonResult();
                m_ServerView.ShowAcceptAndRejetButtons(false);
                m_ServerView.ShowConnectButton(true);
                m_ServerView.resetEverything();
                break;

            }

        }

    };

    @Override
    protected void onDestroy() {
        super.onDestroy();
        doUnbindService();
        HomeView.refreshListView();
        Log.d(TAG_DEBUG, "Server Config Activity Destroyed");

    }
//More Code !!
}

从HomeActivity调用ServerConfig活动

final Intent configIntent = new         
Intent(m_presenter,ServerConfigActivityP.class);
Bundle selServer = new Bundle();
selServer.putInt(ServerDBM.TAG_SERVER_POSITION,position);
configIntent.putExtra(ServerDetails.TAG_SELECTED_SERVER,selServer);
// Start the Server ConfigActivity
m_presenter.startActivity(configIntent);

1 个答案:

答案 0 :(得分:0)

根据您显示的代码,我无法看到您通过按钮定义的myListener连接位置。

要正确操作,您应该在OnCreate()OnResume()方法中为按钮定义OnClick-listening(连接它们)。