黑马程序员技术交流社区
标题:
activitygroup切换activity
[打印本页]
作者:
周开伟
时间:
2012-8-23 17:24
标题:
activitygroup切换activity
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.meishuihuiyin.bbs"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="5"
android:targetSdkVersion="15" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/test" >
<activity
android:name=".MainActivity"
android:label="@string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".ZiXunChu">
</activity>
</application>
</manifest>
复制代码
先贴代码MainActivity代码
package com.meishuihuiyin.bbs;
import android.os.Bundle;
import android.app.ActivityGroup;
import android.content.Intent;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ScrollView;
public class MainActivity extends ActivityGroup implements OnClickListener {
Button one = null;
Button two = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
one = (Button)findViewById(R.id.first);
two = (Button)findViewById(R.id.second);
one.setOnClickListener(this);
one.setTag(1);
two.setOnClickListener(this);
two.setTag(2);
}
public void onClick(View v){
int tag = (Integer)v.getTag();
Intent intent = null;
Window subActivity = null;
ScrollView container = null;
switch(tag){
case 1:
//container.removeAllViews();
intent=new Intent(MainActivity.this,ZiXunChu.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
((ActivityGroup)MainActivity.this.getParent()).getLocalActivityManager().removeAllActivities();
subActivity=((ActivityGroup)MainActivity.this.getParent()).getLocalActivityManager().startActivity("locallist",intent);
container.addView(subActivity.getDecorView());
break;
case 2:
//container.removeAllViews();
intent=new Intent(MainActivity.this,ZiXunChu.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
((ActivityGroup)MainActivity.this.getParent()).getLocalActivityManager().removeAllActivities();
subActivity=((ActivityGroup)MainActivity.this.getParent()).getLocalActivityManager().startActivity("locallist",intent);
container.addView(subActivity.getDecorView());
break;
}
}
}
复制代码
ZiXunChu代码
package com.meishuihuiyin.bbs;
import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageButton;
public class ZiXunChu extends Activity {
ImageButton back = null;
ImageButton refresh = null;
WebView show_web = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.show); // 注意顺序
back = (ImageButton)findViewById(R.id.back);
refresh = (ImageButton)findViewById(R.id.refresh);
show_web = (WebView)findViewById(R.id.show);
WebSettings settings = show_web.getSettings();
settings.setSupportZoom(false); //支持缩放
//settings.setBuiltInZoomControls(true); //启用内置缩放装置
//settings.setJavaScriptEnabled(true); //启用JS脚本
show_web.loadUrl("http://www.jyubbs.com/bbs/forum-43-1.html");
show_web.setWebViewClient(new WebViewClient() {
//当点击链接时,希望覆盖而不是打开新窗口
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url); //加载新的url
return true; //返回true,代表事件已处理,事件流到此终止
}
});
back.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
show_web.goBack();
}
});
refresh.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
show_web.reload();
}
});
show_web.setOnKeyListener(new View.OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
if (event.getAction() == KeyEvent.ACTION_DOWN) {
if (keyCode == KeyEvent.KEYCODE_BACK && show_web.canGoBack()) {
show_web.goBack(); //后退
return true; //已处理
}
}
return false;
}
});
}
}
复制代码
layout_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" >
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:gravity="center_horizontal">
<Button android:id="@+id/first"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_weight="0.5"
android:text="学生咨询"/>
<Button android:id="@+id/second"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_weight="0.5"
android:text="水客茶园"/>
</LinearLayout>
<ScrollView android:measureAllChildren="true" android:id="@+id/body"
android:layout_height="fill_parent"
android:layout_width="wrap_content">
</ScrollView>
</RelativeLayout>
复制代码
show.xml
<p><?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="<a >http://schemas.android.com/apk/res/android</a>"
xmlns:tools="<a >http://schemas.android.com/tools</a>"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="fill_vertical">
<WebView
android:id="@+id/show"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
/>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
></p><p> <ImageButton
android:id="@+id/refresh"
android:layout_width="wrap_content"
android:layout_height="30dip"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:background="@drawable/ic_refresh" /></p><p> <ImageButton
android:id="@+id/back"
android:layout_width="wrap_content"
android:layout_height="30dip"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:background="@drawable/ic_back" /></p><p> </RelativeLayout>
</LinearLayout>
</p><p> </p>
复制代码
AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.meishuihuiyin.bbs"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="5"
android:targetSdkVersion="15" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/test" >
<activity
android:name=".MainActivity"
android:label="@string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".ZiXunChu">
</activity>
</application>
</manifest>
复制代码
哥们纠结,报了N多错误。。。
求解
作者:
周开伟
时间:
2012-8-23 17:33
空指针错误
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2