为什么我的应用错过了一项活动?

时间:2013-09-25 16:43:35

标签: android view android-activity

您好我最近问了一个关于我的应用程序的问题,这里的好人发现它会强制关闭,因为我的图像没有正确处理位图工厂,但作为业余爱好者我决定拿出图像以确保我的代码很好....而且它不是

我想要的是打开第二个活动的主要内容,它列出了4个按钮,其中最重要的一个按钮带你太过于狂热

我所拥有的是主要的跳过第二次活动并且带你太过于暴力,如果你按下它会带你太活跃但是这里的按钮什么都不做

但我的代码中没有错误,应用程序永远不会强制关闭,所以我只是发布代码或logcat吗?

任何帮助表示赞赏

main.java

package com.example.myapplication;

import android.content.Intent;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
 import android.widget.EditText;


public class main extends Activity {
Button ok;
EditText name;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    name=(EditText)findViewById(R.id.editText);
    ok=(Button)findViewById(R.id.button);

    ok.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View arg0) {
            String nameStr = name.getText().toString();

            Intent intent = new Intent(getApplicationContext(), secondactivity.class);
            intent.putExtra("NAMEDATA",nameStr);
            startActivity(intent);

        }
    });


}



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

}

secondactivity.java

package com.example.myapplication;

import android.content.Intent;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.Button;
import android.widget.TextView;

public class secondactivity extends Activity {
TextView t;
Button button;
Button button2;
Button button3;
Button button4;






@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_second);
    t = (TextView)findViewById(R.id.textView3);
    String n = this.getIntent().getStringExtra("NAMEDATA");
    t.setText(n);

    button = (Button)findViewById(R.id.button);
    button2 = (Button)findViewById(R.id.button2);
    button3 = (Button)findViewById(R.id.button3);
    button4 = (Button)findViewById(R.id.button4);

    setButtonOnClickListener();

}


private void setButtonOnClickListener(){
    Intent intent = new Intent(secondactivity.this, bathactivity.class);
    startActivity(intent);
}






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

}

bathactivity.java

package com.example.myapplication;


import android.os.Bundle;
import android.app.Activity;
import android.widget.Button;

public class bathactivity extends Activity {
Button button;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_bath);
    button = (Button)findViewById(R.id.nextbutton);




     }



}

2 个答案:

答案 0 :(得分:0)

您的secondactivity根本没有被跳过。它被调用,从Activity Lifecycle开始onCreate()。在onCreate()内,您拨打setButtonOnClickListener(),其中Intent会将您转到bathactivity。因此,虽然secondactivity被调用,但您没有看到它,因为您立即转到下一个Activity

secondactivity

中更改此代码
button = (Button)findViewById(R.id.button);
button2 = (Button)findViewById(R.id.button2);
button3 = (Button)findViewById(R.id.button3);
button4 = (Button)findViewById(R.id.button4);

setButtonOnClickListener();

}
private void setButtonOnClickListener(){
    Intent intent = new Intent(secondactivity.this, bathactivity.class);
    startActivity(intent);
}

类似

button = (Button)findViewById(R.id.button);
button2 = (Button)findViewById(R.id.button2);
button3 = (Button)findViewById(R.id.button3);
button4 = (Button)findViewById(R.id.button4);

button.setOnClickListener(new OnClickListener() /* this is one of several ways to set the 
                                                 * OnClickListener which you are not doing
                                                */
{
    @Override
    public void onClick(View v)
    {
        Intent intent = new Intent(secondactivity.this, bathactivity.class);
        startActivity(intent);   // this will start your Activity when clicking the Button
    }
});

它应该给你你想要的东西。

答案 1 :(得分:0)

当然,它会直接带你到bathactivity。你问了。在onCreate的{​​{1}}内,您正在调用此secondactivity函数,该函数会创建setButtonOnClickListener();并启动Intent。不要在bathactivity内调用该函数。只需在XML文件中直接使用它,在onCreate中,您只需定义按钮的函数名称即可。