Issue
I'm starting on basic Android development. I'm trying to implement a simple app where in clicking a button will cause a toast message to pop up. But while coding for responding to the click of the button, I'm getting this error: "No resource found that matches the given name (at 'id' with value '@id/button1')."
I tried renaming the button using Properties & XML file, as a hard-coded string, but to no avail. I viewed some questions here too, but there were minor errors such as "@id+" instead of "@+id" in them. There are no such errors in my code. Others suggested importing the R file as "import com.example.R;", but I'm using only this application, and nothing from outside that would need importing. Still, I added that import statement, just for the sake of it. It did not help either. So I'm finally posting this question.
I'm labeling the statements that Eclipse red-lines as errors as "<-- ERROR (num)" for reference.
Here's the part of button's Java code:
final Button btn_Toast = (Button) findViewById(R.id.button1); <-- ERROR (1)
btn_Toast.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Toast.makeText(MainActivity.this, "Here's your toast.", Toast.LENGTH_LONG).show();
}
});
And here's the XML for the the screen:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@id/button1" <-- ERROR (2)
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.helloworld.MainActivity$PlaceholderFragment" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="57dp"
android:text="View In Toast" />
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_below="@+id/textView1"
android:layout_marginTop="68dp"
android:ems="10" >
<requestFocus />
</EditText>
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="146dp"
android:text="@string/hello_world" />
</RelativeLayout>
Here are the LogCat errors. When I opened LogCat, there were a lot of them, and didn't know which ones to paste here. So I kept in mind the last time stamp, and run the app again, which generated new error entries with newer time stamps. The new errors are the ones I'm posting here.
07-06 07:33:39.947: E/memtrack(1877): Couldn't load memtrack module (No such file or directory)
07-06 07:33:39.947: E/android.os.Debug(1877): failed to load memtrack module: -2
07-06 07:33:42.447: E/memtrack(1888): Couldn't load memtrack module (No such file or directory)
07-06 07:33:42.447: E/android.os.Debug(1888): failed to load memtrack module: -2
07-06 07:33:43.007: E/libEGL(52): called unimplemented OpenGL ES API
07-06 07:33:43.007: E/libEGL(52): called unimplemented OpenGL ES API
07-06 07:33:43.007: E/libEGL(52): called unimplemented OpenGL ES API
07-06 07:33:43.007: E/libEGL(52): called unimplemented OpenGL ES API
07-06 07:33:43.017: E/SurfaceFlinger(52): glCheckFramebufferStatusOES error 1437272830
07-06 07:33:43.017: E/SurfaceFlinger(52): got GL_FRAMEBUFFER_COMPLETE_OES error while taking screenshot
07-06 07:33:43.017: E/libEGL(52): called unimplemented OpenGL ES API
07-06 07:33:43.017: E/libEGL(52): called unimplemented OpenGL ES API
07-06 07:33:44.267: E/AndroidRuntime(1899): FATAL EXCEPTION: main
07-06 07:33:44.267: E/AndroidRuntime(1899): Process: com.example.helloworld, PID: 1899
07-06 07:33:44.267: E/AndroidRuntime(1899): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.helloworld/com.example.helloworld.MainActivity}: java.lang.NullPointerException
07-06 07:33:44.267: E/AndroidRuntime(1899): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
07-06 07:33:44.267: E/AndroidRuntime(1899): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
07-06 07:33:44.267: E/AndroidRuntime(1899): at android.app.ActivityThread.access$800(ActivityThread.java:135)
07-06 07:33:44.267: E/AndroidRuntime(1899): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
07-06 07:33:44.267: E/AndroidRuntime(1899): at android.os.Handler.dispatchMessage(Handler.java:102)
07-06 07:33:44.267: E/AndroidRuntime(1899): at android.os.Looper.loop(Looper.java:136)
07-06 07:33:44.267: E/AndroidRuntime(1899): at android.app.ActivityThread.main(ActivityThread.java:5017)
07-06 07:33:44.267: E/AndroidRuntime(1899): at java.lang.reflect.Method.invokeNative(Native Method)
07-06 07:33:44.267: E/AndroidRuntime(1899): at java.lang.reflect.Method.invoke(Method.java:515)
07-06 07:33:44.267: E/AndroidRuntime(1899): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
07-06 07:33:44.267: E/AndroidRuntime(1899): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
07-06 07:33:44.267: E/AndroidRuntime(1899): at dalvik.system.NativeStart.main(Native Method)
07-06 07:33:44.267: E/AndroidRuntime(1899): Caused by: java.lang.NullPointerException
07-06 07:33:44.267: E/AndroidRuntime(1899): at com.example.helloworld.MainActivity.onCreate(MainActivity.java:32)
07-06 07:33:44.267: E/AndroidRuntime(1899): at android.app.Activity.performCreate(Activity.java:5231)
07-06 07:33:44.267: E/AndroidRuntime(1899): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
07-06 07:33:44.267: E/AndroidRuntime(1899): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
07-06 07:33:44.267: E/AndroidRuntime(1899): ... 11 more
07-06 07:33:45.527: E/StrictMode(710): class com.android.launcher2.Launcher; instances=2; limit=1
07-06 07:33:45.527: E/StrictMode(710): android.os.StrictMode$InstanceCountViolation: class com.android.launcher2.Launcher; instances=2; limit=1
07-06 07:33:45.527: E/StrictMode(710): at android.os.StrictMode.setClassInstanceLimit(StrictMode.java:1)
07-06 07:33:53.297: E/WindowManager(383): Starting window AppWindowToken{b33d6950 token=Token{b36f6438 ActivityRecord{b2fc6570 u0 com.example.helloworld/.MainActivity t18}}} timed out
Solution
In this case, your RelativeLayout does not need the button1 identifier, as the line is stating that you want to make the identifier for that layout the same as the button identifier (which you don't).
In a more general sense, errors related to R.id.* values usually occur because the field is not in your R.java file. This file is automatically generated from the resource data in your application.
For example, if you create a new string element in your strings.xml file in the values directory
<string name="app_name">Application</string>
an entry will be created in the R.java file to match that:
public static final int app_name=....
The same thing will happen when you add a new button (or other control) to your layout.
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="@+id/button" />
The value of the android:id field will get a static reference value generated for it in your R.java file because you are indicating a declaration of the field by prefacing it with the (+) sign. You can then use in the findViewById methods to search for it or elsewhere in your XML as a reference.
The second issue you're having is most likely due to the location you're trying to put the button listener code. If you have a MainActivity.java class with a static Fragment declared within it, you need to make sure your button listener is created inside the fragment class, if your button XML is inside a fragment XML.
When you create a new project, you will usually have the follow hierarchy:
src/main/java/com.package/MainActivity.java
src/main/res/layout/activity_main.xml
/fragment_main.xml
Your MainActivity.java file will declare a MainActivity class and a static Fragment class (PlaceholderFragment in the default, I think).
So if you put your button XML inside fragment_main.xml, your button listener needs to go after the view inflater inside the onCreateView method and be modified slightly to the below:
final Button btn_Toast = (Button) rootView.findViewById(R.id.button);
btn_Toast.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Toast.makeText(v.getContext(), "Here's your toast.", Toast.LENGTH_LONG).show();
}
});
You would search for the element within the rootView and you would use the current view context in the toast creation.
The PlaceholderFragment class will end up looking like this:
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
final Button btn_Toast = (Button) rootView.findViewById(R.id.button);
btn_Toast.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Toast.makeText(v.getContext(), "Here's your toast.", Toast.LENGTH_LONG).show();
}
});
return rootView;
}
}
Answered By - Jared
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.