【发布时间】:2018-03-26 17:03:48
【问题描述】:
我是 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>
请问,你能解释一下如何解决这个问题吗?
【问题讨论】:
-
您在尝试运行 ADB 日志时应该有一个堆栈跟踪。你能提供日志吗?
-
事件日志:15/10/17 12:50 AM Gradle 同步完成 1:19 AM 执行任务:[:app:assembleDebug] 1:19 AM Gradle 构建完成,出现 4 个错误5s 362ms 1:20 AM 执行任务:[:app:assembleDebug] 1:20 AM Gradle 构建完成,2s 261ms 1:26 AM 执行任务:[:app:assembleDebug] 1:26 AM Gradle build完成于 1s285ms 1:27 AM 执行任务:[:app:assembleDebug] 1:27 AM Gradle 构建完成于 4s424ms 1:28 AM 执行任务:[:app:assembleDebug] 1:28 AM Gradle 构建完成于 1s879ms 1:29 AM 会话“应用程序”:启动活动时出错
-
我在帖子中添加了 gradle 控制台(已编辑)
-
我在运行时得到了apk文件,但是打开后,它会在2秒后关闭
标签: java android eclipse android-intent