2015-09-03 3 views
0

Я хотел бы сделать простое приложение с кнопкой, которая показывает изображение при нажатии. Вот что я написал.кнопка, чтобы показать изображение приложения для Android

манифеста:

<?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: You ne ed для использования темы Theme.AppCompat (или потомка) с этим действием.

Где моя ошибка?

+0

возможно дубликат [ActionBarCompat: java.lang.IllegalStateException: Вы должны использовать Theme.AppCompat] (http://stackoverflow.com/questions/18063395/actionbarcompat-java-lang-illegalstateexception-you-need-to-use-a-theme-appcom) – WOUNDEDStevenJones

ответ

0

В Style.xml, вам нужно использовать AppCompat Theme - (РЕКОМЕНДУЕМУЮ)

<!-- Base application theme. --> 
<style name="AppTheme" parent="android:Theme.AppCompat.Light"> 
<!-- Customize your theme here. --> 
</style> 

выше тема Материал Light тема.

В противном случае, вы можете оставить AppCompat (Но я не рекомендую это как AppCompat используется для материала темы для более низких версий Android, как Jellybean, Honeycomb и Gingerbread), используя этот код в Main - (НЕ РЕКОМЕНДУЕТСЯ)

public class MainActivity extends Activity { // Changed from AppCompatActivity to just Activity 

    ... Enter the code you use here 

} 

Изменить это тоже, если вы используете 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. PS -Material.light доступна только на Android 4.4 (KitKat) +

+0

Если я изменю материал в AppCompat, я получаю, что такой символ не разрешен. Должен ли я что-то установить? – Helios83

+0

На самом деле, я решил с этим: