我想知道如何在单击按钮时打开新活动。我知道网上有很多答案,但没有一个可以工作,当我添加代码来打开新活动时,我可能在Eclipse中没有错误,但是当我在模拟器或手机上打开应用程序时,它崩溃了..
这是MainActivity.java:
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.os.Build;
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
}
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container,
false);
return rootView;
}
}
}
这是我的actiity_main.xml:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.BlueTechnologies.UltimateGreekApp.MainActivity"
tools:ignore="MergeRootFrame" />
这是我的fragment_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="com.BlueTechnologies.UltimateGreekApp.MainActivity$PlaceholderFragment" >
<Button
android:id="@+id/btnRadio"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/btnTV"
android:layout_centerHorizontal="true"
android:layout_marginTop="14dp"
android:text="Radio" />
<Button
android:id="@+id/btnNews"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/btnRadio"
android:layout_centerHorizontal="true"
android:layout_marginTop="15dp"
android:text="News" />
<Button
android:id="@+id/btnTV"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="81dp"
android:onClick="openNewActivity"
android:text="Live TV" />
</RelativeLayout>
这是我的TVActivity.java:
package com.BlueTechnologies.UltimateGreekApp;
import android.os.Bundle;
import org.apache.cordova.*;
public class TVActivity extends DroidGap {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.loadUrl("file:///android_asset/www/tv.html");
}
}
这是我的activity_tv.xml:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.BlueTechnologies.UltimateGreekApp.TVActivity"
tools:ignore="MergeRootFrame" />
这是我的fragment_tv.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="com.BlueTechnologies.UltimateGreekApp.TVActivity$PlaceholderFragment" >
</RelativeLayout>
这是我的AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.BlueTechnologies.UltimateGreekApp"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="14" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.BlueTechnologies.UltimateGreekApp.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=".TVActivity"></activity>
</application>
</manifest>
答案 0 :(得分:0)
我认为您应该在每个按钮的主要活动中添加以下代码, 也“将第二项活动改为你需要的活动”
Button firstButton = (Button)findViewById(R.id.btnRadio);
firstButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this, **SecondActivity**.class);
startActivity(intent);
} }