我如何使用打算更改活动?

时间:2013-04-08 13:49:35

标签: java android android-intent

我做了一些按钮,我想在点击按钮1时更改为另一个活动。 但它找不到我的viewImave.class

我是

这是我的MainActivity.java

   package com.example.image;   
   import android.os.Bundle;
   import android.view.View.*;
   import android.view.View;
   import android.view.View.OnClickListener;
   import android.widget.Button;
   import android.widget.Toast;
   import android.app.Activity;
   import android.content.Intent;
   import android.widget.ImageView;


   public class MainActivity extends Activity implements OnClickListener {
   Toast toast;
   @Override
   protected void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.activity_main);
     final Button button1 = (Button)findViewById(R.id.button1);       
     button1.setText("take Image");
     button1.setOnClickListener(this); 
     final Button button2 = (Button)findViewById(R.id.button2);
     button2 = (Button)findViewById(R.id.button2);
     button2.setText("take Image2");
     button2.setOnClickListener(this);
     }

   @Override
     public void onClick (View v) {
         if(v.getId()==R.id.button1){
            Intent showimageIntent = new Intent(this, ViewImave.class);
                startActivity(showimageIntent);
             }

         else if(v.getId()==R.id.button2)
         { 
           toast = Toast.makeText(this, "onclickbutton2", Toast.LENGTH_SHORT);
           toast.show();
         }

         else
             {
           toast = Toast.makeText(this, "error", Toast.LENGTH_SHORT);
           toast.show();
         }
         }       
     }

这是我的ViewImave.java

         package com.example.image;
         import android.app.Activity;
         import android.graphics.Bitmap;
         import android.graphics.Canvas;
         import android.graphics.Color;
         import android.graphics.drawable.Drawable;
         import android.os.Bundle;
         import android.view.View;
         import android.widget.Button;
         import android.widget.ImageView;
         import android.widget.Toast;
         import android.app.Activity;
         import android.view.Menu;

       public class ViewImave extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {//
     setContentView(R.layout.showimage);
     Toast toast = Toast.makeText(this, "newimage oncreate", Toast.LENGTH_SHORT);
         toast.show();
     }
        }

这是showimage.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" >
   <Showimage
     android:layout_width="fill_parent"
 android:layout_height="fill_parent"
     android:id="@+id/koala" />

    </LinearLayout>

这是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"
     tools:context=".MainActivity" >

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"
    android:text="Button" 
 />

<Button
    android:id="@+id/button2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true"
    android:layout_below="@+id/button1"
    android:text="Button" />

    </RelativeLayout>

2 个答案:

答案 0 :(得分:1)

试试这个:

 @Override
 public void onClick (View v) {
     if(v.getId()==R.id.button1){
         Intent showimageIntent = new Intent(getApplicationContext(), ViewImave.class);
               startActivity(showimageIntent);
         }

并在您的清单文件中添加ViewImave活动,如下所示:

<activity android:name="com.example.image.ViewImave" />

答案 1 :(得分:1)

在AndroidManifest中必须是:

   <activity android:name="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=".ViewImave"/>
MainActivity.java中的

public class MainActivity extends Activity implements View.OnClickListener {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    final Button button1 = (Button)findViewById(R.id.button1);
    button1.setText("take Image");
    button1.setOnClickListener(this);
    final Button button2 = (Button)findViewById(R.id.button2);
    button2.setText("take Image2");
    button2.setOnClickListener(this);
}

@Override
public void onClick(View v) {
   switch (v.getId()){
       case R.id.button1:
       {
           Intent showImageIntent = new Intent(this, ViewImave.class);
           startActivity(showImageIntent);
           break;
       }
       case R.id.button2:
       {
           Toast toast = Toast.makeText(this, "onClickButton2", Toast.LENGTH_SHORT);
           toast.show();
           break;
       }
   }
}
}
ViewImave.java中的

public class ViewImave extends Activity {
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.showimage);
        Toast toast = Toast.makeText(this, "new image on create", Toast.LENGTH_SHORT);
        toast.show();
    }
}

检查一下。希望它有所帮助