我有一个我无法解决的相当奇怪的问题。
我自学了Android Eclipse,我创建了一个非常简单的应用程序,有三个按钮,1)Facebook 2)LinkedIn和3)Favorite Band。但是,当我在手机上打开应用程序时,我只能按顺序按下按钮1) - > 2) - > 3)。例如,我不能打开2)除非我已经打开1)并且我无法打开3)除非我已经打开2)等。我怎样才能改变它我可以先打开3)例如?
我已经在下面发布了我的java脚本,任何想法都会很棒!
package tabletop.app4asli;
import android.app.Activity;
import android.content.Intent;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
public class MainActivity extends Activity {
MediaPlayer thesong;
Button main;
Button main2;
Button main3;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
main = (Button) findViewById(R.id.btn_go);
main.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String url = "https://www.facebook.com/asli.akidil?fref=ts";
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
startActivity(i);
main2 = (Button) findViewById(R.id.button2);
main2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String url = "https://www.linkedin.com/pub/asli-akidil/8a/719/55a.";
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
startActivity(i);
main3 = (Button) findViewById(R.id.button3);
main3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String url = "http://www.biffyclyro.com";
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
startActivity(i);
}
答案 0 :(得分:0)
这是因为按钮2的布局代码包含在按钮1的onClick事件中,按钮3的布局包含在按钮2的onClicked事件中。
onCreated()函数的代码应如下所示:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
main = (Button) findViewById(R.id.btn_go);
main.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String url = "https://www.facebook.com/asli.akidil?fref=ts";
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
startActivity(i);
}
});
main2 = (Button) findViewById(R.id.button2);
main2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String url = "https://www.linkedin.com/pub/asliakidil/8a/719/55a.";
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
startActivity(i);
}
});
main3 = (Button) findViewById(R.id.button3);
main3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String url = "http://www.biffyclyro.com";
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
startActivity(i);
}
});
}
答案 1 :(得分:0)
您在按钮2的ClickListener中为按钮3设置ClickListener,在按钮1的ClickListener中为按钮2设置ClickListener。因此,按钮2和3不会初始化,直到分别单击按钮1和2。您可以将OnClickListener
设置为Buttons
循环中的所有for
,如下所示:
ArrayList<Button> buttonList = new ArrayList<Button>();
buttonList.add((Button) findViewById(R.id.button1));
buttonList.add((Button) findViewById(R.id.button2));
buttonList.add((Button) findViewById(R.id.button3));
ArrayList<String> urlList = new ArrayList<String>();
urlList.add("https://www.facebook.com/asli.akidil?fref=ts");
urlList.add("https://www.linkedin.com/pub/asli-akidil/8a/719/55a.");
urlList.add("http://www.biffyclyro.com");
for (int i = 0; i < buttonList.size(); i++) {
final int index = i;
final Button button = buttonList.get(index);
final String url = urlList.get(index);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
startActivity(i);
}
});
}