我对这个 android 的东西相当陌生,但我正在构建一个小应用程序,当你按下一个按钮时,你可以转到一个特定的 URL(转到一个优化版本的网站)。然而我遇到了一个问题。应用程序符合并加载到 AVD 上,但提供任何响应的唯一按钮是第一个按钮,它产生的响应是弹出错误消息说(不幸的是,MyApplication 已停止。)并关闭应用程序。其他按钮不响应点击。请参阅下面的代码。
package wag.cymal.libraryportal.welshlibraries;
import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.graphics.*;
import android.graphics.drawable.Drawable;
/**
* Main Activity will deal with all possible functionality
* @author Daniel Drave
*
*/
@SuppressWarnings("unused")
public class MainActivity extends Activity implements OnClickListener
{
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button libButton1 = (Button) findViewById(R.id.button1); //giving buttons onClickListener will track for user touches
libButton1.setOnClickListener(this);
Button libButton2 = (Button) findViewById(R.id.button2); // ""
libButton2.setOnClickListener(this);
Button libButton3 = (Button) findViewById(R.id.button3); // ""
libButton3.setOnClickListener(this);
Button libButton4 = (Button) findViewById(R.id.button4); // ""
libButton4.setOnClickListener(this);
Button libButton5 = (Button) findViewById(R.id.button5); // ""
libButton5.setOnClickListener(this);
}
/**
* onClick is a required method of OnClickListener and deals with the switch case statement governing what happens depending on
* what button you click.
*/
public void onClick(View v) {
switch (v.getId())
{
case R.id.button1: method1();
break;
case R.id.button2: method2();
break;
case R.id.button3: method3();
break;
case R.id.button4: method4();
break;
case R.id.button5: method5();
break;
default: break;
}
}
/**
*
*/
public void method1() {
Uri uri = Uri.parse("http://.....");
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
}
/**
*
*/
public void method2(){
Uri uri = Uri.parse("http://.....");
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
}
/**
*
*/
public void method3(){
Uri uri = Uri.parse("http://....");
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
}
/**
*
*/
public void method4(){
Uri uri = Uri.parse("http://.....");
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
}
/**
*
*/
public void method5(){
Uri uri = Uri.parse("http://.....");
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
这就是 MainActivity.java 类,下面是 activity_main XML 文件。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/natlib"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="@+id/textView1"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_marginLeft="-8dp"
android:padding="@dimen/padding_medium"
android:text="@string/welsh_libs"
android:textColor="#79438F"
android:textSize="27dip"
android:textStyle="bold"
tools:context=".MainActivity" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<Button
android:id="@id/button1"
android:layout_width="137dp"
android:layout_height="wrap_content"
android:layout_marginBottom="25dp"
android:background="#A4C81C"
android:text="@string/ask_lib" />
<Button
android:id="@id/button2"
android:layout_width="137dp"
android:layout_height="wrap_content"
android:layout_marginBottom="25dp"
android:background="#FF0066"
android:text="@string/find_book" />
<Button
android:id="@id/button3"
android:layout_width="137dp"
android:layout_height="wrap_content"
android:layout_marginBottom="25dp"
android:background="#3F83F1"
android:text="@string/find_lib" />
<Button
android:id="@id/button4"
android:layout_width="137dp"
android:layout_height="wrap_content"
android:layout_marginBottom="25dp"
android:background="#FE0002"
android:text="@string/register" />
<Button
android:id="@id/button5"
android:layout_width="137dp"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:background="#FBFC3F"
android:text="@string/login" />
</LinearLayout>
<ImageView
android:id="@id/image1"
android:layout_width="wrap_content"
android:layout_height="0dip"
android:layout_marginBottom="5dp"
android:layout_marginLeft="165dp"
android:layout_weight="0.34"
android:contentDescription="@string/desc"
android:src="@drawable/waglogo"
android:visibility="visible" />
这就是代码,我只是想知道它为什么会产生那个弹出框。我在代码本身中看不到任何逻辑错误。
PS 我已经修改了 URL,所以你看不到它们的实际去向。项目非常安静;)