Error on "findViewById(R.id.video)" - android

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

Related

Whether the resource id in Android xml can be used before being declared

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

OpenGL ES 2: GLSurface View Showing Black Screen When SetContentView is set to an XML File

I'm fairly new to OpenGL and my problem is when I do SetContentView("R.layout.main") nothing happens, however when I do SetContentView("mGLView")the game is displayed fine. My main.xml file contains an surface view and therefore I would of thought that it would be displayed, since main.xml has an surface view.
I suspect a way to fix this, will be done with addview or findviewbyid, however my implication of this have failed.
Relevent MainActivity Code:
public class OpenGLES20Activity extends Activity {
private GLSurfaceView mGLView;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mGLView = new MyGLSurfaceView(this);
setContentView(R.layout.main);
main.xml code:
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout android:id="#+id/linearLayout1" android:layout_width="wrap_content"
android:layout_height="wrap_content">
<SurfaceView
android:id="#+id/SurfaceView"
android:layout_width="500dp"
android:layout_height="478dp">
</SurfaceView>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/Timer"
android:id="#+id/Timer"/>
</RelativeLayout>
</FrameLayout>
Any help is appreciated.
You need to set your extended GLSurfaceView instead of just SurfaceView in the xml file so just change SurfaceView to com.example.yourapp.MyGLSurfaceView:
<com.example.yourapp.MyGLSurfaceView
android:id="#+id/SurfaceView"
android:layout_width="500dp"
android:layout_height="478dp"/>

Why the program not found activity_main and web_view?

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.

Simple Android app fails without starting

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>

Visibility of ScrollView

I am using two ScrollViews and want to set visibility of one as GONE or INVISIBLE but when I do it by following java code the application terminates. If I do the same thing using Properties pan, in the eclipse, it just works. What is wrong?
Java code is:
public class Test extends Activity {
List<ScrollView> sv;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
sv = new ArrayList<ScrollView>();
sv.add((ScrollView)findViewById(R.id.scrollView1));
sv.add((ScrollView)findViewById(R.id.scrollView2));
sv.get(1).setVisibility(View.GONE);
setContentView(R.layout.main);
}
}
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="fill_vertical">
<ScrollView android:layout_height="wrap_content" android:id="#+id/scrollView1" android:layout_width="match_parent">
<LinearLayout android:layout_height="wrap_content" android:layout_width="match_parent" android:id="#+id/linearLayout1" android:orientation="vertical">
<TextView android:text="TextView" android:id="#+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
</LinearLayout>
</ScrollView>
<ScrollView android:layout_height="wrap_content" android:layout_width="match_parent" android:id="#+id/scrollView2">
<TextView android:text="TextView" android:layout_width="match_parent" android:id="#+id/textView2" android:layout_height="match_parent"></TextView>
</ScrollView>
</LinearLayout>
you haven't setContentview(yourLayout.xml)
it wont be able to findViewById.
do the findViewById AFTER the setContentView
First of you have to set layout file.
setContentView(R.layout.new_view);
sv = new ArrayList<ScrollView>();
sv.add((ScrollView)findViewById(R.id.scrollView1));
sv.add((ScrollView)findViewById(R.id.scrollView2));
sv.get(1).setVisibility(View.GONE);
try this if you are getting any error then please post logcat.

Categories

Resources