The Bolt The Bolt - 3 months ago 12
Java Question

Error with AppCompatAcitivity

I have just started working on Android Studio and I am not able to solve this problem. I Have been getting the uploaded errors for a long time. Here are the files I am using. I have checked the package name as well, so that is not the issue.

MainAcitivity.java

package com.example.android.justjava;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;

/**
* This app displays an order form to order coffee.
*/
public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}

/**
* This method is called when the order button is clicked.
*/
public void submitOrder(View view) {
display(1);
}

/**
* This method displays the given quantity value on the screen.
*/
private void display(int number) {
TextView quantityTextView = (TextView) findViewById(
R.id.quantity_text_view);
quantityTextView.setText("" + number);
}
}


activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="16dp"
android:orientation="vertical"
tools:context=".MainActivity">


<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
android:text="Quantity"
android:textAllCaps="true" />

<TextView
android:id="@+id/quantity_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0"
android:textColor="#000000"
android:textSize="16sp" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="Order"
android:textAllCaps="true"
android:onClick="submitOrder" />

</LinearLayout>


Error messages

Answer

You probably didn't add the appropriate support lib dependency. Add the following line to the dependencies block in your module's build.gradle file:

compile 'com.android.support:appcompat-v7:24.2.0'

You should also read the Support Library Setup before using the support libs.