Assign list view id error - Android Studio - java

I'm new to Android development. I'm using Android studio. I created a listview in xml and when I'm trying to call it in MainActivity(Java file). It gives an error near R.layout.listView .
My code as follows.
public class MainActivity extends ActionBarActivity {
ListView l;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
l=(ListView)findViewById(R.layout.listView);
}
}

Wrong Tag
l=(ListView)findViewById(R.layout.listView);
Please set id instead of layout
Finally
public class MainActivity extends ActionBarActivity {
ListView l;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
l=(ListView)findViewById(R.id.listView);
}
}

Change
l=(ListView)findViewById(R.layout.listView);
To
l=(ListView)findViewById(R.id.listView);
Your code will be like this:
public class MainActivity extends ActionBarActivity {
ListView l;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
l=(ListView)findViewById(R.id.listView);
}
}

please check the same id that you have added to your xml file, I mean to say that in your xml file has the same id name listView that you have added to your java class.
public class MainActivity extends ActionBarActivity {
private ListView l;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
l = (ListView)findViewById(R.id.listView);
}
}

Related

How can I do not use constructor in MainActivity in Android Studio?

I'm a beginner.
I'm trying to make Locale project with Android Studio.
So I made a class regard with Context.
In MainActivity required a constructor.
But, I'm heard that MainActivity do not need constructor but onCreate.
Here is my code.
BaseContextWrapper
public class BaseContextWrapper extends AppCompatActivity {
//AppCompatActivity-FragmentActivity-ComponentActivity-Activity-ContextThemeWrapper-ContextWrapper-Context
public BaseContextWrapper(Context base) {
super.attachBaseContext(base);
}
#RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR1)
#NonNull
public static ContextWrapper wrap(Context context) {
Resources resources = context.getResources();
Configuration newConfig = new Configuration();
DisplayMetrics metrics = resources.getDisplayMetrics();
newConfig.setToDefaults();
newConfig.densityDpi = metrics.densityDpi;
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.JELLY_BEAN_MR1) {
context = context.createConfigurationContext(newConfig);
} else {
resources.updateConfiguration(newConfig, resources.getDisplayMetrics());
}
return new BaseContextWrapper(context);
}
}
MainActivity
public class MainActivity extends BaseContextWrapper {
public MainActivity(Context base) {
super(base);
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
It all depends on what you inherit from. If from "extends Base ContextWrapper", then the constructor is required, it is included in the class. But if from "extends AppCompatActivity" then nothing but the onCreate() method is needed
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
Rewrite your MainActivity as
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}

how to use activity context from other activity?

I want to use activity context from another activity simple code example below Any idea ?
public class Activity_A extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_Activity_A);
}
}
public class Activity_B extends AppCompatActivity {
Dialog dialog1 ;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_Activity_B);
dialog1 = new Dialog(I want Activity_A Context) ; // Is this possible ??
}
}
Thanks
Tamim you can definitely achieve this using a different class by making a public static function in that other class
public class Utils {
public static void showDialog(Context context){
//// your code here
}
now you use it wherever you want...
Utils.showDialog(this);

why I can not use create method out side a function in AppCompatActivity

My app is crashing when I launch when I write code like this
public class MainActivity extends AppCompatActivity {
MediaPlayer mplayer= MediaPlayer.create(this,R.raw.song);
public void playMusic(View view){
mplayer.start();
}
public void pauseMusic(View view){
mplayer.pause();
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
but when I write above code like this then it's working perfectly fine
public class MainActivity extends AppCompatActivity {
MediaPlayer mplayer;
public void playMusic(View view){
mplayer= MediaPlayer.create(this,R.raw.song);
mplayer.start();
}
public void pauseMusic(View view){
mplayer.pause();
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
please anybody tell me what is wrong with first code
Thanks
because the player must be initialized at the moment when the context is not empty. That is, in the upper code, context == null in player. Therefore, the application crashes.

Need Modified Code Structure for MainActivity.java

I want to insert code in MainActivity of 'Webview' Project To code in MainActivity of 'Push Notification' project.
As I'm new to Android & Java, I have spent hell lot of time but unable to produce a solution.
Suggest solution for following code structure:
'Push Notification' Project
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//------Code for push notification---------------------
}
'Webview' Project
public class MainActivity extends Activity{
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//-----------Code for WebView------------
}
private class CustomWebViewClient extends WebViewClient {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
//----------code-----------------
}
}
}
I have found solution myself. Hope others find it useful.
NOTE: Put push notification code before WebView code else it will not run push notification.
public class MainActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//------Code for push notification---------------------
//-----------Code for WebView------------
}
private class CustomWebViewClient extends WebViewClient {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
//----------code-----------------
}
}
}

How to correctly use findViewById if a class A extends class B extends Activity

In Android, I have a class A which extends class B which extends an ActionBarActivity. Class A sets the content view. For some reason, I'm unable to access the view elements from Class B. How can I access the view elements (ex: TextView)? When I attempt to use findViewById in class B, it is always null.
public class A extends B {
#Override
protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.activity_main);
super.onCreate(savedInstanceState);
}
}
public class B extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView myTextView = (TextView) findViewById(R.id.myTextView);
// myTextView is null
}
}
you're inheriting things backwards. Class B doesn't ever setContentView.
I think this is what you want:
public class A extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.activity_main);
super.onCreate(savedInstanceState);
}
}
public class B extends A {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// A called setContentView(), so now we can find views by id.
TextView myTextView = (TextView) findViewById(R.id.myTextView);
// myTextView now is not null.

Categories