Rik*_*Rik 4 tabs android button android-intent android-tabhost
这是我的代码.它很简单但是当我点击按钮"button1"时,没有任何反应.我做错了什么?
public class TestTab extends Activity {
Button button1;
Intent i;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button1 = (Button) findViewById(R.id.btnScore);
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "On Click Event", 5000).show();
//i = new Intent(TestTab.this, AndroidTab.class);
//startActivity(i);
}
});
}}
Run Code Online (Sandbox Code Playgroud)
第二 -
package com.example.picturegame2;
import android.app.TabActivity;
public class AndroidTab extends TabActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.tabzz);
TabHost tabHost = getTabHost();
TabSpec localspec = tabHost.newTabSpec("Local");
localspec.setIndicator("Local", getResources().getDrawable(R.drawable.friend));
Intent localIntent = new Intent(this, local.class);
localspec.setContent(localIntent);
TabSpec worldwidespec = tabHost.newTabSpec("worldwide");
worldwidespec.setIndicator("Worldwide", getResources().getDrawable(R.drawable.worldwide));
Intent worldwideIntent = new Intent(this, worldwide.class);
worldwidespec.setContent(worldwideIntent);
tabHost.addTab(worldwidespec);
tabHost.addTab(localspec);
}
}
Run Code Online (Sandbox Code Playgroud)
第三 -
package="com.example.picturegame2"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<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=".local"/>
<activity android:name=".AndroidTab"/>
<activity android:name=".TestTab"/>
<activity android:name=".tabzz"/>
<activity android:name=".worldwide"/>
</application>
Run Code Online (Sandbox Code Playgroud)
布局 -
<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" >
<Button
android:layout_width="250dp"
android:layout_height="100dp"
android:text="Start New Game"
android:id="@+id/BtnGame"
android:textSize="35dp"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
/>
<ListView
android:id="@+id/LsOpenGames"
android:layout_width="250dp"
android:layout_height="210dp"
android:layout_centerHorizontal="true"
android:layout_marginTop="130dp"
></ListView>
<Button
android:layout_width="120dp"
android:layout_height="40dp"
android:text="Profile"
android:id="@+id/btnProfile"
android:textSize="15dp"
android:layout_marginLeft="40dp"
android:layout_marginTop="375dp"
/>
<Button
android:layout_width="120dp"
android:layout_height="40dp"
android:text="Score"
android:id="@+id/btnScore"
android:textSize="15dp"
android:layout_marginLeft="170dp"
android:layout_marginTop="375dp"
/>
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)
新Logcat:11-29 12:15:39.552:D/gralloc_goldfish(613):未检测到GPU仿真的仿真器.11-29 12:15:40.062:I/Choreographer(613):跳过51帧!应用程序可能在其主线程上做了太多工作.11-29 12:16:08.902:I/Choreographer(613):跳过30帧!应用程序可能在其主线程上做了太多工作.
您必须删除全局声明的Intent变量,如下所示,
Button button1;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button1 = (Button) findViewById(R.id.btnScore);
button1.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(TestTab.this, AndroidTab.class);
startActivity(i);
}
});
}
Run Code Online (Sandbox Code Playgroud)
或试试这个,
Button button1;
Intent i;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button1 = (Button) findViewById(R.id.btnScore);
button1.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View v) {
i = new Intent(TestTab.this, AndroidTab.class);
startActivity(i);
}
});
}
Run Code Online (Sandbox Code Playgroud)
您startActivity(i)可能已经引用了您忘记初始化的全局声明的Intent变量,并在本地创建了一个与全局声明的名称"i"相同的Intent对象.
如果上述想法对您没有帮助,那么用户Yajneshwar Mandal建议您需要尝试一下.或者在ClickListener中放入一个日志以查看控件是否进入它.
| 归档时间: |
|
| 查看次数: |
3343 次 |
| 最近记录: |