在这里,我完成了5个按钮的代码,如Aboutus,开发,运营商,服务,联系人。
在这里,我点击了关于我们的按钮,显示错误,因为不幸的应用已关闭。
这是我的代码:
MainActivity.java
public class MainActivity extends Activity {
Button aboutus,development,service,carriers,contactus;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button aboutus=(Button) findViewById(R.id.aboutus);
aboutus.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(v.getContext(),aboutus.class);
startActivity(i);
}
});
}
@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;
}
}
activity_main.xml中
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:id="@+id/aboutus"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="About us"
/>
<Button
android:id="@+id/services"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Services"
/>
<Button
android:id="@+id/development"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Development"
/>
<Button
android:id="@+id/carriers"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Carriers"
/>
<Button
android:id="@+id/contactus"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Contact Us"
/>
</LinearLayout>
</RelativeLayout>
aboutus.java
public class aboutus extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.aboutus);
}
}
aboutus.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" >
<TextView android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="Aboutus Page"/>
</LinearLayout>
AndroidManifest.xml 这是正确的吗?
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.globalinfosoft"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.globalinfosoft.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<Activity android:name= "aboutus"/>
</application>
</manifest>
答案 0 :(得分:0)
而不是v.getContext()使用'getApplicationContext()'。
Intent i = new Intent(getApplicationContext(),aboutus.class);
startActivity(i);
答案 1 :(得分:0)
试试这个..
Button aboutus=(Button) findViewById(R.id.aboutus);
aboutus.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(getApplicationContext(),aboutus.class);
startActivity(i);
}
});
答案 2 :(得分:0)
Intent i = new Intent(v.getContext(),aboutus.class);
startActivity(i);
代替代码使用以下代码和指令:
Intent i = new Intent(MainActivity.this,aboutus.class);
startActivity(i);
你必须在AndroidManifest.xml上声明关于我们的活动,就像这样
<Activity android:name= ".aboutus"/>
答案 3 :(得分:0)
<强> MainActivity.java 强>
package com.example.a;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.widget.Button;
public class MainActivity extends Activity {
Button aboutus, development, service, carriers, contactus;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button aboutus = (Button) findViewById(R.id.aboutus);
aboutus.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
//Starting a new Intent
Intent nextScreen = new Intent(getApplicationContext(),aboutus.class);
startActivity(nextScreen);
}
});
}
}
这是Androidmanifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.a"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.a.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.example.a.aboutus"></activity>
</application>
</manifest>
答案 4 :(得分:0)
首先,您应该确保在AndroidManifest.xml文件中声明了aboutus acitivity。
<Activity android:name= "aboutus"/>
其次,你可以使用
Intent i = new Intent(getApplicationContext(),aboutus.class);
或
Intent i = new Intent(MainActivity.this,aboutus.class);
而不是
Intent i = new Intent(v.getContext(),aboutus.class);