我想制作一个简单的应用程序,按下按钮显示图片。 这是我写的。
清单:
<?xml version="1.0" encoding="utf-8"?>
<application
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name" >
<activity
android:label="@string/app_name"
android:name=".MainActivity" >
<intent-filter >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:theme="@style/AppTheme"
android:name="Activityfullscreen"></activity>
</application>
主:
package com.mycompany.button_show;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = (Button) findViewById(R.id.button_send);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent inf=new Intent(MainActivity.this,Activityfullscreen.class);
startActivity(inf);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
.java for the second activity:
public class Activityfullscreen extends AppCompatActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_full_screen);
ImageView img = (ImageView) findViewById(R.id.widget45);
img.setBackgroundResource(R.drawable.aa);
}
}
activity_main:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:id="@+id/button_send"
android:layout_width="fill_parent"
android:layout_height="120dp"
/>
<TextView
android:id="@+id/widget45"
android:layout_width="fill_parent"
android:layout_height="120dp"
/>
</LinearLayout>
活动全屏:
<?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/widget45"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</LinearLayout>
样式:
<!-- Base application theme. -->
<style name="AppTheme" parent="android:Theme.Material.Light">
<!-- Customize your theme here. -->
</style>
当我跑步时,一切都很好,但是当我启动应用程序时,我收到以下错误: java.lang.IllegalStateException:您需要在此活动中使用Theme.AppCompat主题(或后代)。
我的错误在哪里?
答案 0 :(得分:0)
在AppCompat
中,您需要使用<!-- Base application theme. -->
<style name="AppTheme" parent="android:Theme.AppCompat.Light">
<!-- Customize your theme here. -->
</style>
主题 - (推荐)
AppCompat
以上主题是Material Light主题。
否则,您可以离开public class MainActivity extends Activity { // Changed from AppCompatActivity to just Activity
... Enter the code you use here
}
(但我不建议这样做,因为AppCompat用于较低Android版本的材质主题,如Jellybean,Honeycomb和Gingerbread),在Main中使用此代码 - (< strong> NOT WOMMENDED )
NOT RECOMMENDED
如果您使用public class Activityfullscreen extends Activity { // Changed from AppCompatActivity to just Activity
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_full_screen);
ImageView img = (ImageView) findViewById(R.id.widget45);
img.setBackgroundResource(R.drawable.aa);
}
方法 -
selected
如果您喜欢这个答案,请将其标记为Material.light
。
PS - entry = prompt("Enter number grade from 0 through 100\n" +
"Or enter STOP to end entries", "STOP");
entry = parseInt(entry);
仅适用于Android 4.4(Kitkat)+