0

I would like to make a simple app with a button that shows a picture when pressed. Here is what I've written.

manifest:

<?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>

main:

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>

activity full screen:

<?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>

styles:

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

When I run, everything is fine, but when I launch the app I get the following error: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.

Where is my mistake?

Helios83
  • 87
  • 1
  • 12
  • possible duplicate of [ActionBarCompat: java.lang.IllegalStateException: You need to use a Theme.AppCompat](http://stackoverflow.com/questions/18063395/actionbarcompat-java-lang-illegalstateexception-you-need-to-use-a-theme-appcom) – WOUNDEDStevenJones Sep 03 '15 at 17:33

1 Answers1

0

In Style.xml, you need to use AppCompat Theme - (RECOMMENDED)

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

The above theme is Material Light theme.

Otherwise, you can leave AppCompat(But I don't recommend this as AppCompat is used for Material themes for lower Android versions like Jellybean, Honeycomb and Gingerbread) by using this code in Main - (NOT RECOMMENDED)

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

    ... Enter the code you use here

}

Change this too if you use the NOT RECOMMENDED method -

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);
}

If you like this answer, please mark it as selected. PS - Material.light is available only on Android 4.4 (Kitkat) +

FadedCoder
  • 1,517
  • 1
  • 16
  • 36
  • If I change Material into AppCompat, I get that such as symbol is not resolved. Should I install something? – Helios83 Sep 03 '15 at 18:10
  • Actually, I have solved with this: – Helios83 Sep 04 '15 at 06:57
  • Ohh, that meant you didn't have the latest AppCompat library. Anways, nice that you solved your problem. – FadedCoder Sep 04 '15 at 09:20