-3

我做了一些按钮,我想在单击该 button1 时更改为另一个活动。但它不会找到我的 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>
4

2 回答 2

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" />
于 2013-04-08T13:53:30.817 回答
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();
    }
}

核实。希望有帮助

于 2013-04-08T13:57:02.350 回答