I write a simple program and when i run the program, the program has errors. I have a activity_main.xml in layout folder but the program has error of activity_main. Why? What is the problem?
errors: activity_main cannot be resolved or is not a field
web_view cannot be resolved or is not a field
activity_main.xml
<?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">
<WebView
android:id="#+id/web_view"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1.0" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal">
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/button1"/>
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/button2"/>
</LinearLayout>
</LinearLayout>
MainActivity.java
public class MainActivity extends Activity {
Button button1;
Button button2;
WebView mWeb;
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mWeb= (WebView)findViewById(R.id.web_view);
button1=(Button)findViewById(R.id.button1);
button2=(Button)findViewById(R.id.button2);
button1.setOnClickListener(onClickListener);
button2.setOnClickListener(onClickListener);
}
private OnClickListener onClickListener=new OnClickListener(){
public void onClick(View v){
//don't work something
}
};
}
Sometimes Eclipse adds
import android.R
to the list of imports. This is an error and it should be deleted. Removing it sometimes resolves the described problem.
Try following:
Clean you workspace and try to run again
Check if R.java is created in gen folder or not and what is the package name there?
Make sure your Web view and R.Java share the same package name if not , change the package name of your WebView or change in manifest.xml.
Try closing and opening eclipse again.
Related
I wonder that Whether the resource id in Android xml can be used before being declared? when I test it in a demo application , compiled in Android Studio and ran in the phone both work. The xml code in demo as following:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id ="#+id/view"
android:layout_width="100dp"
android:layout_height="100dp"
android:background="#color/black"
android:layout_alignBottom="#id/bt"/>
<Button
android:id="#+id/bt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" am a button" />
</RelativeLayout>
The Activity code as following:
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
I have a problem with an Android application. In the app there is an xml file which contains a RelativeLayout and inside a Scroll view. Inside the scrollview there some images which I want to display. The problem is that when there are 3 ImageView inside the scrollview the activity and the xml page works fine but ,when I add the 4th ImageView , application crashes when I start the activity.
I am posting the code below, the problem occurs when the 4th ImageView is added. Any ideas?
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:alwaysDrawnWithCache="true"
android:fadeScrollbars="true"
android:verticalScrollbarPosition="right">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#FFFFFF"
android:gravity="center"
android:orientation="vertical">
<ImageView
android:id="#+id/Double_image"
android:layout_width="match_parent"
android:layout_height="200dp"
android:scaleType="centerCrop"
android:src="#drawable/trouble" />
<ImageView
android:id="#+id/Secret_image"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_below="#+id/Double_image"
android:scaleType="centerCrop"
android:src="#drawable/secret" />
<ImageView
android:id="#+id/kidnap_image"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_below="#id/Secret_image"
android:scaleType="centerCrop"
android:src="#drawable/kidnap" />
<ImageView
android:id="#+id/kidnap2_image"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_below="#id/kidnap_image"
android:scaleType="centerCrop"
android:src="#drawable/kidnap2" />
</RelativeLayout>
</ScrollView>
NOTE: The activity which is connected with this xml file doesn't have any functionality.
Here is the Activity class connected with the above xml.
package com.example.thomas.firstcue;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class RoomsActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_rooms);
}
}
I start the activity from Main through a button click and the function StartActivity
rooms_but.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View view) {
startActivity(intentrooms);
}
});
The application starts normally on phone but when I am pressing the button to Start the activity , the app crashes.
On phone error
One possibility of crashing app is your 4th image which reside in drawable have v24 like kidnap.png(v24)
so first delete this image from drawable folder(..\app\src\main\res\drawable-v24) and again paste it in drawable(..\app\src\main\res\drawable) folder with only kidnap.png
Trying to change a layout from an ImageButton but when i run it on my phone it does not switch instead it says program has stopped.
Java;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageButton ImageButton = (ImageButton) findViewById(R.id.image);
ImageButton.setBackgroundColor(1);
ImageButton.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View view)
{
setContentView(R.id.aaa);
}
});
}
}
XML for first layout:
<?xml version="1.0" encoding="utf-8"?>
<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"
tools:context="com.example.bassammetwally.anew.MainActivity">
<RelativeLayout
android:layout_width="420dp"
android:layout_height="50dp"
android:background="#3955a1">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Writer's Block"
android:textColor="#ffffff"
android:textSize="30dp"
android:layout_marginLeft="10dp"
android:layout_marginTop="4dp"
android:id="#+id/Asd"/>
<ImageButton
android:layout_width="20dp"
android:layout_height="40dp"
android:src="#drawable/pen"
android:layout_marginLeft="380dp"
android:layout_marginTop="5dp"
android:scaleType="fitXY"
android:adjustViewBounds="true"
android:id="#+id/image"/>
</RelativeLayout>
</LinearLayout>
Second layout to change to;
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/aaa">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Ans"/>
</LinearLayout>
I think it might have something to do with my syntax.
You need to use a layout reference instead of an id
setContentView(R.layout.view);
Where view.xml is the layout you want.
You may want to rethink your approach, though. Using Fragments is preferred over completely replacing the layout because your ImageButton is no longer usable and you'll need to recall findViewById for any new views that are displayed.
If you just want to load a layout file and add it to the current layout, see Inflating one layout
please help me as i'm hell stuck in an idiot error :( ..
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<VideoView
android:id="#+id/video"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#android:color/background_dark"/>
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</RelativeLayout>
this is xml which i think is correct.
public class MainActivity extends Activity {
VideoView videoview;
Button button;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//videoview=(VideoView)findViewById(R.id.video);
//button=(Button)findViewById(R.id.button);
}
and that one is java file..im getting an error when im getting videoview and button through their id's. error is "video cannot be resolved or is not a field"
please tell me if you know the solution
thankyou
Build and clean the project will solve your problem
Hello there people of stack overflow, I have just started developing with android and have run into a bit of a problem. I am trying to run a very simple app that has a single button, which when clicked updates the text to the current time. However, the app does not even start up. I am following a book. "Beginning Android 2", and am just learning about XML layouts. The same app worked without an XML layout, so it may be something to do with my xml.
Anyways, I'm running it on a droid 2, if that helps.
Heres the codes.
package apt.tutorial;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import java.util.Date;
public class FirstApp extends Activity implements View.OnClickListener {
/** Called when the activity is first created. */
Button btn;
#Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
btn = (Button)findViewById(R.id.button);
btn.setOnClickListener(this);
updateTime();
}
public void onClick(View view) {
updateTime();
}
private void updateTime() {
btn.setText(new Date().toString());
}
}
And the XML file
<Button xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/button"
android:text=""
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
Thanks in advance.
You should enclose the button inside a layout (linear, relative, etc):
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" >
<Button xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/button"
android:text=""
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</LinearLayout>
Both answers are incorrect. The only line missing in that xml is the first one:
<?xml version="1.0" encoding="utf-8"?>
Of course you will need some kind of ViewGroup to make any meaningful UI, but setContentView can take any View as parameter
The problem is that your root layout must be a ViewGroup. Change the layout to something like this for example:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button
android:id="#+id/button"
android:text=""
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
</LinearLayout>