I import an existing project into eclipse, and always encountered this problem. First, it appears the error: R cannot be solved. After import android.R. the error turned to the following methods. "start cannot be or is not a field"
I also checked the gen folder, where contains a R.java file. The file looks good, including the following contents: I also tried to clear the project and rebuilt which didnot work. Does any body have any idea how to solve this? Thanks a lot.
R.java:
public static final class layout {
public static final int kbd=0x7f030000;
public static final int main=0x7f030001;
public static final int start=0x7f030002;
}
startactivity.java:
import android.R;
import android.R.layout;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
public class StartActivity extends Activity {
Intent mMain;
/** Called when the activity is first created. */
/* (non-Javadoc)
* #see android.app.Activity#onCreate(android.os.Bundle)
*/
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.start);//where error appears**
}
public void onStartButtonClick(View v) {
mMain = new Intent(this, MainActivity.class);
startActivity(mMain);
}
you are importing
android.R
first remove android.R and android.R.layout from import
and import R with your packagename.R
for eg. com.example.R instead of android.R
Try doing Project -> Clean. If that doesn't work it looks like this might have already been answered: R cannot be resolved - Android error
To any one who may face the similar problem with me. I found an answer from the link below:
R cannot be resolved - Android error
My problem was solved by:
1. delete all the imports lines.
2. press ctrl+shift+O to manually regenerate all imports.
And then the problem disappeared.
Many thanks for all the comments above.
Related
I am creating a hook using AndHook to test some functions getting called. I need to show a Toast inside a method without being able to get the context object (I can't directly import MainActivity because I am injecting the script without having the corresponding package when compiling so I can't use MainActivity.this). Here's the code sample:
import andhook.lib.HookHelper;
import android.widget.Toast;
import android.content.Context;
import android.graphics.BitmapFactory;
import android.graphics.Bitmap;
public class AndHookConfig {
#HookHelper.Hook(clazz = BitmapFactory.class)
private static Bitmap decodeFile(BitmapFactory thiz, String path) {
Toast.makeText(Class.forName("com.example.activity.MainActivity").this, "Bitmap.decodeFile("+path+")", Toast.LENGTH_LONG).show();
return (Bitmap)(HookHelper.invokeObjectOrigin(thiz, path));
}
}
I think the only way to do this is using reflection but the code sample doesn't compile and results in an error. Do you have any solutions ?
Thanks in advance.
I found great instrumental test tutorial on YT Advanced Android Espresso. I took code from there with small adjustment to my needs.
import static android.support.test.InstrumentationRegistry.getInstrumentation;
import static android.support.test.espresso.Espresso.onView;
import static android.support.test.espresso.action.ViewActions.click;
import static android.support.test.espresso.assertion.ViewAssertions.matches;
import static android.support.test.espresso.matcher.ViewMatchers.isAssignableFrom;
import static android.support.test.espresso.matcher.ViewMatchers.isDisplayed;
import static android.support.test.espresso.matcher.ViewMatchers.withChild;
import static android.support.test.espresso.matcher.ViewMatchers.withId;
import static android.support.test.espresso.matcher.ViewMatchers.withParent;
import static android.support.test.espresso.matcher.ViewMatchers.withText;
import static org.hamcrest.core.AllOf.allOf;
...
#Test
public void checkToolbarTitle() {
String toolbarTitile = getInstrumentation().getTargetContext().getString(R.string.my_bus_stops);
onView(allOf(isAssignableFrom(TextView.class), withParent(isAssignableFrom(Toolbar.class)))).check(matches(withText(toolbarTitile)));
}
Unfortunatelly it does not work for me. Test failed with:
android.support.test.espresso.NoMatchingViewException: No views in hierarchy found matching: (is assignable from class: class android.widget.TextView and has parent matching: is assignable from class: class android.widget.Toolbar)
What is wrong with it? How can I test it in other way?
This works for me:
onView(allOf(instanceOf(TextView.class), withParent(withId(R.id.toolbar))))
.check(matches(withText("toolbarTitile")));
SOLUTION
The method is fine. As Chiu-Ki Chan wrote in her tutorial you can "pinpoint that one particular view".
BUT you have to make sure you imported proper Toolbar:
import android.support.v7.widget.Toolbar;
instead of:
import android.widget.Toolbar;
Here's an alternative (and more idiomatic approach):
onView(withId(R.id.toolbar)).check(matches(hasDescendant(withText(toolbarTitle))))
If you're using an ActionBar, not a Toolbar, use this:
onView(allOf(instanceOf(TextView.class),
withParent(withResourceName("action_bar"))))
.check(matches(withText("My ActionBar title")));
Note: To quickly add the imports for these methods, put the blinking cursor on the unresolved method, then do Android Studio ➔ Help ➔ Find Action ➔ search for "show context action" or "show intention action" ➔ click on the result option ➔ A popup window will appear ➔ click on "Import static method ...". You can also assign a keyboard shortcut to "Show Context Actions". More info here. Another way is to enable "Add unambiguous imports on the fly" in the Settings.
I don't remember if I wrote this myself, or if I found it somewhere, but this is how I check toolbar titles:
public static Matcher<View> withToolbarTitle(CharSequence title) {
return withToolbarTitle(is(title));
}
public static Matcher<View> withToolbarTitle(final Matcher<CharSequence> textMatcher) {
return new BoundedMatcher<View, Toolbar>(Toolbar.class) {
#Override
public boolean matchesSafely(Toolbar toolbar) {
return textMatcher.matches(toolbar.getTitle());
}
#Override
public void describeTo(Description description) {
description.appendText("with toolbar title: ");
textMatcher.describeTo(description);
}
};
}
This works with all cases. Example assertion: onView(withId(R.id.toolbar)).check(matches(withToolbarTitle("title")));
Like the in-built Math class, there are a couple of methods that one can use without importing the Math class. e.g
int io = (int) Math.random();
and notice the import region: no MATH whatsoever
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JPanel;
but seeing, Math set doesn't have everything i needed, i created mine in a new class, but i can't seem to figure out what to do so i can be able to use it.
Taking a hint from the Math.java file, I've made my class final and my methods static but no avail..
Here's an excerpt of my code
package customops.Sets;
/**
*
* #author Kbluue
*/
public final class SetOpz {
public SetOpz(){}
public static int setMax(int[] set){
int out = set[0];
for(int i=1; i<set.length; i++){
out = Math.max(out, set[i]);
}
return out;
}
how do i use just the import command without having to copy and paste the SetOpz class in the DTL package?
You don't need to import Math explicitly because it is included by default. To use your own code you will have to import it. If you're using IntelliJ or Eclipse or some other smart IDE it will offer to import it for you automatically. Otherwise add a import statement at the top:
import customops.Sets.SetOpz;
You can import your method wherever you want to use with the following import statement
import static customops.Sets.SetOpz.setMax;
I know C++ at a decent level and I am trying to learn java. This will be a silly question but I cannot figure out how to import a .java file into another. I am at Eclipse IDE and in my project I have two files:
FileReader.java
Entry.java
I want to import the Entry.java in the other file but no matter what I do I get an error. Can you help me? Thx in advance.
FileReader.java :
import java.io.*;
class FileReader {
public static void main(String[] args) throws Exception {
System.out.println("Hello, World");
Entry a(10,"a title","a description");
a.print();
}
}
Entry.java:
public class Entry{
int ID;
String title;
String description;
public Entry(int id, String t,String d){
ID=id;
title=t;
description=d;
}
public void print(){
System.out.println("ID:"+ID);
System.out.println("Title:"+title);
System.out.println("Description:"+description);
}
}
At this state I get an error that Entry cannot be resolved as a variable. So I believe that it is related to the import.
Firstly
Entry a(10,"a title","a description");
should be
Entry a = new Entry (10,"a title","a description");
If Entry is in the same package then you will not need to import it.
If Entry is in a different package, say com.example then you will need to do
Either
import com.example.Entry;
or
import com.example.*;
The second import will import all classes in the com.example package - usually not such a good thing.
You need new Entry
The new keyword creates the new object
Entry a = new Entry(10,"a title","a description")
a.print();
An Entry object is created with the a reference with the above instantiation.
For the import part of your question, if two files are in the same package, no import is needed. If you Entry class was in a different package than your FileReader class, then you would need to import mypackage.Entry
Try
Entry a = new Entry(/*args*/);
And if you need to import the class, then use the absolute name (package+class) and put it after import above the class declaration
import com.example.you.Entry;
In Eclipse you can do Ctrl+Shift+O to resolve all imports.
Method is undefined for the type in Eclipse. Can't seem to solve it. The error is in the lines: msg.setTimestamp( System.currentTimeMillis() ); and msg.setBody("this is a test SMS message");
package com.example.smsnotification;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
public class PopSMSActivity extends Activity{
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Retrieve serializable sms message object by the key "msg" used to pass to it
Intent in = this.getIntent();
PopMessage msg = (PopMessage) in.getSerializableExtra("msg");
//Case where we launch the app to test the UI EG: No incoming SMS
if(msg==null){
msg = new PopMessage();
con.setPhone("0123456789");
msg.setTimestamp( System.currentTimeMillis() );
msg.setBody("this is a test SMS message");
}
showDialog(msg);
}
Remove the code and write it back to eclipse. It worked for me....You can try copy and paste to after writing signature of function/class.
This means the PopMessage class doesn't provide the methods setTimestamp(long) and setBody(String).
There is no import statement for PopMessage in your code so I assume it is a class which you have implemented and is contained in the same package as the Activity which you have posted.
So you could either solve this by implementing those two methods in PopMessage or by removing the calls.
You may also extend your Eclipse Settings by activating the "save Actions" (Window->Preferences->Java->Editor->Save Actions) and use the Option "Organize Imports". This would at least add the propbably missing Import "...PopMessage" while you press Ctrl+S.