0

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

我想要的主要是打开第二个活动,其中列出了 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;
}

}

沐浴活动.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);




     }



}
4

2 回答 2

0

secondactivity根本没有被跳过。它被调用来启动你的onCreate()每个Activity Lifecycle。在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
    }
});

它应该给你你想要的。

于 2013-09-25T17:32:13.743 回答
0

当然它会直接带你去bathactivity。你问的。在你里面调用这个函数onCreate,它创建一个并启动. 不要调用里面的函数。只需在 XML 文件中直接使用它,您可以在其中定义按钮的函数名称。secondactivitysetButtonOnClickListener();IntentbathactivityonCreateandroid:onClick

于 2013-09-25T17:32:23.400 回答