So I just started programming in eclipse, and I got the error ``main cannot be resolved or is not a field" with this code:
package com.example.wouter1;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
import com.example.wouter1.R;
public class HelloWorldActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main); // PROBLEM HERE <<<< # MAIN
TextView myText = new TextView(this);
myText.setText("hello world");
setContentView(myText);
}
}
Related
Nothing happens when I click the "start" button, the app suddenly closes. I think the View.OnClickListener doesn't work, but I can't find how to fix it.
package com.example.myquiz;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.res.ResourcesCompat;
import android.content.Intent;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private TextView title;
private Button start;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
title = findViewById(R.id.main_title);
start = findViewById(R.id.ma_startB);
Typeface typeface = ResourcesCompat.getFont(this,R.font.blacklist);
title.setTypeface(typeface);
start.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this,CategoryActivity.class);
startActivity(intent);
}
});
}
}
I got this error in Logcat:
error screenshot
This is the code for the CategoryActivity class, this is where I get the error from, but I really don't see where the problem is.
package com.example.myquiz;
import android.os.Bundle;
import android.view.MenuItem;
import android.widget.GridView;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import java.util.ArrayList;
import java.util.List;
public class CategoryActivity extends AppCompatActivity {
private GridView catGrid;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_category);
androidx.appcompat.widget.Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setTitle("Categories");
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
catGrid = findViewById(R.id.catGridView);
List<String> catList = new ArrayList<>();
catList.add("Cat 1");
catList.add("Cat 2");
catList.add("Cat 3");
catList.add("Cat 4");
catList.add("Cat 5");
catList.add("Cat 6");
CatGridAdapter adapter = new CatGridAdapter(catList);
catGrid.setAdapter(adapter);
}
#Override
public boolean onOptionsItemSelected(#NonNull MenuItem item) {
if(item.getItemId() == android.R.id.home)
{ CategoryActivity.this.finish();}
return super.onOptionsItemSelected(item);
}
}
The click listener is working correct, looking at the error log the launching of the CategoryActivity class is causing the app crash:
Caused by: java.lang.IllegalStateException: This Activity already has an action bar supplied by the window decor...
This tells the ActionBar / ToolBar inside the CategoryActivity is setup twice. Probably (if not, please share the class code) you can either remove setSupportActionBar() or change the theme of the CategoryActivity to something like: Theme.MaterialComponents.Light.NoActionBar to see if you get the Activity running after the button click.
I know that you can not 100% stop the user from taking a screenshot if he insists to. But I read that you can still stop manual screenshots by setting LayoutParams.FLAG_SECURE in Java.
I tried adding it to my MainApplication file but getWindow() kept on throwing errors no matter what I do. So I moved that line of code to the MainActivity file and it worked without any errors.
Problem is, I can still normally take screenshots.
MainApplication:
package com.testapp;
import android.app.Activity;
import com.reactnativenavigation.NavigationApplication;
import com.facebook.react.modules.i18nmanager.I18nUtil;
import com.facebook.react.ReactNativeHost;
import com.facebook.react.ReactPackage;
import com.facebook.react.shell.MainReactPackage;
import com.facebook.soloader.SoLoader;
import android.support.annotation.Nullable;
/* custom modules */
import com.oblador.vectoricons.VectorIconsPackage;
import org.pgsqlite.SQLitePluginPackage;
import com.learnium.RNDeviceInfo.RNDeviceInfo;
import java.util.Arrays;
import java.util.List;
public class MainApplication extends NavigationApplication {
#Override
public boolean isDebug() {
return BuildConfig.DEBUG;
}
#Nullable
#Override
public List<ReactPackage> createAdditionalReactPackages() {
return Arrays.<ReactPackage>asList(
new SQLitePluginPackage(),
new VectorIconsPackage(),
new RNDeviceInfo()
);
}
#Override
public void onCreate() {
super.onCreate();
I18nUtil sharedI18nUtilInstance = I18nUtil.getInstance();
sharedI18nUtilInstance.allowRTL(getApplicationContext(), false);
}
}
MainActivity:
package com.testapp;
import android.widget.ImageView;
import com.reactnativenavigation.controllers.SplashActivity;
import android.os.Bundle;
import android.view.WindowManager.LayoutParams;
public class MainActivity extends SplashActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().setFlags(LayoutParams.FLAG_SECURE, LayoutParams.FLAG_SECURE);
}
}
I did simply the following and it is working properly:
public class MainActivity extends AppCompatActivity
{
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_SECURE,
WindowManager.LayoutParams.FLAG_SECURE);
setContentView(R.layout.activity_main);
}
}
I am trying to finish off the first tutorial in the Android Studio documentation, but I can't even seem to do that.
I keep getting a:
Error:(17, 60) error: cannot find symbol variable EXTRA_MESSAGE
Here is the code to my DisplayMessageActivity.java file:
package com.example.myfirstapp;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.ViewGroup;
import android.widget.TextView;
public class DisplayMessageActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_message);
Intent intent = getIntent();
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
TextView textView = new TextView(this);
textView.setTextSize(40);
textView.setText(message);
ViewGroup layout = (ViewGroup) findViewById(R.id.activity_display_message);
layout.addView(textView);
}
}
The tutorial I am trying to follow is here:
https://developer.android.com/training/basics/firstapp/starting-activity.html
There is no static variable with name EXTRA_MESSAGE. Add public static String EXTRA_MESSAGE = <Some Message String>. And this error will be resolved
There is no static variable with name EXTRA_MESSAGE. Add public static String EXTRA_MESSAGE = .
package com.example.harikrishna.myapp4;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.view.View;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity {
EditText ed1,ed2;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ed1=(EditText)findViewById(R.id.editText);
ed2=(EditText)findViewById(R.id.editText2);
}
public void fun(View V){
if(V.getId()==R.id.button){
Editable e=ed1.getText();
String s=e.toString();
s=s.trim();
if(s.length()>0){
ed2=setText(s);
ed1=setText("");
ed2=requestFocus();
}
}
else{
String s=ed2.getText().toString().trim();
if(s.length()>0){
ed1=setText(s);
ed2=setText("");
ed1=requestFocus();
}
}
}
}
compiler shows error Error:(26, 21) error: cannot find symbol method setText(String) and cannot resolve method setText(java.lang.String).
pl could u pl some one let me know how to solve this problem
change your code from
ed2=setText(s);
ed1=setText("");
ed1=requestFocus();
to this
ed2.setText(s);
ed1.setText("");
ed1.requestFocus();
I am wondering why image view in java class pots error
like ImageView smtin;
And there's error.
It dont recognize it like class...
My project code is:
package com.klemenjezakon.koceSLO;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
ImageView slika;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
Intent i = getIntent();
koca podatki=(koca)i.getSerializableExtra("koca");
setContentView(R.layout.koca);
}
}
You have to put inside your class. So instead of:
ImageView slika;
public class MainActivity extends Activity {
it have to be:
public class MainActivity extends Activity {
ImageView slika;