Cannot resolve add (int, new tFragment ()) method. commit () in android studion 1.0.1

I worked with fragments in android studio 1.0.1. Just as the network process is unsuitable in the main user interface, I moved my code to another class containing fragments. But what happened afterwards does not help me continue. the error that I get cannot be solved by the add(R.id.container, new com.sunshine.example.sunshine.app.Fragment).commit(); method add(R.id.container, new com.sunshine.example.sunshine.app.Fragment).commit(); here is the code snippet of my MainActivity class, but everything is fine with the Fragment class.

 package com.sunshine.example.sunshine.app; import android.support.v7.app.ActionBarActivity; import android.support.v4.app.Fragment; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; public class MainActivity extends ActionBarActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); if (savedInstanceState == null) { getSupportFragmentManager().beginTransaction(). add(R.id.container, new ForecastFragment()).commit(); } } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.menu_main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); //noinspection SimplifiableIfStatement if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } } 
+5
source share
2 answers

a compile-time error assumes that your ForecastFragment uses support for the native app.Fragment fragment, but in your activity you use a transaction from the support library. You must match the import. Modify your ForecastFragment to use a Snippet from the support library

+25
source

I don’t know what you are doing, but it looks like you have a syntax error.

 new com.sunshine.example.sunshine.app.Fragment*()* 

You must put parentheses () at the end of any object creation statement.

0
source

Source: https://habr.com/ru/post/1214642/


All Articles