启动应用程序时出现意外错误

时间:2017-10-14 20:12:17

标签: java android eclipse android-intent

我是java新手,我开始为Android编写应用程序,但我有一个错误,我认为问题是关于意图过滤器,但我真的不确定..我不明白任何一件事在这里。我怎么能达到我的水平?我的代码是一个很好的起点吗?

错误讯息:

     $ adb shell am start -n "test.beta1/test.beta1.MainActivity" -a 
       android.intent.action.MAIN -c android.intent.category.LAUNCHER
       Unexpected error while executing: am start -n 
       "test.beta1/test.beta1.MainActivity" -a android.intent.action.MAIN -c 
       android.intent.category.LAUNCHER
       Error while Launching activity

主要活动代码(已更新):

   package test.beta1;

   import android.content.Intent;
   import android.media.Image;
   import android.net.Uri;
   import android.provider.MediaStore;
   import android.support.v7.app.AppCompatActivity;
   import android.os.Bundle;
   import android.view.View;
   import android.widget.Button;
   import android.widget.ImageView;

   public class MainActivity extends AppCompatActivity implements 
   View.OnClickListener{
   private static final int RESULT_LOAD_IMAGE  = 1;
   ImageView imageToUpload;
   Button bUploadImage;


  @Override
   protected void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.activity_main);
     imageToUpload  =  (ImageView)  findViewById(R.id.imageToUpload);
     bUploadImage  = (Button) findViewById(R.id.etUploadName);
     imageToUpload.setOnClickListener(this);
     bUploadImage.setOnClickListener(this);
  }

  @Override
  public void onClick(View v) {
   switch(v.getId()){
       case R.id.imageToUpload:
           Intent galleryIntent = new Intent(Intent.ACTION_PICK, 
         MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
          startActivityForResult(galleryIntent,RESULT_LOAD_IMAGE);
          break;
      case R.id.bUploadImage:

          break;

  }
}

 @Override
   protected void onActivityResult(int requestCode, int resultCode, Intent 
    data) 
  {
    super.onActivityResult(requestCode, resultCode, data);
     if(requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && data != 
       null){
        Uri selectedImage  = data.getData();
        imageToUpload.setImageURI(selectedImage);

       }
      }
  }

Activity_main.xml代码:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:padding="10dp"
android:layout_height="match_parent">

<LinearLayout
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <ImageView
        android:id="@+id/imageToUpload"
        android:layout_gravity="center_horizontal"
        android:layout_width="150dp"
        android:layout_height="150dp" />

    <EditText
        android:id="@+id/etUploadName"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <Button
        android:id="@+id/bUploadImage"
        android:text="Upload Image"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</LinearLayout>

拜托,你能解释一下如何解决这个问题吗?

2 个答案:

答案 0 :(得分:1)

您可以从此错误中找到问题的根源:

/Users/abhinanda/Desktop/Beta1/app/src/main/java/test/beta1/MainActivity.java:16: error: variable bDownloadedImage is already defined in class MainActivity Button bUploadImage, bDownloadedImage; ^

/Users/abhinanda/Desktop/Beta1/app/src/main/java/test/beta1/MainActivity.java:28: error: incompatible types: Button cannot be converted to ImageView bDownloadedImage = (Button) findViewById(R.id.etDownloadName); ^ 2 errors

首先,您定义 bDownloadedImage 多个 其次,您正在尝试将Button转换为ImageView,这是一个错误。

检查你的布局。

答案 1 :(得分:0)

在你的onCreate中,你将错误的id输出到bUploadImage。您正在投射R.id.etUploadName的ID是Textview