我在AVD中收到消息 - 不幸的是已经停止了,请帮我解决这个错误,我想在android中学到更多但是被这个错误所吸引。
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" >
<ImageView
android:id="@+id/iv"
android:layout_width="250dp"
android:layout_height="250dp"
android:layout_gravity="center"
android:src="@drawable/ic_launcher"
android:contentDescription="@null"
/>
<ImageButton
android:id="@+id/ib"
android:layout_width="125dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:src="@drawable/ic_launcher"
android:contentDescription="@null"
/>
<Button
android:id="@+id/bt"
android:layout_width="125dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="set wallpaper" />
</LinearLayout>
java代码
package com.example.second;
public class photo extends Activity implements View.OnClickListener {
ImageView iv;
Button bt;
ImageButton ib;
Intent i ;
Bitmap bmp;
final static int cameraData =0;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
intsallttion();
InputStream is = getResources().openRawResource(R.drawable.ic_launcher);
bmp = BitmapFactory.decodeStream(is);
}
private void intsallttion() {
// TODO Auto-generated method stub
iv = (ImageView) findViewById(R.id.iv);
bt = (Button) findViewById(R.id.bt);
ib = (ImageButton) findViewById(R.id.ib);
bt.setOnClickListener(this);
ib.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch(v.getId()){
case R.id.bt:
try {
getApplicationContext().setWallpaper(bmp);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
break;
case R.id.ib:
i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(i,cameraData);
break;
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if(resultCode==RESULT_OK){
Bundle extras = data.getExtras();
bmp = (Bitmap) extras.get("data");
iv.setImageBitmap(bmp);
}
}
}
mainfest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.second"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="16" />
<uses-permission android:name="android.permission.SET_WALLPAPER"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".omar"
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=".Menu"
android:label="@string/app_name" >
<intent-filter>
<action android:name="com.example.second.MENU" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="com.example.second.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".TextPlay"
android:label="@string/app_name" >
</activity>
<activity
android:name=".Email"
android:label="@string/app_name" >
</activity>
<activity
android:name=".photo"
android:label="@string/app_name" >
</activity>
</application>
</manifest>
答案 0 :(得分:6)
您忘记添加.xml文件语句,如
setContentView(R.layout.activity_main);
在onCreate()
方法中,在此行之后
super.onCreate(savedInstanceState);
由于您忘记添加.xml,您的活动无法获取该组件的详细信息,如ImageView,Button&amp;的ImageButton。
答案 1 :(得分:0)
您在清单中将活动omar定义为第一个午餐的应用程序:
<activity
android:name=".omar"
android:label="@string/app_name" >
<!-- remove this intent filter if you don't want to lunch this activity first-->
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
您向我们展示了活动photo
。
如果你想在这个活动photo
吃午饭,你必须像这样更新你的xml:
<activity
android:name=".photo"
android:label="@string/app_name" >
<!--this intent filter detects which activity to lunch the first-->
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".omar"
android:label="@string/app_name" >
<!-- you should remove the intent filter from here -->
</activity>
此外,在java代码中,您必须在onCreat()中定义布局。所以你的onCreat看起来像:
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
// I am supposing that your xml file is called activity_photo.xml .
setContentView(R.layout.activity_photo);
intsallttion();
// getRessources() : get the ressorces of your application (present in your /res folder)
// openRawResource(R.drawable.ic_launcher) : get "ic_launcher" from /drawable and put it in
// InputStream
InputStream is = getResources().openRawResource(R.drawable.ic_launcher);
//this line creats you a Bitmap of the picture ic_launcher
bmp = BitmapFactory.decodeStream(is);
}
回答你的上一条评论:
在ImageView中,您将使用此代码android:src="@drawable/ic_launcher"
放置drawable / ic_launcher。这是您的图片视图:
<ImageView
android:id="@+id/iv"
android:layout_width="250dp"
android:layout_height="250dp"
android:layout_gravity="center"
android:src="@drawable/ic_launcher"
android:contentDescription="@null"
/>
然后通过代码,您将相同的图片放在此ImageView drawable/ic_launcher
中,您将其转换为Bitmap并调用bmp。
如果您想要查看更改内容,则必须从图像视图中删除此android:src="@drawable/ic_launcher"
,然后单击按钮,您将看到图片显示在ImageView中。然后删除这两行代码:InputStream is = getResources().openRawResource(R.drawable.ic_launcher);
bmp = BitmapFactory.decodeStream(is);
然后单击按钮,您将看到图片没有出现。
结论:这两行是从您的/ res拍摄的照片并将其放入位图中。然后clickListener将此位图放在ImageView中。如果删除它们,单击Listeners将在ImageView中放置一个空的Bitmap。