I am new to android and Java, just started. I am trying to build an app that shows a random string, which in my case is a quote, from an array list. I want the quote to show up in a textview, but I am also open to different methods of showing the quote. I have tried two ways of accomplishing this, but none of them work. I notice that both ways have trouble with add- when I hover over add (which is red) it says that cannot resolve symbol 'add'. Here is my first attempt:
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
import com.google.android.gms.appindexing.Action;
import com.google.android.gms.appindexing.AppIndex;
import com.google.android.gms.appindexing.Thing;
import com.google.android.gms.common.api.GoogleApiClient;
import java.util.ArrayList;
import java.util.Random;
public class MainActivity extends AppCompatActivity {
ArrayList<String> strings = new ArrayList<String>();
strings.add("quote 1");
strings.add("second quote");
strings.add("new quote");
strings.add("quote");
strings.add("sgbsb");
strings.add("sgsgb");
strings.add("sfgbdfg");
Random r = new Random();
String string = strings.get(r.nextInt(strings.size()));
TextView text = (TextView) findViewById(R.id.textView2);
private GoogleApiClient client;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build();
}
public Action getIndexApiAction() {
Thing object = new Thing.Builder()
.setName("Main Page") // TODO: Define a title for the content shown.
// TODO: Make sure this auto-generated URL is correct.
.setUrl(Uri.parse("http://[ENTER-YOUR-URL-HERE]"))
.build();
return new Action.Builder(Action.TYPE_VIEW)
.setObject(object)
.setActionStatus(Action.STATUS_TYPE_COMPLETED)
.build();
}
@Override
public void onStart() {
super.onStart();
client.connect();
AppIndex.AppIndexApi.start(client, getIndexApiAction());
}
@Override
public void onStop() {
super.onStop();
AppIndex.AppIndexApi.end(client, getIndexApiAction());
client.disconnect();
}
}
In my second attempt, which uses a slightly different code (that is why I left a lot out), the problem gets worst. It has problems with NextINT, size, and get.
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
import com.google.android.gms.appindexing.Action;
import com.google.android.gms.appindexing.AppIndex;
import com.google.android.gms.appindexing.Thing;
import com.google.android.gms.common.api.GoogleApiClient;
import java.util.ArrayList;
import java.util.Random;
public class MainActivity extends AppCompatActivity {
ArrayList<String> strings = new ArrayList<String>();
strings.add("My adress is 1274 ogden st");
strings.add("fsdfs");
strings.add("bsgsgb");
strings.add("sgbfg");
strings.add("sgbsb");
strings.add("sgsgb");
strings.add("sfgbdfg");
Random random = new Random();
strings.get(random.nextInt(strings.size()))
TextView text = (TextView) findViewById(R.id.textView2);
private GoogleApiClient client;
text.seText((CharSequence)strings));
Is there anyway to fix the cannot resolve symbol problem? I have tried all of the suggestions that I found on the internet, including Stackoverflow.
Aucun commentaire:
Enregistrer un commentaire