切换到onClick上的不同活动

时间:2011-03-27 17:04:12

标签: java android

我是Android新手,目前正在探索它。 我有两个图像按钮,必须在点击上加载不同的活动。

ImageButton btn1= (ImageButton)findViewById(R.id.timetable);
btn1.setOnClickListener(btnListener1);

ImageButton btn2= (ImageButton)findViewById(R.id.location);
btn2.setOnClickListener(btnListener2);
private OnClickListener btnListener1 = new OnClickListener()
{
    public void onClick(View view)
    {                        
         Intent myIntent = new Intent(getBaseContext(), HelloWorld1.class);
         startActivity(myIntent);
    }
};

private OnClickListener btnListener2 = new OnClickListener()
{
    public void onClick(View view)
    {                 
        Intent myIntent2 = new Intent(getBaseContext(), HelloWorld2.class);         
        startActivity(myIntent2);
    }
};

//我的清单......

<activity android:name="myApp" 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=".HelloWorld1"></activity>
<activity android:name=".HelloWorld2"></activity>

//和我的main.xml

<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout
android:id="@+id/widget34"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:background="#ffffff"
>

<GridView
    android:id="@+id/widget36"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:numColumns="2"
    android:layout_x="110px"

    android:layout_y="32px"
    android:layout_centerInParent="true">
</GridView>
<ImageButton
    android:id="@+id/timetable"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_x="210px"
    android:layout_y="142px"
    android:background="@drawable/icon2">
</ImageButton>
<ImageButton
    android:id="@+id/location"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_x="100px"
    android:layout_y="342px"
    android:background="@drawable/icon">
</ImageButton>

此代码会导致错误,任何人都可以指出我出错的地方。 非常感谢提前。

3 个答案:

答案 0 :(得分:1)

让我们抛开finish()方法,因为我不知道它在那里做什么:) 案例1:仔细查看您的活动xml视图文件,您可能会意外地将按钮定义为Button而不是ImageButton - &gt;错误

案例2:不要使用view.getContext(),而是使用getBaseContext()getApplicationContext()

答案 1 :(得分:0)

您正在呼叫startActivityForResult(),然后立即呼叫finish()。如果活动结束,结果会在哪里?

你想要什么样的行为,取而代之的是什么。您的具体情况越多,获得的帮助质量就越好。

答案 2 :(得分:0)

尝试编写类似下面的代码。您还可以定义第一个功能,该功能将在按下按钮时创建新活动。

public class HelloAndroid extends Activity {
private Button button_1;
private Button button_2;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    initialiyeFields();
}

private void initialiyeFields(){
    button_1 = (Button)findViewById(R.id.button1);
    button_1.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(HelloAndroid.this, HelloWord1.class);
            startActivity(intent);
        }
    });

    button_2 = (Button)findViewById(R.id.button2);
    button_2.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(HelloAndroid.this, HelloWord2.class);
            startActivity(intent);
        }
    });
}

}